同事问到这个,记录在这里吧。
国内不能直接访问 openai 的 api,于是想有没有中转的方法,网上一搜果然有,在自己的 vps 里实现一下。
Config server
root@vultr-3:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.6 LTS
Release: 20.04
Codename: focal
root@vultr-3:~# apt update
root@vultr-3:~# apt upgrade
ssh-copy-id [email protected]
ssh [email protected]
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.1
vi /etc/ssh/sshd_config
Port 10086
MaxAuthTries 6
MaxSessions 10
PubkeyAuthentication yes
PasswordAuthentication no
PermitEmptyPasswords no
ChallengeResponseAuthentication no
UsePAM yes
X11Forwarding yes
PrintMotd no
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
systemctl restart ssh.service
ufw allow 10086
Nginx setup and config
ssh [email protected] -p 10086
ufw allow 80
apt install nginx
systemctl status nginx
http://xx.xx.xx.xx -- will get "Welcome to nginx!"
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.1
vi /etc/nginx/nginx.conf
add below to under "http {"
server {
listen 80;
server_name xx.xx.xx.xx;
location /v1 {
proxy_pass https://api.openai.com/v1;
proxy_ssl_server_name on;
proxy_set_header Host api.openai.com;
chunked_transfer_encoding off;
proxy_buffering off;
proxy_cache off;
}
}
systemctl reload nginx
systemctl status nginx
Test in local
PROXY_URL="http://xx.xx.xx.xx/v1/chat/completions"
OPENAI_API_KEY="xxxx"
curl -s -k $PROXY_URL \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "你好"}],
"temperature": 0.7
}'
Usecase
- OpenAI Translator
- OpenCat