adjust openai request
This commit is contained in:
@@ -85,20 +85,23 @@ app.get("/v1/models", OpenAIApiKeyAuth, (req, res) => {
|
|||||||
});
|
});
|
||||||
// handle openai format model request
|
// handle openai format model request
|
||||||
app.post("/v1/chat/completions", OpenAIApiKeyAuth, (req, res) => {
|
app.post("/v1/chat/completions", OpenAIApiKeyAuth, (req, res) => {
|
||||||
|
// 用于存储请求体
|
||||||
req.rawBody = "";
|
req.rawBody = "";
|
||||||
req.setEncoding("utf8");
|
req.setEncoding("utf8");
|
||||||
|
|
||||||
|
// 接收数据
|
||||||
req.on("data", function (chunk) {
|
req.on("data", function (chunk) {
|
||||||
req.rawBody += chunk;
|
req.rawBody += chunk;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 数据接收完毕后处理请求
|
||||||
req.on("end", async () => {
|
req.on("end", async () => {
|
||||||
console.log("Handling request of OpenAI format");
|
console.log("处理 OpenAI 格式的请求");
|
||||||
res.setHeader("Content-Type", "text/event-stream;charset=utf-8");
|
res.setHeader("Content-Type", "text/event-stream;charset=utf-8");
|
||||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||||
let jsonBody = JSON.parse(req.rawBody);
|
let jsonBody = JSON.parse(req.rawBody);
|
||||||
|
|
||||||
// Normalize messages
|
// 规范化消息
|
||||||
jsonBody.messages = openaiNormalizeMessages(jsonBody.messages);
|
jsonBody.messages = openaiNormalizeMessages(jsonBody.messages);
|
||||||
|
|
||||||
console.log("message length:" + jsonBody.messages.length);
|
console.log("message length:" + jsonBody.messages.length);
|
||||||
@@ -119,12 +122,11 @@ app.post("/v1/chat/completions", OpenAIApiKeyAuth, (req, res) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// decide which session to use randomly
|
// 随机选择一个会话
|
||||||
let randomSession = Object.keys(provider.sessions)[Math.floor(Math.random() * Object.keys(provider.sessions).length)];
|
let randomSession = Object.keys(sessions)[Math.floor(Math.random() * Object.keys(sessions).length)];
|
||||||
console.log("Using session " + randomSession);
|
console.log("Using session " + randomSession);
|
||||||
// call provider to get completion
|
|
||||||
|
|
||||||
// try to map model
|
// 尝试映射模型
|
||||||
if (jsonBody.model && modelMappping[jsonBody.model]) {
|
if (jsonBody.model && modelMappping[jsonBody.model]) {
|
||||||
jsonBody.model = modelMappping[jsonBody.model];
|
jsonBody.model = modelMappping[jsonBody.model];
|
||||||
}
|
}
|
||||||
@@ -134,7 +136,7 @@ app.post("/v1/chat/completions", OpenAIApiKeyAuth, (req, res) => {
|
|||||||
}
|
}
|
||||||
console.log("Using model " + jsonBody.model);
|
console.log("Using model " + jsonBody.model);
|
||||||
|
|
||||||
// call provider to get completion
|
// 调用 provider 获取回复
|
||||||
try {
|
try {
|
||||||
const {completion, cancel} = await provider.getCompletion({
|
const {completion, cancel} = await provider.getCompletion({
|
||||||
username: randomSession,
|
username: randomSession,
|
||||||
@@ -144,9 +146,10 @@ app.post("/v1/chat/completions", OpenAIApiKeyAuth, (req, res) => {
|
|||||||
useCustomMode: process.env.USE_CUSTOM_MODE === "true"
|
useCustomMode: process.env.USE_CUSTOM_MODE === "true"
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 监听开始事件
|
||||||
completion.on("start", (id) => {
|
completion.on("start", (id) => {
|
||||||
if (jsonBody.stream) {
|
if (jsonBody.stream) {
|
||||||
// send message start
|
// 发送消息开始
|
||||||
res.write(createEvent(":", "queue heartbeat 114514"));
|
res.write(createEvent(":", "queue heartbeat 114514"));
|
||||||
res.write(
|
res.write(
|
||||||
createEvent("data", {
|
createEvent("data", {
|
||||||
@@ -166,9 +169,10 @@ app.post("/v1/chat/completions", OpenAIApiKeyAuth, (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 监听完成事件
|
||||||
completion.on("completion", (id, text) => {
|
completion.on("completion", (id, text) => {
|
||||||
if (jsonBody.stream) {
|
if (jsonBody.stream) {
|
||||||
// send message delta
|
// 发送消息增量
|
||||||
res.write(
|
res.write(
|
||||||
createEvent("data", {
|
createEvent("data", {
|
||||||
choices: [
|
choices: [
|
||||||
@@ -192,7 +196,7 @@ app.post("/v1/chat/completions", OpenAIApiKeyAuth, (req, res) => {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
// 只会发一次,发送final response
|
// 只发送一次,发送最终响应
|
||||||
res.write(
|
res.write(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
id: id,
|
id: id,
|
||||||
@@ -222,6 +226,7 @@ app.post("/v1/chat/completions", OpenAIApiKeyAuth, (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 监听结束事件
|
||||||
completion.on("end", () => {
|
completion.on("end", () => {
|
||||||
if (jsonBody.stream) {
|
if (jsonBody.stream) {
|
||||||
res.write(createEvent("data", "[DONE]"));
|
res.write(createEvent("data", "[DONE]"));
|
||||||
@@ -229,6 +234,7 @@ app.post("/v1/chat/completions", OpenAIApiKeyAuth, (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 监听客户端关闭事件
|
||||||
res.on("close", () => {
|
res.on("close", () => {
|
||||||
console.log(" > [Client closed]");
|
console.log(" > [Client closed]");
|
||||||
completion.removeAllListeners();
|
completion.removeAllListeners();
|
||||||
|
|||||||
Reference in New Issue
Block a user