adjust message format
This commit is contained in:
@@ -286,10 +286,14 @@ app.post("/v1/messages", AnthropicApiKeyAuth, (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
req.on("end", async () => {
|
req.on("end", async () => {
|
||||||
console.log("Handling request of Anthropic format");
|
console.log("处理 Anthropic 格式的请求");
|
||||||
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);
|
||||||
|
|
||||||
|
// 处理消息格式
|
||||||
|
jsonBody.messages = normalizeMessages(jsonBody.messages);
|
||||||
|
|
||||||
if (jsonBody.system) {
|
if (jsonBody.system) {
|
||||||
// 把系统消息加入messages的首条
|
// 把系统消息加入messages的首条
|
||||||
jsonBody.messages.unshift({role: "system", content: jsonBody.system});
|
jsonBody.messages.unshift({role: "system", content: jsonBody.system});
|
||||||
@@ -312,20 +316,19 @@ app.post("/v1/messages", AnthropicApiKeyAuth, (req, res) => {
|
|||||||
console.log(`Using model ${proxyModel}`);
|
console.log(`Using model ${proxyModel}`);
|
||||||
|
|
||||||
// call provider to get completion
|
// call provider to get completion
|
||||||
await provider
|
try {
|
||||||
.getCompletion({
|
const {completion, cancel} = await provider.getCompletion({
|
||||||
username: randomSession,
|
username: randomSession,
|
||||||
messages: jsonBody.messages,
|
messages: jsonBody.messages,
|
||||||
stream: !!jsonBody.stream,
|
stream: !!jsonBody.stream,
|
||||||
proxyModel: proxyModel,
|
proxyModel: proxyModel,
|
||||||
useCustomMode: process.env.USE_CUSTOM_MODE === "true"
|
useCustomMode: process.env.USE_CUSTOM_MODE === "true"
|
||||||
})
|
});
|
||||||
.then(({completion, cancel}) => {
|
|
||||||
completion.on("start", (id) => {
|
completion.on("start", (id) => {
|
||||||
if (jsonBody.stream) {
|
if (jsonBody.stream) {
|
||||||
// send message start
|
// send message start
|
||||||
res.write(
|
res.write(createEvent("message_start", {
|
||||||
createEvent("message_start", {
|
|
||||||
type: "message_start",
|
type: "message_start",
|
||||||
message: {
|
message: {
|
||||||
id: `${id}`,
|
id: `${id}`,
|
||||||
@@ -337,8 +340,7 @@ app.post("/v1/messages", AnthropicApiKeyAuth, (req, res) => {
|
|||||||
stop_sequence: null,
|
stop_sequence: null,
|
||||||
usage: {input_tokens: 8, output_tokens: 1},
|
usage: {input_tokens: 8, output_tokens: 1},
|
||||||
},
|
},
|
||||||
})
|
}));
|
||||||
);
|
|
||||||
res.write(createEvent("content_block_start", {
|
res.write(createEvent("content_block_start", {
|
||||||
type: "content_block_start",
|
type: "content_block_start",
|
||||||
index: 0,
|
index: 0,
|
||||||
@@ -351,37 +353,24 @@ app.post("/v1/messages", AnthropicApiKeyAuth, (req, res) => {
|
|||||||
completion.on("completion", (id, text) => {
|
completion.on("completion", (id, text) => {
|
||||||
if (jsonBody.stream) {
|
if (jsonBody.stream) {
|
||||||
// send message delta
|
// send message delta
|
||||||
res.write(
|
res.write(createEvent("content_block_delta", {
|
||||||
createEvent("content_block_delta", {
|
|
||||||
type: "content_block_delta",
|
type: "content_block_delta",
|
||||||
index: 0,
|
index: 0,
|
||||||
delta: {type: "text_delta", text: text},
|
delta: {type: "text_delta", text: text},
|
||||||
})
|
}));
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
// 只会发一次,发送final response
|
// 只会发一次,发送final response
|
||||||
res.write(
|
res.write(JSON.stringify({
|
||||||
JSON.stringify({
|
|
||||||
id: id,
|
id: id,
|
||||||
content: [
|
content: [
|
||||||
{
|
{text: text},
|
||||||
text: text,
|
{id: "string", name: "string", input: {}},
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "string",
|
|
||||||
name: "string",
|
|
||||||
input: {},
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
model: "string",
|
model: proxyModel,
|
||||||
stop_reason: "end_turn",
|
stop_reason: "end_turn",
|
||||||
stop_sequence: "string",
|
stop_sequence: null,
|
||||||
usage: {
|
usage: {input_tokens: 0, output_tokens: 0},
|
||||||
input_tokens: 0,
|
}));
|
||||||
output_tokens: 0,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
);
|
|
||||||
res.end();
|
res.end();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -389,13 +378,11 @@ app.post("/v1/messages", AnthropicApiKeyAuth, (req, res) => {
|
|||||||
completion.on("end", () => {
|
completion.on("end", () => {
|
||||||
if (jsonBody.stream) {
|
if (jsonBody.stream) {
|
||||||
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(createEvent("message_delta", {
|
||||||
createEvent("message_delta", {
|
|
||||||
type: "message_delta",
|
type: "message_delta",
|
||||||
delta: {stop_reason: "end_turn", stop_sequence: null},
|
delta: {stop_reason: "end_turn", stop_sequence: null},
|
||||||
usage: {output_tokens: 12},
|
usage: {output_tokens: 12},
|
||||||
})
|
}));
|
||||||
);
|
|
||||||
res.write(createEvent("message_stop", {type: "message_stop"}));
|
res.write(createEvent("message_stop", {type: "message_stop"}));
|
||||||
res.end();
|
res.end();
|
||||||
}
|
}
|
||||||
@@ -406,49 +393,51 @@ app.post("/v1/messages", AnthropicApiKeyAuth, (req, res) => {
|
|||||||
completion.removeAllListeners();
|
completion.removeAllListeners();
|
||||||
cancel();
|
cancel();
|
||||||
});
|
});
|
||||||
})
|
|
||||||
.catch((error) => {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
const errorMessage = "Error occurred, please check the log.\\n\\n出现错误,请检查日志:<pre>" + (error.stack || error) + "</pre>";
|
||||||
if (jsonBody.stream) {
|
if (jsonBody.stream) {
|
||||||
res.write(
|
res.write(createEvent("content_block_delta", {
|
||||||
createEvent("content_block_delta", {
|
|
||||||
type: "content_block_delta",
|
type: "content_block_delta",
|
||||||
index: 0,
|
index: 0,
|
||||||
delta: {
|
delta: {type: "text_delta", text: errorMessage},
|
||||||
type: "text_delta",
|
}));
|
||||||
text: "Error occurred, please check the log.\n\n出现错误,请检查日志:<pre>" + error.stack || error + "</pre>",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
);
|
|
||||||
res.end();
|
|
||||||
} else {
|
} else {
|
||||||
res.write(
|
res.write(JSON.stringify({
|
||||||
JSON.stringify({
|
|
||||||
id: uuidv4(),
|
id: uuidv4(),
|
||||||
content: [
|
content: [{text: errorMessage}, {id: "string", name: "string", input: {}}],
|
||||||
{
|
model: proxyModel,
|
||||||
text: "Error occurred, please check the log.\n\n出现错误,请检查日志:<pre>" + error.stack || error + "</pre>",
|
stop_reason: "error",
|
||||||
},
|
stop_sequence: null,
|
||||||
{
|
usage: {input_tokens: 0, output_tokens: 0},
|
||||||
id: "string",
|
}));
|
||||||
name: "string",
|
}
|
||||||
input: {},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
model: "string",
|
|
||||||
stop_reason: "end_turn",
|
|
||||||
stop_sequence: "string",
|
|
||||||
usage: {
|
|
||||||
input_tokens: 0,
|
|
||||||
output_tokens: 0,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
);
|
|
||||||
res.end();
|
res.end();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 辅助函数:规范化消息格式
|
||||||
|
function normalizeMessages(messages) {
|
||||||
|
return messages.map(message => {
|
||||||
|
if (typeof message.content === 'string') {
|
||||||
|
return message;
|
||||||
|
} else if (Array.isArray(message.content)) {
|
||||||
|
// 新版格式,提取文本内容
|
||||||
|
const textContent = message.content
|
||||||
|
.filter(item => item.type === 'text')
|
||||||
|
.map(item => item.text)
|
||||||
|
.join('\n');
|
||||||
|
return {...message, content: textContent};
|
||||||
|
} else {
|
||||||
|
// 未知格式,返回原始消息
|
||||||
|
console.warn('未知的消息格式:', message);
|
||||||
|
return message;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// handle other
|
// handle other
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user