From 70ea39591b9f96e499a9d396e0a8b0ea47de39c7 Mon Sep 17 00:00:00 2001 From: Yanyutin753 <132346501+Yanyutin753@users.noreply.github.com> Date: Fri, 5 Apr 2024 18:54:04 +0800 Subject: [PATCH 1/2] optimize code in messagesPrepare --- src/api/controllers/chat.ts | 34 +++++++++------------------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/src/api/controllers/chat.ts b/src/api/controllers/chat.ts index c13f2fb..88ddd1f 100644 --- a/src/api/controllers/chat.ts +++ b/src/api/controllers/chat.ts @@ -362,53 +362,37 @@ function extractRefFileUrls(messages: any[]) { * @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文 */ function messagesPrepare(messages: any[]) { - // 先剔除所有的 base64 数据 - let validMessages = messages.map((message) => { - if (Array.isArray(message.content)) { - message.content = message.content.filter(v => { - if (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",如果有则注入消息 - let latestMessage = validMessages[validMessages.length - 1]; + // 注入消息提升注意力 + let latestMessage = messages[messages.length - 1]; let hasFileOrImage = Array.isArray(latestMessage.content) && latestMessage.content.some(v => (typeof v === 'object' && ['file', 'image_url'].includes(v['type']))); if (hasFileOrImage) { let newFileMessage = { - "content": "关注用户最新发送的文件和消息", + "content": "关注用户最新发送文件和消息", "role": "system" }; - validMessages.splice(validMessages.length - 1, 0, newFileMessage); + messages.splice(messages.length - 1, 0, newFileMessage); logger.info("注入提升尾部文件注意力system prompt"); } else { let newTextMessage = { "content": "关注用户最新的消息", "role": "system" }; - validMessages.splice(validMessages.length - 1, 0, newTextMessage); + messages.splice(messages.length - 1, 0, newTextMessage); logger.info("注入提升尾部消息注意力system prompt"); } - const content = validMessages.reduce((content, message) => { + const content = messages.reduce((content, message) => { if (Array.isArray(message.content)) { return message.content.reduce((_content, v) => { - if (!_.isObject(v) || v['type'] != 'text') - return _content; - return _content + ('user:' + v['text'] || '') + "\n"; + if (!_.isObject(v) || v['type'] != 'text') return _content; + return _content + `\n${message.role || "user"}:${v["text"] || ""}`; }, content); } return content += `${message.role || 'user'}:${wrapUrlsToTags(message.content)}\n`; }, ''); - logger.info("\n对话合并:\n" + content); + logger.info("\n对话合并:" + content); return [ { role: 'user', content } ] From cda36ed4fc0fbbad252238df6a5c060030d7ed44 Mon Sep 17 00:00:00 2001 From: Yanyutin753 <132346501+Yanyutin753@users.noreply.github.com> Date: Fri, 5 Apr 2024 19:12:47 +0800 Subject: [PATCH 2/2] fix the position of "\n" --- src/api/controllers/chat.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/controllers/chat.ts b/src/api/controllers/chat.ts index 88ddd1f..9c37aaf 100644 --- a/src/api/controllers/chat.ts +++ b/src/api/controllers/chat.ts @@ -389,7 +389,7 @@ function messagesPrepare(messages: any[]) { return _content + `\n${message.role || "user"}:${v["text"] || ""}`; }, content); } - return content += `${message.role || 'user'}:${wrapUrlsToTags(message.content)}\n`; + return content += `\n${message.role || 'user'}:${wrapUrlsToTags(message.content)}`; }, ''); logger.info("\n对话合并:" + content);