一个计算机技术爱好者与学习者

0%

好好学Docker:使用Docker安装配置OPANAI API代理

1. 前言

OPENAI API代理,顾名思义,是一种用于代理OPENAI API的专用代理。
目前OPENAI API代理中,做的比较好的是 openai-forward。本文中,我们基于Docker安装配置一下openai-forward。

参考文档:

2. 使用openai-forward代理

KenyonY/openai-forward项目作者提供了三个公开的代理服务。

命令行使用OPENAI API代理的方法:

1
openai -b "https://api.openai-forward.com/v1" api chat_completions.create -m gpt-3.5-turbo -g user "Hello world"

Python使用OPEANAI API代理的方法:

1
2
3
4
5
6
7
8
import os
import openai

openai.api_base = 'https://api.openai-forward.com/v1'
openai.api_key = 'sk-xxx'

chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])
print(chat_completion)

3. 自建openai-forward代理

3.1. 机器准备

如果打算自建 openai-forward 服务,那么首先要准备一台海外的可以访问 OPENAI API 的主机。

主机测试方法:

1
curl https://api.openai.com/v1

正常响应:

1
2
3
4
5
6
7
8
{
"error": {
"type": "invalid_request_error",
"code": "unknown_url",
"message": "Unknown request URL: GET /v1. Please check the URL for typos, or see the docs at https://platform.openai.com/docs/api-reference/.",
"param": null
}
}

如果有正常响应,那么说明这台主机是满足需求的。

3.2. 安装配置openai-forward

1、下载openai-forward镜像

1
docker pull beidongjiedeguang/openai-forward:latest

2、启动openai-forward容器

1
2
3
4
docker run --name openai-forward -d \
--restart=always \
-p 8000:8000 \
beidongjiedeguang/openai-forward:latest

3、检查容器状态

1
2
docker ps
docker logs openai-forward

4. 配置Nginx

http配置:

1
2
3
4
5
6
http {
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
}

server配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
server {
listen 80;
server_name openai-forward.voidking.com;
rewrite ^(.*) https://openai-forward.voidking.com$1 permanent;
}

server {
listen 443 ssl;
server_name openai-forward.voidking.com;
access_log off;
error_log off;

ssl_certificate /etc/nginx/ssl/openai-forward.voidking.com/openai-forward.voidking.com_bundle.crt;
ssl_certificate_key /etc/nginx/ssl/openai-forward.voidking.com/openai-forward.voidking.com.key;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_max_temp_file_size 0;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_temp_file_write_size 64k;
proxy_buffering off;
}
}