Package: ai-forward
中文|EnglishOpenAI ForwardOpenAI API 接口转发服务The fastest way to deploy openai api forwarding功能|部署指南|应用|配置选项|对话日志本项目用于解决一些地区无法直接访问OpenAI的问题，将该服务部署在可以正常访问OpenAI API的(云)服务器上，
通过该服务转发OpenAI的请求。即搭建反向代理服务; 允许输入多个OpenAI API-KEY 组成轮询池; 可自定义二次分发api key.由本项目搭建的长期代理地址：https://api.openai-forward.comhttps://cloudflare.worker.openai-forward.comhttps://cloudflare.page.openai-forward.comhttps://vercel.openai-forward.comhttps://render.openai-forward.comhttps://railway.openai-forward.com功能基础功能支持转发OpenAI所有接口支持流式响应支持指定转发路由前缀docker部署pip 安装部署Railway 一键部署Render 一键部署cloudflare 部署Vercel一键部署高级功能允许输入多个openai api key 组成轮询池自定义 转发api key (见高级配置)流式响应对话日志多接口转发部署指南👉部署文档提供以下几种部署方式有海外vps方案pip 安装部署Docker部署https://api.openai-forward.com无vps免费部署方案Railway部署https://railway.openai-forward.comRender一键部署https://render.openai-forward.com下面的部署仅提供单一转发功能一键Vercel部署https://vercel.openai-forward.comcloudflare部署https://cloudflare.page.openai-forward.com应用聊天应用基于开源项目ChatGPT-Next-Web搭建自己的chatgpt服务替换docker启动命令中的BASE_URL为我们自己搭建的代理服务地址detailsdockerrun-d\-p3000:3000\-eOPENAI_API_KEY="sk-******"\-eBASE_URL="https://api.openai-forward.com"\-eCODE="******"\yidadaa/chatgpt-next-web在代码中使用Pythonimport openai+ openai.api_base = "https://api.openai-forward.com/v1"openai.api_key = "sk-******"More ExamplesJS/TSimport { Configuration } from "openai";const configuration = new Configuration({+ basePath: "https://api.openai-forward.com/v1",apiKey: "sk-******",});gpt-3.5-turbocurlhttps://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 detailsaifd run参数配置项配置项说明默认值--port服务端口号8000--workers工作进程数1--openai_base_url同 OPENAI_BASE_URLhttps://api.openai.com--openai_route_prefix同 OPENAI_ROUTE_PREFIXNone--api_key同 OPENAI_API_KEYNone--forward_key同 FORWARD_KEYNone--extra_base_url同 EXTRA_BASE_URLNone--extra_route_prefix同 EXTRA_ROUTE_PREFIXNone--log_chat同 LOG_CHATFalse环境变量配置项支持从运行目录下的.env文件中读取环境变量说明默认值OPENAI_BASE_URL默认 openai官方 api 地址https://api.openai.comOPENAI_ROUTE_PREFIXopenai(接口格式)路由前缀/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 keyClick 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-******。用例:curlhttps://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!"}]}'Pythonimport openai+ openai.api_base = "https://api.openai-forward.com/v1"- openai.api_key = "sk-******"+ openai.api_key = "fk-******"Web applicationdockerrun-d\-p3000:3000\-eOPENAI_API_KEY="fk-******"\-eBASE_URL="https://api.openai-forward.com"\-eCODE="<your password>"\yidadaa/chatgpt-next-web多路由转发支持转发不同地址的服务至同一端口的不同路由下，基本可以转发任何服务。用例见.env.example对话日志默认不记录对话日志，若要开启需设置环境变量LOG_CHAT=trueClick 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格式：aifdconvert得到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 SponsorLicenseOpenAI-Forward is licensed under theMITlicense.