From b457a62abb555d2857c5ca65cdbaef2c3dfd5e5d Mon Sep 17 00:00:00 2001 From: fatwang2 Date: Fri, 24 May 2024 00:12:52 +0800 Subject: [PATCH] support coze.cn --- .env.template | 3 ++- README.md | 4 +++- README_CN.md | 4 +++- app.js | 5 +++-- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/.env.template b/.env.template index 5f25159..1551e64 100644 --- a/.env.template +++ b/.env.template @@ -1,2 +1,3 @@ BOT_ID=default_bot_id -BOT_CONFIG={"model_name_1": "bot_id_1", "model_name_2": "bot_id_2", "model_name_3": "bot_id_3"} \ No newline at end of file +BOT_CONFIG={"model_name_1": "bot_id_1", "model_name_2": "bot_id_2", "model_name_3": "bot_id_3"} +COZE_API_BASE=api.coze.com \ No newline at end of file diff --git a/README.md b/README.md index d199378..ab2a5ba 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ This project converts the Coze API to the OpenAI API format, giving you access t - Supports multi-bot switching ## Preparation -1. Register with [Coze](https://www.coze.com) and obtain your API token +1. Register with [coze.com](https://www.coze.com) or [coze.cn](https://www.coze.cn)and obtain your API token ![cozeapitoken](pictures/token.png) 2. Create your bot and publish it to the API @@ -85,6 +85,7 @@ This project provides some additional configuration items set with environment v | -------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- | | `BOT_ID` | Yes | The ID of the bot. Obtain it from the Develop page URL of your bot in Coze. The number after the bot parameter is the bot ID.| `73428668*****`| | `BOT_CONFIG` | No | Configure different models to correspond to different bot ids to enable fast bot switching on the client side. Models that are not included will request the default BOT_ID | `{"model_name_1": "bot_id_1", "model_name_2": "bot_id_2", "model_name_3": "bot_id_3"}`| +| `COZE_API_BASE` | No | Choose coze.com or coze.cn | `api.coze.com, api.coze.cn`| ## Roadmap **Coming Soon** @@ -94,6 +95,7 @@ This project provides some additional configuration items set with environment v * Docker support **Available Now** +* Coze.cn * Multi-bot switching * Workflow, Plugins, Knowledge base * Continuous dialogue with the history of chat diff --git a/README_CN.md b/README_CN.md index 315d4d9..3913abf 100644 --- a/README_CN.md +++ b/README_CN.md @@ -11,7 +11,7 @@ - 支持多机器人快速切换 # 准备工作 -1. 在 [Coze](https://www.coze.com)注册并获取您的 API 令牌 +1. 在 [coze.com](https://www.coze.com)或 [coze.cn](https://www.coze.cn)注册并获取您的 API 令牌 ![cozeapitoken](pictures/token.png) 2. 创建您的机器人并发布到 API @@ -87,6 +87,7 @@ console.log(data); | -------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- | | `BOT_ID` | Yes | 机器人的 ID。从 Coze 中机器人的开发页面 URL 获取它。 bot参数后面的数字是bot id.| `73428668*****`| | `BOT_CONFIG` | No | 配置模型和机器人ID的对应关系,实现在客户端切换模型来调用不同的机器人的效果。如果调用不在配置文件的模型,则走默认的BOT_ID| `{"model_name_1": "bot_id_1", "model_name_2": "bot_id_2", "model_name_3": "bot_id_3"}`| +| `COZE_API_BASE` | No | 选择coze.com或者coze.cn| `api.coze.com, api.coze.cn`| # 路线图 @@ -97,6 +98,7 @@ console.log(data); * Docker 部署 **现在可用** +* 支持 coze.cn * 多机器人切换 * 连续对话,有对话历史 * Zeabur&Vercel&Railway 部署 diff --git a/app.js b/app.js index 74d667c..cd8cf1e 100644 --- a/app.js +++ b/app.js @@ -6,6 +6,7 @@ dotenv.config(); const app = express(); app.use(bodyParser.json()); +const coze_api_base = process.env.COZE_API_BASE || "api.coze.com"; const default_bot_id = process.env.BOT_ID || ""; const botConfig = process.env.BOT_CONFIG ? JSON.parse(process.env.BOT_CONFIG) : {}; var corsHeaders = { @@ -87,8 +88,8 @@ app.post("/v1/chat/completions", async (req, res) => { bot_id: bot_id, chat_history: chatHistory }; - - const resp = await fetch("https://api.coze.com/open_api/v2/chat", { + const coze_api_url = `https://${coze_api_base}/open_api/v2/chat`; + const resp = await fetch(coze_api_url, { method: "POST", headers: { "Content-Type": "application/json",