fix 上下文上传文件聊天不连贯问题

This commit is contained in:
Yanyutin753 2024-04-02 23:23:58 +08:00
parent f453c075e2
commit 083014b899

View File

@ -487,25 +487,39 @@ function extractRefFileUrls(messages: any[]) {
* @param messages gpt系列消息格式 * @param messages gpt系列消息格式
*/ */
function messagesPrepare(messages: any[], refs: any[]) { function messagesPrepare(messages: any[], refs: any[]) {
// 只保留最新消息以及不包含"type": "image_url"或"type": "file"的消息 // 先剔除所有的 base64 数据
let validMessages = messages.filter((message, index) => { let validMessages = messages.map((message) => {
if (index === messages.length - 1) return true; if (Array.isArray(message.content)) {
if (!Array.isArray(message.content)) return true; message.content = message.content.filter(v => {
// 不含"type": "image_url"或"type": "file"的消息保留 if (typeof v === 'object' && ['file', 'image_url'].includes(v['type'])) {
return !message.content.some(v => (typeof v === 'object' && ['file', 'image_url'].includes(v['type']))); // 如果内容是 base64 数据,就剔除
return !util.isBASE64Data(v['url']);
}
// 如果不是 base64 数据,就保留
return true;
});
}
return message;
}); });
// 检查最新消息是否含有"type": "image_url"或"type": "file",如果有则注入消息 // 检查最新消息是否含有"type": "image_url"或"type": "file",如果有则注入消息
let latestMessage = validMessages[validMessages.length - 1]; let latestMessage = validMessages[validMessages.length - 1];
let hasFileOrImage = Array.isArray(latestMessage.content) let hasFileOrImage = Array.isArray(latestMessage.content)
&& latestMessage.content.some(v => (typeof v === 'object' && ['file', 'image_url'].includes(v['type']))); && latestMessage.content.some(v => (typeof v === 'object' && ['file', 'image_url'].includes(v['type'])));
if (hasFileOrImage) { if (hasFileOrImage) {
// newMessage是一个用于指示文件的消息是的模型能正常回答出相应的问题 let newFileMessage = {
let newMessage = { "content": "关注用户最新发送文件和消息结尾",
"content": "持续关注用户最新发送的文件和消息的结尾",
"role": "system" "role": "system"
}; };
validMessages.splice(validMessages.length - 1, 0, newMessage); validMessages.splice(validMessages.length - 1, 0, newFileMessage);
logger.info("检查注入文件消息");
} else {
let newTextMessage = {
"content": "关注用户消息的结尾",
"role": "system"
};
validMessages.splice(validMessages.length - 1, 0, newTextMessage);
logger.info("检查注入文本消息");
} }
const content = const content =
@ -514,7 +528,7 @@ function messagesPrepare(messages: any[], refs: any[]) {
return ( return (
message.content.reduce((_content, v) => { message.content.reduce((_content, v) => {
if (!_.isObject(v) || v["type"] != "text") return _content; if (!_.isObject(v) || v["type"] != "text") return _content;
return _content + (v["text"] || ""); return _content + ('user:' + v["text"] || "") + "\n";
}, content) + "\n" }, content) + "\n"
); );
} }