Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
fatwang2 committed Jun 14, 2024
2 parents e282a35 + 9ba64a6 commit 1de0a60
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
PORT=3000
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"}
COZE_API_BASE=api.coze.com
COZE_API_BASE=api.coze.com
33 changes: 25 additions & 8 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,21 @@ app.post("/v1/chat/completions", async (req, res) => {
res.end();
} else if (chunkObj.event === "ping") {
} else if (chunkObj.event === "error") {
console.error(`Error: ${chunkObj.code}, ${chunkObj.message}`);
res
.status(500)
.write(
`data: ${JSON.stringify({ error: chunkObj.message })}\n\n`
);
let errorMsg = chunkObj.code + " " + chunkObj.message;

if(chunkObj.error_information) {
errorMsg = chunkObj.error_information.err_msg;
}

console.error('Error: ', errorMsg);

res.write(
`data: ${JSON.stringify({ error: {
error: "Unexpected response from Coze API.",
message: errorMsg
}
})}\n\n`
);
res.write("data: [DONE]\n\n");
res.end();
}
Expand Down Expand Up @@ -238,9 +247,14 @@ app.post("/v1/chat/completions", async (req, res) => {
res.status(500).json({ error: "No answer message found." });
}
} else {
console.error("Error:", data.msg);
res
.status(500)
.json({ error: "Unexpected response from Coze API." });
.json({ error: {
error: "Unexpected response from Coze API.",
message: data.msg
}
});
}
})
.catch((error) => {
Expand All @@ -253,4 +267,7 @@ app.post("/v1/chat/completions", async (req, res) => {
}
});

app.listen(process.env.PORT || 3000);
const server = app.listen(process.env.PORT || 3000, function () {
let port = server.address().port
console.log('Ready! Listening all IP, port: %s. Example: at http://localhost:%s', port, port)
});
6 changes: 3 additions & 3 deletions vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
],
"routes": [
{
"src": "/v1/chat/completions",
"src": "/v1/(.*)",
"dest": "/app.js",
"methods": ["POST"]
"methods": ["OPTIONS", "POST"]
},
{
"src": "/",
"dest": "/app.js",
"methods": ["GET"]
}
]
}
}

0 comments on commit 1de0a60

Please sign in to comment.