add support for non-stream request

This commit is contained in:
Archeb
2024-05-19 12:01:49 +08:00
parent 6afba31565
commit e8f3c98807
+33 -29
View File
@@ -39,30 +39,6 @@ app.post("/v1/messages", apiKeyAuth, (req, res) => {
res.setHeader("Access-Control-Allow-Origin", "*");
try {
let jsonBody = JSON.parse(req.rawBody);
if (!jsonBody.stream) {
res.send(
JSON.stringify({
id: uuidv4(),
content: [
{
text: "Please turn on streaming.",
},
{
id: "string",
name: "string",
input: {},
},
],
model: "string",
stop_reason: "end_turn",
stop_sequence: "string",
usage: {
input_tokens: 0,
output_tokens: 0,
},
})
);
} else if (jsonBody.stream == true) {
// 计算用户消息长度
let userMessage = [{ question: "", answer: "" }];
let userQuery = "";
@@ -145,6 +121,7 @@ app.post("/v1/messages", apiKeyAuth, (req, res) => {
let msgid = uuidv4();
if(jsonBody.stream){
// send message start
res.write(
createEvent("message_start", {
@@ -163,6 +140,7 @@ app.post("/v1/messages", apiKeyAuth, (req, res) => {
);
res.write(createEvent("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }));
res.write(createEvent("ping", { type: "ping" }));
}
// proxy response
@@ -213,7 +191,7 @@ app.post("/v1/messages", apiKeyAuth, (req, res) => {
throw e;
}
});
var finalResponse = "";
let cachedLine = "";
const stream = proxyReq.data;
stream.on("data", (chunk) => {
@@ -243,7 +221,11 @@ app.post("/v1/messages", apiKeyAuth, (req, res) => {
index: 0,
delta: { type: "text_delta", text: json.youChatToken },
});
if(jsonBody.stream){
res.write(createEvent("content_block_delta", chunkJSON));
}else{
finalResponse += json.youChatToken;
}
}
});
}else{
@@ -262,6 +244,7 @@ app.post("/v1/messages", apiKeyAuth, (req, res) => {
});
stream.on("end", () => {
if(jsonBody.stream){
// send ending
res.write(createEvent("content_block_stop", { type: "content_block_stop", index: 0 }));
res.write(
@@ -272,12 +255,33 @@ app.post("/v1/messages", apiKeyAuth, (req, res) => {
})
);
res.write(createEvent("message_stop", { type: "message_stop" }));
res.end();
});
} else {
throw new Error("Invalid request");
res.write(
JSON.stringify({
id: uuidv4(),
content: [
{
text: finalResponse,
},
{
id: "string",
name: "string",
input: {},
},
],
model: "string",
stop_reason: "end_turn",
stop_sequence: "string",
usage: {
input_tokens: 0,
output_tokens: 0,
},
})
);
}
res.end();
});
} catch (e) {
console.log(e);
res.write(JSON.stringify({ error: e.message }));