自主部署代理
不用搭环境,自己有境外的vps就行,最好是openai支持的地区的vps,下载bin里面的执行文件直接就能跑,最简单的api proxy方式,最重要的是支持SSE,让客户端请求时响应得更加迅速,也提供了golang的源码,需要定制的可以自行完善。1
./api_proxy -daemon -port 9000 # 最好开启daemon守护进程模式
docker的部署方式,直接拉镜像部署,不用自己配置环境,这种自主部署的代理可以支持sse,使用nodejs的可以参考:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15sudo yum remove docker
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io
sudo systemctl start docker # 这个不要忘了
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
docker pull python:3-slim # 可以用来跑python脚本
docker run -dit --name proxy -p 9000:9000 easychen/ai.level06.com:latest # Proxy地址为 http://${IP}:9000
# 可用环境变量
# PORT: 服务端口
# PROXY_KEY: 代理访问KEY,用于限制访问
# TIMEOUT:请求超时时间,默认5秒
openai安装
1 | pip3 install openai |
python测试流式输出:1
2
3
4
5
6
7
8
9
10
11
12
13
14import os
import openai
openai.api_key = ""
openai.api_base = "https://api.openai.com/v1" # 换成代理,一定要加v1
for resp in openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": "hello"}
],
stream = True):
if 'content' in resp.choices[0].delta:
print(resp.choices[0].delta.content, end="", flush=True)
客户端使用
以 https://www.npmjs.com/package/chatgpt 为例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
28chatApi= new gpt.ChatGPTAPI({
apiKey: 'sk.....:<proxy_key写这里>',
apiBaseUrl: "http://localhost:9001", // 传递代理地址
});
// WARNING: this method will expose your access token to a third-party. Please be
// aware of the risks before using this method.
const api = new ChatGPTUnofficialProxyAPI({
// optionally override the default reverse proxy URL (or use one of your own...)
// apiReverseProxyUrl: 'https://chat.duti.tech/api/conversation',
// apiReverseProxyUrl: 'https://gpt.pawan.krd/backend-api/conversation',
accessToken: process.env.OPENAI_ACCESS_TOKEN,
debug: false
})
const prompt = '写一首关于猫的诗词'
const res = await api.sendMessage(prompt, {
onProgress: (partialResponse) => {
process.stdout.write(`\r${partialResponse.text}`);
}
})
// const prompt2 = '你能让它变得更短吗?'
// api.sendMessage(prompt2, {
// conversationId: res.conversationId,
// parentMessageId: res.id
// })