数据格式标准化

This commit is contained in:
锦恢 2025-04-26 15:12:36 +08:00
parent dddb786aa4
commit cf3a3a57ce

View File

@ -0,0 +1,54 @@
import { ChatMessage, ToolMessage } from "./chat";
const OPENAI_SUPPORT_MEDIA = new Set(['text', 'image_url', 'video_url']);
function hasInvalidType(toolMessage: ToolMessage) {
for (const content of toolMessage.content) {
if (OPENAI_SUPPORT_MEDIA.has(content.type)) {
continue;
}
return true;
}
return false;
}
function normaliseToolMessage(rest: ToolMessage) {
if (!hasInvalidType(rest)) {
return rest;
}
const newRest = JSON.parse(JSON.stringify(rest));
// 过滤一下 userMessages现在的大部分模型只支持 text, image_url and video_url 这三种类型的数据
for (const content of newRest.content) {
if (OPENAI_SUPPORT_MEDIA.has(content.type)) {
continue;
}
}
return rest;
}
/**
* @description
*
* @param userMessages
*/
export async function normaliseChatMessage(userMessages: ChatMessage[]) {
const normalisedMessages = [];
for (const msg of userMessages) {
if (msg.role === 'tool') {
const normMessage = normaliseToolMessage(msg);
const { extraInfo, name, ...rest } = normMessage;
normalisedMessages.push(rest);
} else {
const { extraInfo, name, ...rest } = msg;
normalisedMessages.push(rest);
}
}
return normalisedMessages;
}