如何解決本地開發(fā)調(diào)用OpenAI接口的問題

在開發(fā)過程中,如果需要在本地調(diào)用openAI接口進行開發(fā)調(diào)試,一般主要是通過以下兩種方式:直連和代理轉(zhuǎn)發(fā)。歡迎私信交流。

1. 直連

  • 1.簡單粗暴,懂的都懂
    file

2. 代理轉(zhuǎn)發(fā)

代理轉(zhuǎn)發(fā)又有兩種類型,使用第三方代理和自建代理兩種,下面將分別舉例說明

2.1. 第三方AI網(wǎng)關

  • 1.注冊Cloudflare,開通AI網(wǎng)關功能(Beta功能,目前免費)
    file
  • 2.使用AI網(wǎng)關地址替換官方接口地址,即可本地調(diào)用
    file
  • 3.在AI網(wǎng)關管理界面,還可以看到接口調(diào)用、tokens消耗、以及日志等情況,非常好用
    file

2.2. 自建Nginx

  • 1.查看Nginx版本和是否已配置stream模塊(Nginx版本需要在1.9.0以上,如果已安裝stream模塊,直接跳到步驟7):/usr/sbin/nginx -V | grep with-stream
    file
  • 2.重新下載已安裝版本的Nginx源碼包(上一步我查到已安裝的是版本號1.18.0):wget https://nginx.org/download/nginx-1.18.0.tar.gz
  • 3.在原有配置基礎上增加配置stream模塊
# 在原有配置基礎上,追加--with-stream --with-stream_realip_module,切記不要漏掉原有配置
./configure --with-cc-opt='-g -O2 -ffile-prefix-map=/build/nginx-d8gVax/nginx-1.18.0=. -flto=auto -ffat-lto-objects -flto=auto -ffat-lto-objects -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -flto=auto -ffat-lto-objects -flto=auto -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-compat --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --add-dynamic-module=/build/nginx-d8gVax/nginx-1.18.0/debian/modules/http-geoip2 --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_sub_module --with-http_ssl_module --with-stream --with-stream_realip_module
  • 4.編譯,千萬不要安裝
# 千萬不要執(zhí)行make install
make
  • 5.備份sbin目錄下原來的nginx文件:mv nginx nginx.bak
  • 6.將新編譯的nginx文件拷貝到Nginx的sbin目錄下
  • 7.配置OpenAI接口location(域名和ssl配置請自行設置好)
location /gpt {
    proxy_pass  https://api.openai.com/v1/;
    proxy_ssl_server_name on;
    proxy_set_header Host api.openai.com;
    proxy_set_header Connection '';
    proxy_http_version 1.1;
    # 不緩存,支持流式輸出,打字機效果
    proxy_cache off;  # 關閉緩存
    proxy_buffering off;  # 關閉代理緩沖
    chunked_transfer_encoding on;  # 開啟分塊傳輸編碼
    tcp_nopush on;  # 開啟TCP NOPUSH選項,禁止Nagle算法
    tcp_nodelay on;  # 開啟TCP NODELAY選項,禁止延遲ACK算法
    keepalive_timeout 120s;  # 設定keep-alive超時時間
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Forwarded-Proto $scheme;
}
  • 8.重新加載Nginx配置:/usr/sbin/nginx -s reload
  • 9.本地訪問,驗證接口調(diào)用成功
    file

本文由博客一文多發(fā)平臺 OpenWrite 發(fā)布!

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容