Nginx反向代理OpenAI接口

612次阅读  |  发布于1年以前

准备

在开始之前,我们需要进行一些准备工作:

配置

执行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

Copyright© 2013-2019

京ICP备2023019179号-2