add support for non-stream request
This commit is contained in:
@@ -39,30 +39,6 @@ app.post("/v1/messages", apiKeyAuth, (req, res) => {
|
|||||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||||
try {
|
try {
|
||||||
let jsonBody = JSON.parse(req.rawBody);
|
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 userMessage = [{ question: "", answer: "" }];
|
||||||
let userQuery = "";
|
let userQuery = "";
|
||||||
@@ -145,6 +121,7 @@ app.post("/v1/messages", apiKeyAuth, (req, res) => {
|
|||||||
|
|
||||||
let msgid = uuidv4();
|
let msgid = uuidv4();
|
||||||
|
|
||||||
|
if(jsonBody.stream){
|
||||||
// send message start
|
// send message start
|
||||||
res.write(
|
res.write(
|
||||||
createEvent("message_start", {
|
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("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }));
|
||||||
res.write(createEvent("ping", { type: "ping" }));
|
res.write(createEvent("ping", { type: "ping" }));
|
||||||
|
}
|
||||||
|
|
||||||
// proxy response
|
// proxy response
|
||||||
|
|
||||||
@@ -213,7 +191,7 @@ app.post("/v1/messages", apiKeyAuth, (req, res) => {
|
|||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
var finalResponse = "";
|
||||||
let cachedLine = "";
|
let cachedLine = "";
|
||||||
const stream = proxyReq.data;
|
const stream = proxyReq.data;
|
||||||
stream.on("data", (chunk) => {
|
stream.on("data", (chunk) => {
|
||||||
@@ -243,7 +221,11 @@ app.post("/v1/messages", apiKeyAuth, (req, res) => {
|
|||||||
index: 0,
|
index: 0,
|
||||||
delta: { type: "text_delta", text: json.youChatToken },
|
delta: { type: "text_delta", text: json.youChatToken },
|
||||||
});
|
});
|
||||||
|
if(jsonBody.stream){
|
||||||
res.write(createEvent("content_block_delta", chunkJSON));
|
res.write(createEvent("content_block_delta", chunkJSON));
|
||||||
|
}else{
|
||||||
|
finalResponse += json.youChatToken;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}else{
|
}else{
|
||||||
@@ -262,6 +244,7 @@ app.post("/v1/messages", apiKeyAuth, (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
stream.on("end", () => {
|
stream.on("end", () => {
|
||||||
|
if(jsonBody.stream){
|
||||||
// send ending
|
// send ending
|
||||||
res.write(createEvent("content_block_stop", { type: "content_block_stop", index: 0 }));
|
res.write(createEvent("content_block_stop", { type: "content_block_stop", index: 0 }));
|
||||||
res.write(
|
res.write(
|
||||||
@@ -272,12 +255,33 @@ app.post("/v1/messages", apiKeyAuth, (req, res) => {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
res.write(createEvent("message_stop", { type: "message_stop" }));
|
res.write(createEvent("message_stop", { type: "message_stop" }));
|
||||||
|
|
||||||
res.end();
|
|
||||||
});
|
|
||||||
} else {
|
} 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) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
res.write(JSON.stringify({ error: e.message }));
|
res.write(JSON.stringify({ error: e.message }));
|
||||||
|
|||||||
Reference in New Issue
Block a user