转载

Nginx反向代理OpenAI接口

准备

在开始之前,我们需要进行一些准备工作:
  • 一台境外服务器,比如说腾讯云新加坡服务器
  • 安装有Nginx环境,centos可以用yum install nginx安装。
  • 一个域名,比如说www.xxxx.com,以及对应的ssl证书(腾讯云可以在这里申请免费的证书)

配置

执行vim /etc/nginx/nginx.conf,添加如下配置:
# 省略其它配置,可以保持默认配置不变
server {
 listen 443 ssl;
 server_name {your_domain_name};
 ssl_certificate {your_cert_path};
 ssl_certificate_key {your_cert_key_path};
 ssl_session_cache shared:le_nginx_SSL:1m;
 ssl_session_timeout 1440m;
 ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
 ssl_prefer_server_ciphers on;
 ssl_ciphers TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+ECDSA+AES128:EECDH+aRSA+AES128:RSA+AES128:EECDH+ECDSA+AES256:EECDH+aRSA+AES256:RSA+AES256:EECDH+ECDSA+3DES:EECDH+aRSA+3DES:RSA+3DES:!MD5;
 location / {
 proxy_pass https://api.openai.com/;
 proxy_ssl_server_name on;
 proxy_set_header Host api.openai.com;
 proxy_set_header Connection '';
 proxy_http_version 1.1;
 chunked_transfer_encoding off;
 proxy_buffering off;
 proxy_cache off;
 proxy_set_header X-Forwarded-For $remote_addr;
 proxy_set_header X-Forwarded-Proto $scheme;
 }
}
{your_domain_name}可以填写www.xxx.com。 关于配置的介绍,可部分参考Nginx反向代理OpenAI API
重启nginx服务
sudo nginx -s stop 
sudo nginx

测试

配置环境:
export OPENAI_API_KEY=sk-xxxxx
export OPENAI_API_ORG=org-xxxx
测试模型列表:
curl https://xxx.com/v1/models \
 -H "Authorization: Bearer $OPENAI_API_KEY" \
 -H "OpenAI-Organization: $OPENAI_API_ORG"
测试对话:
curl https://xxx.com/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d '{
 "model": "gpt-3.5-turbo",
 "messages": [{"role": "user", "content": "Hello!"}]
}'
这里使用https://xxx.comhttps://www.xxx.com均可。
若使用openai-python包,则注意要设置openai代理服务器地址,主要有两种方法:
方法一:在openai/__init__.py(例如/data/home/zdaiot/.local/lib/python3.10/site-packages/openai/__init__.py)中添加如下行:
api_base = os.environ.get("OPENAI_API_BASE", "https://api.openai.com/v1")
api_base = "https://xxx.com/v1" # 添加的行
方法二:添加环境变量export OPENAI_API_BASE= https://xxx.com/v1

参考

Nginx反向代理OpenAI API 使用Nginx反向代理OpenAI API 如何用Nginx反向代理openAI接口
正文到此结束
Loading...