中文 | English
OpenAI Forward
OpenAI API 接口转发服务
The fastest way to deploy openai api forwarding
本项目用于解决一些地区无法直接访问OpenAI的问题,将该服务部署在可以正常访问OpenAI API的(云)服务器上, 通过该服务转发OpenAI的请求。即搭建反向代理服务; 允许输入多个OpenAI API-KEY 组成轮询池; 可自定义二次分发api key.
由本项目搭建的长期代理地址:
https://api.openai-forward.com
https://cloudflare.worker.openai-forward.com
https://cloudflare.page.openai-forward.com
https://vercel.openai-forward.com
https://render.openai-forward.com
https://railway.openai-forward.com
功能
基础功能
- 支持转发OpenAI所有接口
- 支持流式响应
- 支持指定转发路由前缀
- docker部署
- pip 安装部署
- Railway 一键部署
- Render 一键部署
- cloudflare 部署
- Vercel一键部署
高级功能
- 允许输入多个openai api key 组成轮询池
- 自定义 转发api key (见高级配置)
- 流式响应对话日志
- 多接口转发
部署指南
👉 部署文档
提供以下几种部署方式
有海外vps方案
无vps免费部署方案
下面的部署仅提供单一转发功能
应用
聊天应用
基于开源项目ChatGPT-Next-Web搭建自己的chatgpt服务
替换docker启动命令中的 BASE_URL为我们自己搭建的代理服务地址
details
docker run -d \
-p 3000:3000 \
-e OPENAI_API_KEY="sk-******" \
-e BASE_URL="https://api.openai-forward.com" \
-e CODE="******" \
yidadaa/chatgpt-next-web
在代码中使用
Python
import openai
+ openai.api_base = "https://api.openai-forward.com/v1"
openai.api_key = "sk-******"
More Examples
JS/TS
import { Configuration } from "openai";
const configuration = new Configuration({
+ basePath: "https://api.openai-forward.com/v1",
apiKey: "sk-******",
});
gpt-3.5-turbo
curl https://api.openai-forward.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-******" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello!"}]
}'
Image Generation (DALL-E)
curl --location 'https://api.openai-forward.com/v1/images/generations' \
--header 'Authorization: Bearer sk-******' \
--header 'Content-Type: application/json' \
--data '{
"prompt": "A photo of a cat",
"n": 1,
"size": "512x512"
}'
配置选项
配置的设置方式支持两种
一种为在命令行中执行aifd run 的运行参数(如--port=8000)中指定;
另一种为读取环境变量的方式指定。
命令行参数
可通过 aifd run --help 查看
Click for more details
aifd run参数配置项
| 配置项 | 说明 | 默认值 |
|---|---|---|
| --port | 服务端口号 | 8000 |
| --workers | 工作进程数 | 1 |
| --openai_base_url | 同 OPENAI_BASE_URL | https://api.openai.com |
| --openai_route_prefix | 同 OPENAI_ROUTE_PREFIX | None |
| --api_key | 同 OPENAI_API_KEY | None |
| --forward_key | 同 FORWARD_KEY | None |
| --extra_base_url | 同 EXTRA_BASE_URL | None |
| --extra_route_prefix | 同 EXTRA_ROUTE_PREFIX | None |
| --log_chat | 同 LOG_CHAT | False |
环境变量配置项
支持从运行目录下的.env文件中读取
| 环境变量 | 说明 | 默认值 |
|---|---|---|
| OPENAI_BASE_URL | 默认 openai官方 api 地址 | https://api.openai.com |
| OPENAI_ROUTE_PREFIX | openai(接口格式)路由前缀 | / |
| OPENAI_API_KEY | 默认openai api key,支持多个默认api key, 以 sk- 开头, 以逗号分隔 |
无 |
| FORWARD_KEY | 允许调用方使用该key代替openai api key,支持多个forward key, 以逗号分隔; 如果设置了OPENAI_API_KEY,而没有设置FORWARD_KEY, 则客户端调用时无需提供密钥, 此时出于安全考虑不建议FORWARD_KEY置空 | 无 |
| EXTRA_BASE_URL | 额外转发服务地址 | 无 |
| EXTRA_ROUTE_PREFIX | 额外转发服务路由前缀 | 无 |
| LOG_CHAT | 是否记录聊天内容 | false |
高级配置
设置openai api_key为自定义的forward key
Click for more details
需要配置 OPENAI_API_KEY 和 FORWARD_KEY, 例如
OPENAI_API_KEY=sk-*******
FORWARD_KEY=fk-****** # 这里fk-token由我们自己定义
这里我们配置了FORWARD_KEY为fk-******, 那么后面客户端在调用时只需设置OPENAI_API_KEY为我们自定义的fk-****** 即可。
这样的好处是在使用一些需要输入OPENAI_API_KEY的第三方应用时,我们可以使用自定义的api-keyfk-******,
无需担心真正的OPENAI_API_KEY被泄露。并且可以对外分发fk-******。
用例:
curl https://api.openai-forward.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer fk-******" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello!"}]
}'
Python
import openai
+ openai.api_base = "https://api.openai-forward.com/v1"
- openai.api_key = "sk-******"
+ openai.api_key = "fk-******"
Web application
docker run -d \
-p 3000:3000 \
-e OPENAI_API_KEY="fk-******" \
-e BASE_URL="https://api.openai-forward.com" \
-e CODE="<your password>" \
yidadaa/chatgpt-next-web
多路由转发
支持转发不同地址的服务至同一端口的不同路由下,基本可以转发任何服务。
用例见 .env.example
对话日志
默认不记录对话日志,若要开启需设置环境变量LOG_CHAT=true
Click for more details
保存路径在当前目录下的Log/chat路径中。
记录格式为
{'messages': [{'user': 'hi'}], 'model': 'gpt-3.5-turbo', 'forwarded-for': '', 'uid': '467a17ec-bf39-4b65-9ebd-e722b3bdd5c3', 'datetime': '2023-07-18 14:01:21'}
{'assistant': 'Hello there! How can I assist you today?', 'uid': '467a17ec-bf39-4b65-9ebd-e722b3bdd5c3'}
{'messages': [{'user': 'Hello!'}], 'model': 'gpt-3.5-turbo', 'forwarded-for': '', 'uid': 'f844d156-e747-4887-aef8-e40d977b5ee7', 'datetime': '2023-07-18 14:01:23'}
{'assistant': 'Hi there! How can I assist you today?', 'uid': 'f844d156-e747-4887-aef8-e40d977b5ee7'}
转换为json格式:
aifd convert
得到chat.json:
[
{
"datetime": "2023-07-18 14:01:21",
"forwarded-for": "",
"model": "gpt-3.5-turbo",
"messages": [
{
"user": "hi"
}
],
"assistant": "Hello there! How can I assist you today?"
},
{
"datetime": "2023-07-18 14:01:23",
"forwarded-for": "",
"model": "gpt-3.5-turbo",
"messages": [
{
"user": "Hello!"
}
],
"assistant": "Hi there! How can I assist you today?"
}
]
Backer and Sponsor
License
OpenAI-Forward is licensed under the MIT license.
Metadata
Release files for ai-forward 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ai_forward-0.0.1.tar.gz | 14.0 kB | Without provenance | Details for ai_forward-0.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ai_forward-0.0.1-py3-none-any.whl | Python 3 | none | any | Details for ai_forward-0.0.1-py3-none-any.whl |
Total release size: 31.6 kB
Release files / ai_forward-0.0.1.tar.gz
| Download URL | ai_forward-0.0.1.tar.gz |
|---|---|
| Size | 14.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a9dab2947ed00e86efbd0ed59a23a2f2b6933c746966db1328223ef860d443ee
|
|
BLAKE2b-256 checksum How to use checksums |
e39be30c2aa2f52b01b3bb071dd68fce7bfc0ebf02eb8a8cd2419ea93b3121a1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.16
|
Release files / ai_forward-0.0.1-py3-none-any.whl
| Download URL | ai_forward-0.0.1-py3-none-any.whl |
|---|---|
| Size | 17.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e5586067648473075f7eb599f10f8c672fe564fe2e19a16721c8226101c8b7fd
|
|
BLAKE2b-256 checksum How to use checksums |
ad8f4137eff752bb58f34c0f1e90542415bbabab533e11476476cdf2140249fd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.16
|
Security for ai-forward 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.