openai-api-proxy
Simple proxy for OpenAi API via a one-line docker command
? If you are too lazy to build it yourself, you can try the third-party OpenAI API service that can be accessed in China and can be recharged on WeChat: API2D.com, which supports Chat, OpenCat, NextWeb, and VSCode plug-ins.
- Tencent Cloud Function Deployment Tutorial Tencent Cloud Function has supported SSE across the region since April 25. It is recommended to use it.
- Simplified Chinese instructions
- "How to quickly develop an OpenAI/GPT application: Domestic Developer Notes"
? SSE is already supported and content can be returned in real time
The following is translated by GPT. The following English was translated by GPT.
️ This is the server-side of the proxy, not the client-side. It needs to be deployed to a network environment that can access the openai api.
Features
- Supports SSE streaming output
- Built-in text moderation (requires Tencent Cloud KEY configuration)
- ? SSE streaming output supports text moderation, that's how powerful it is.
NodeJS Deployment
You can deploy ./app.js to any environment that supports nodejs 14+, such as cloud functions and edge computing platforms.
- Copy app.js and package.json to the directory
- Install dependencies with yarn install
- Start the service with node app.js
Docker Deployment
docker run -p 9000:9000 easychen/ai.level06.com:latest
The proxy address is http://${IP}:9000
Available Environment Variables
- PORT: Service port
- PROXY_KEY: Proxy access key, used to restrict access
- TIMEOUT: Request timeout, default 30 seconds
- TENCENT_CLOUD_SID: Tencent Cloud secret_id
- TENCENT_CLOUD_SKEY: Tencent Cloud secret_key
- TENCENT_CLOUD_AP: Tencent Cloud region (eg ap-singapore Singapore)
API Usage
- Change the domain/IP (with port number) of the openai request address in the original project (eg https://api.openai.com) to the domain/IP of this proxy.
- If PROXY_KEY is set, add
:<PROXY_KEY>
after the openai key. If not set, no modification is required. - moderation: true enables moderation, false disables moderation
- moderation_level: high interrupts all sentences whose moderation result is not Pass, low only interrupts sentences whose moderation result is Block.
Notes
- Only supports GET and POST methods, not file-related interfaces.
SSE is not currently supported, so stream-related options need to be turned off Now supported.
Client-side Usage Example
Using https://www.npmjs.com/package/chatgpt
as an example:
chatApi = new gpt . ChatGPTAPI ( {
apiKey : 'sk.....:<proxy_key_here>' ,
apiBaseUrl : "http://localhost:9001/v1" , // Replace with proxy domain/IP
} ) ;
Acknowledgements
- SSE reference to chatgpt-api project related code