mirror of
https://github.com/LLM-Red-Team/kimi-free-api.git
synced 2024-11-01 20:09:20 +08:00
优化降低传文件上下文混淆问题
This commit is contained in:
parent
1e09d807e6
commit
6853087757
@ -362,12 +362,19 @@ function extractRefFileUrls(messages: any[]) {
|
|||||||
* @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
|
* @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
|
||||||
*/
|
*/
|
||||||
function messagesPrepare(messages: any[]) {
|
function messagesPrepare(messages: 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",如果有则注入消息
|
||||||
@ -375,17 +382,6 @@ function messagesPrepare(messages: any[]) {
|
|||||||
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) {
|
||||||
|
|
||||||
// 对 latestMessage.content 进行过滤,只保留"type": "text"的内容
|
|
||||||
// latestMessage.content = latestMessage.content.filter(v => typeof v === 'object' && v['type'] === 'text');
|
|
||||||
|
|
||||||
// 对 latestMessage.content 进行过滤,只保留不含base64的内容
|
|
||||||
latestMessage.content = latestMessage.content.filter(v => {
|
|
||||||
if (typeof v === 'object' && ['file', 'image_url'].includes(v['type'])) {
|
|
||||||
return !util.isBASE64Data(v['image_url']['url']);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
let newFileMessage = {
|
let newFileMessage = {
|
||||||
"content": "关注用户最新发送文件和消息结尾",
|
"content": "关注用户最新发送文件和消息结尾",
|
||||||
"role": "system"
|
"role": "system"
|
||||||
@ -406,13 +402,13 @@ function messagesPrepare(messages: any[]) {
|
|||||||
return message.content.reduce((_content, v) => {
|
return message.content.reduce((_content, v) => {
|
||||||
if (!_.isObject(v) || v['type'] != 'text')
|
if (!_.isObject(v) || v['type'] != 'text')
|
||||||
return _content;
|
return _content;
|
||||||
return _content + (v['text'] || '');
|
return _content + ('user:' + v['text'] || '') + "\n";
|
||||||
}, content);
|
}, content);
|
||||||
}
|
}
|
||||||
return content += `${message.role || 'user'}:${wrapUrlsToTags(message.content)}\n`;
|
return content += `${message.role || 'user'}:${wrapUrlsToTags(message.content)}\n`;
|
||||||
}, '');
|
}, '');
|
||||||
|
|
||||||
logger.info("本次上传消息:" + { role: 'user', content });
|
logger.info("上传消息:" + content);
|
||||||
return [
|
return [
|
||||||
{ role: 'user', content }
|
{ role: 'user', content }
|
||||||
]
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user