一、前言
宝塔反代openai官方API接口详细教程,实现国内使用ChatGPT+502 Bad Gateway问题解决,
此方法最简单快捷,没有复杂步骤,不容易出错,即最简单,零代码、零部署的方法。
二、实现前提
- 一台海外VPS服务器
- OpenAI官方的API_KEY
- 第三方网站系统或插件
关于第三方网站系统或插件,可以看另一篇文章介绍
《SparkAi系统详情及搭建部署文档》:
https://www.yuque.com/yuqueyonghutq9yt2/egy0d0/ah9sq89lgl6b7s4w
三、实现过程
这里使用的海外服务器,一般配置,如果只搭建ChatGPT的反代接口配置不需要很高。
**第一步:**使用宝塔面板新建一个站点 ------ 绑定自己的域名 ------ 选择纯静态 ------ 确认提交即可

**第二步:**配置该域名的SSL证书,这里可以使用宝塔自带的免费证书

开启SSL安全访问

**第三步:**添加配置反向代理

其中,目标地址填写openai官方的API地址:https://api.openai.com,发送域名也是:api.openai.com,会自动获取。
**第四步:**访问测试

出现访问反代地址出现502 Bad Gateway,
**原因:**SSL证书的原因,开启了SSL,在进行三次握手的时候,不匹配,造成握手失败!
**第五步:**解决访问出现502 Bad Gateway
在方向代理的配置文件中添加代码

第一行代码:
XML
proxy_ssl_server_name on;
第二行代码:
XML
proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
**第六步:**测试API

可以看到我们自己的反代api接口已经跟官方返回一样了。
四、使用
生成API KEY
进入openai的api官网,https://platform.openai.com/account/api-keys,创建KEY

配置对接API
对接到第三方网站系统或插件

测试使用
这里为国内服务器搭建的AI系统,网络为本地国内网络,使用效果

五、总结
这里使用的方法为宝塔自带的反向代理功能,就正常的方法。使用SSL,得解决Nginx三次握手的问题。