A service based on Cloudflare Worker proxy access to OpenAI/AzureOpenAI API, currently supporting access to enterprise WeChat applications and WeChat public accounts
There are three ways
快速编辑
in the upper right corner, download the packaged js file in the release of this project, copy the content to the editor, and save设置
in the middle row of options on the page, select变量
on the left, scroll down the page to KV 命名空间绑定
,编辑变量
and click to add the binding created in the second part Namespace, the variable name on the left must be filled in KV
环境变量
at the top, and read the following chapters to configure them on the interface according to the service you want to access.触发器
in the middle row of options on the Worker page to add the domain name to the Worker. You can refer to using Cloudflare Workers to solve the problem of API inaccessibility of OpenAI and ChatGPT. Note: When wrangler
executes deployment, it will print relevant information, such as the environment variables and KV ID configured in wrangler.toml
. These can be seen from the Github Actions execution log
kv_namespaces
id
in the file wrangler.toml
is filled in with the id of the created namespace for binding.Settings
- Secrets and variables
- Actions
in your own project repository[vars]
in the supplementary file wrangler.toml
in the following chapters according to the service to be accessed, and fill in the entire file contentDeploy
Action on the Github Actions page to complete the deployment.Need to understand the basic use of Git and Node.Js
wranger
is the official command line client of Cloudflare. After logging in npx wrangler login
, you can deploy Workers through the command line.KV
can be created through the command line, and then kv_namespaces
id
in the file wrangler.toml
is filled in with the id of the created namespace for binding.[vars]
in the configuration file wrangler.toml
according to the services you want to access.wrangler.toml
to wrangler.prod.toml
and use the command npm run deploy
to deploy to WorkerCompared with WeChat, it is more recommended to use Enterprise WeChat. The advantages are: 1. The visible range of the enterprise WeChat application is controlled and is not completely open like the public account; 2. After joining the enterprise, the personal WeChat can enter the application through the company in the personal WeChat, which only requires 2 clicks, while entering the subscription account requires More clicks; 3. Support enterprise WeChat group robot alarms
Register for Enterprise WeChat, fill in the information as you like, no authentication required
Enterprise WeChat Management Backend-My Enterprise-Enterprise Information Page Confirm your enterprise ID (corpid)
Application management - create an application, you can view the AgentId and Secret, receive messages - set up the API reception to randomly generate Token, EncodingAESKey, and do not need to save it at this time.
Worker configures the environment variables required by the application. It can be configured through interface operations, see the third step of Cloudflare interface deployment above. It can be configured through the configuration file. See the third step of Github Action deployment and the third step of local command line deployment.
variable name | Content description | Remark |
---|---|---|
WEWORK_ID_LIST | A list of application IDs that are allowed to be accessed, multiples separated by commas. | The ID is customized by you. It is recommended to consist of letters and numbers within 10 digits. |
WEWORK_${ID}_APPID | corporate WeChat corporate | Replace ${ID} with your custom ID |
WEWORK_${ID}_TOKEN | Application Token | Replace ${ID} with your custom ID |
WEWORK_${ID}_AES_KEY | EncodingAESKey applied | Replace ${ID} with your custom ID |
WEWORK_${ID}_ADMIN_USER_ID_LIST | admin user list, multiples separated by commas | You can leave it unconfigured for now and configure it later when you know your user ID. |
According to the domain name and customized ID, obtain the server address (URL) in step 3 and configure it. The format is https://${域名}/openai/wework/${ID}
. For example, if the domain name is xxx.com
and the customized ID is id123
, the server address (URL) is https://xxx.com/openai/wework/id123
. Although it is suggested为保障企业数据安全,需配置备案主体与当前企业主体相同或有关联关系的域名
. However, actual testing found that Cloudflare binding its own domain name can also pass the verification. I am not sure what the specific rules are.
Users can join the company through My Business-WeChat Plug-in-Invite Follower and scan the QR code, and enter the company to use applications on personal WeChat. Note that you can turn off application messages in Business WeChat Settings - New Message Notification - Only Receive Messages in Business WeChat, otherwise you will not receive reply messages in your personal WeChat.
Register a WeChat public account, usually a personal subscription account, with low qualification verification threshold
Public Account Management Platform - Settings and Development - Basic Configuration Page Confirm your developer ID (AppID), generate token (Token), message encryption and decryption key (EncodingAESKey) (only required if security mode or compatibility mode is turned on), this There is no need to enable server configuration when
Worker configures the environment variables required by the WeChat public account. It can be configured through interface operations, see the third step of Cloudflare interface deployment above. It can be configured through the configuration file. See the third step of Github Action deployment and the third step of local command line deployment.
variable name | Content description | Remark |
---|---|---|
WECHAT_ID_LIST | A list of official account IDs that are allowed to be accessed, multiple ones separated by commas. | The ID is customized by you. It is recommended to consist of letters and numbers within 10 digits. |
WECHAT_${ID}_APPID | Developer ID (AppID) of the official account | Replace ${ID} with your custom ID |
WECHAT_${ID}_TOKEN | Token of public account | Replace ${ID} with your custom ID |
WECHAT_${ID}_AES_KEY | Official account’s message encryption and decryption key (EncodingAESKey) | Replace ${ID} with your custom ID. This is only required when safe mode or compatibility mode is enabled. |
WECHAT_ADMIN_USER_ID_LIST | admin user list, multiples separated by commas | You can leave it unconfigured for now and configure it later when you know your user ID. |
WECHAT_ADMIN_OPENAI_KEY | admin user’s OpenAI Key | Optional, WECHAT_GUEST_OPENAI_KEY will be used by default, with higher priority than OpenAI configuration |
WECHAT_GUEST_OPENAI_KEY | Visitor’s OpenAI Key | Optional, can be used at will, has a higher priority than OpenAI's configuration, configure with caution! |
Based on the domain name and customized ID, obtain the server address (URL) configured in the second step and configure it. The format is https://${域名}/openai/wechat/${ID}
. For example, if the domain name is xxx.com
and the customized ID is id123
, the server address (URL) is https://xxx.com/openai/wechat/id123
The message encryption and decryption method generally selects plain text, enables server configuration, and can be used after successful access verification.
Case can be ignored when inputting
Order | Available roles | illustrate |
---|---|---|
/help | visitor, user | Get command help information |
/setOpenAiType | visitor, user | Set whether to use openAi or azureOpenAi. OpenAi is used by default. |
/bindKey | visitor, user | Bind the OpenAI api key in the format of /bindKey xxx. If the key has been bound, it will be overwritten. After binding, use the /testKey command to test whether it is available normally. |
/unbindKey | user | Unbind OpenAI api key |
/bindAzureKey | visitor, user | Bind AzureOpenAI key in the format of /bindAzureKey yourResourceName:yourDeploymentName:yourApiKey. If the key has been bound, it will be overwritten. After binding, use the /testKey command to test whether it is available normally. |
/unbindAzureKey | user | Unbind AzureOpenAI api key |
/testKey | user | Call OpenAI/AzureOpenAI to list the model interface, test whether the api key is normally bound and available, and does not consume any usage. |
/setChatType | user, trialist | Switch the conversation mode, you can choose between 'single chat' and 'serial chat', and the default is 'single chat'. 'Single chat' only processes the current input, and 'cross chat' will bring historical chat records to request OpenAI, consuming more usage. |
/newChat | user, trialist | Clear previous chat history and start a new chat |
/retry | user, trialist | Get the corresponding answer based on msgId, and the answer will only be kept for 3 minutes. Retention time is configurable via ANSWER_EXPIRES_MINUTES |
.. | user, trialist | Retry last delayed answer |
. . | user, trialist | Retry last delayed answer |
/bindSessionKey | visitor, user | To bind the OpenAI session key, you can view the request header of https://api.openai.com/v1/usage on the usage page. The original session key will become invalid every time you log in again and you need to bind it again. |
/unbindSessionKey | user | Unbind OpenAI session key |
/usage | user | There may be a delay of about 5 minutes to obtain this month's usage information, and you need to bind the OpenAI api key or session key. |
/freeUsage | user | There may be a delay of about 5 minutes to obtain free usage information, and you need to bind the OpenAI session key. |
/system | user,administrator | View some current system configuration information, such as the current OpenAI model, current user ID, etc. |
/faq | visitor, user | some frequently asked questions |
/adminAuth | visitor, user | Hide commands and become an administrator through token authentication to avoid the trouble of configuring the admin user ID for each platform. ADMIN_AUTH_TOKEN needs to be configured first |
/testAlarm | administrator | Hide the command and test sending an alarm message. ALARM_URL needs to be configured first |
/feedback | visitor, user | Users send feedback to developers. Need to configure FEEDBACK_URL first |
Configuration name | default value | illustrate |
---|---|---|
CHAT_MODEL | gpt-3.5-turbo | OpenAI model name |
OPEN_AI_API_PREFIX | https://api.openai.com/v1 | OpenAI’s common API prefix |
GUEST_KEY | Optional, the visitor's default openai key can be used at will, it works across platforms, so configure it carefully! | |
ADMIN_KEY | Optional, the default openai key of the admin user, valid across platforms | |
OPEN_AI_USAGE | https://api.openai.com/dashboard/billing/usage | OpenAI usage address |
OPEN_AI_FREE_USAGE | https://api.openai.com/dashboard/billing/credit_grants | OpenAI free usage address |
OPEN_AI_API_TIMEOUT_MS | 30000 | OpenAI API request timeout, milliseconds |
OPEN_AI_API_KEY_OCCUPYING_DURATION | 0 | The OpenAI API key usage interval, in seconds, is used to limit current, and is enabled when it is greater than 0. Using kv to implement current limiting can only be said to be barely usable. |
OPEN_AI_API_CHAT_EXTRA_PARAMS | OpenAI API chat additional global parameters, JSON string, available parameters | |
MIN_CHAT_RESPONSE_TOKEN_NUM | 500 | The minimum number of tokens OpenAI replies to |
MAX_HISTORY_LENGTH | 20 | Max chat history length |
ANSWER_EXPIRES_MINUTES | 3 | How long to save questions/answers, minutes |
SYSTEM_INIT_MESSAGE | You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible. Knowledge cutoff: 2021-09-01. Current is 2023 | The default first system message sent to OpenAI, which can be used to adjust the model |
WELCOME_MESSAGE | Welcome, you can enter /help to view the currently available commands | Welcome message when users follow the app |
AZURE_API_PREFIX | https://RESOURCENAME.openai.azure.com/openai | Azure OpenAI via request prefix |
AZURE_CHAT_API_VERSION | 2023-03-15-preview | Chat interface API version |
AZURE_LIST_MODEL_API_VERSION | 2022-12-01 | List model interface API versions |
AZURE_GUEST_KEY | Optional, the visitor's default Azure OpenAI key can be used at will, it works across platforms, so configure it carefully! | |
AZURE_ADMIN_KEY | Optional, the default azure openai key for admin users, valid across platforms |
Configuration name | default value | illustrate |
---|---|---|
DEBUG_MODE | false | Debug mode, more logs will be printed |
ECHO_MODE | false | echo mode, directly returns the received information |
ALARM_URL | Alarm URL, currently supports enterprise WeChat group robots and custom addresses. Custom addresses will POST { "msg": "xxxx" } JSON data | |
FEEDBACK_URL | User feedback URL currently supports enterprise WeChat group robots and custom addresses. Custom addresses will be POST { "msg": "xxxx" } JSON data. | |
ADMIN_AUTH_TOKEN | The token authenticated as admin should be of sufficient length and complexity, and should be configured and saved carefully! |