From 66067b4dd91dcb7ae1044e4de0320f179fe0e4d2 Mon Sep 17 00:00:00 2001 From: Yanyutin753 <132346501+Yanyutin753@users.noreply.github.com> Date: Tue, 2 Apr 2024 20:54:46 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=B7=BB=E5=8A=A0prompt?= =?UTF-8?q?=E6=94=B9=E5=96=84=E4=BC=A0=E6=96=87=E4=BB=B6=E6=97=B6=E7=9A=84?= =?UTF-8?q?=E4=B8=8A=E4=B8=8B=E6=96=87=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/controllers/chat.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/api/controllers/chat.ts b/src/api/controllers/chat.ts index 309f6bf..c06c461 100644 --- a/src/api/controllers/chat.ts +++ b/src/api/controllers/chat.ts @@ -347,6 +347,7 @@ function extractRefFileUrls(messages: any[]) { urls.push(v['image_url']['url']); }); } + logger.info("本次请求上传:" + urls.length + "个文件"); return urls; } @@ -369,6 +370,37 @@ function messagesPrepare(messages: any[]) { return !message.content.some(v => (typeof v === 'object' && ['file', 'image_url'].includes(v['type']))); }); + // 检查最新消息是否含有"type": "image_url"或"type": "file",如果有则注入消息 + let latestMessage = validMessages[validMessages.length - 1]; + let hasFileOrImage = Array.isArray(latestMessage.content) + && latestMessage.content.some(v => (typeof v === 'object' && ['file', 'image_url'].includes(v['type']))); + 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 = { + "content": "关注用户最新发送文件和消息结尾", + "role": "system" + }; + 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 = validMessages.reduce((content, message) => { if (Array.isArray(message.content)) { return message.content.reduce((_content, v) => { From 1e09d807e639b22b141c83d6dad201e457cf5649 Mon Sep 17 00:00:00 2001 From: Yanyutin753 <132346501+Yanyutin753@users.noreply.github.com> Date: Tue, 2 Apr 2024 21:15:36 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=89=93=E5=8D=B0=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/controllers/chat.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/api/controllers/chat.ts b/src/api/controllers/chat.ts index c06c461..bc53064 100644 --- a/src/api/controllers/chat.ts +++ b/src/api/controllers/chat.ts @@ -411,6 +411,8 @@ function messagesPrepare(messages: any[]) { } return content += `${message.role || 'user'}:${wrapUrlsToTags(message.content)}\n`; }, ''); + + logger.info("本次上传消息:" + { role: 'user', content }); return [ { role: 'user', content } ] From 6853087757c33c33ecfd5588f5d30db4a0c9fc04 Mon Sep 17 00:00:00 2001 From: Yanyutin753 <132346501+Yanyutin753@users.noreply.github.com> Date: Tue, 2 Apr 2024 23:13:00 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=99=8D=E4=BD=8E?= =?UTF-8?q?=E4=BC=A0=E6=96=87=E4=BB=B6=E4=B8=8A=E4=B8=8B=E6=96=87=E6=B7=B7?= =?UTF-8?q?=E6=B7=86=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/controllers/chat.ts | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/src/api/controllers/chat.ts b/src/api/controllers/chat.ts index bc53064..44045fb 100644 --- a/src/api/controllers/chat.ts +++ b/src/api/controllers/chat.ts @@ -362,12 +362,19 @@ function extractRefFileUrls(messages: any[]) { * @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文 */ function messagesPrepare(messages: any[]) { - // 只保留最新消息以及不包含"type": "image_url"或"type": "file"的消息 - let validMessages = messages.filter((message, index) => { - if (index === messages.length - 1) return true; - if (!Array.isArray(message.content)) return true; - // 不含"type": "image_url"或"type": "file"的消息保留 - return !message.content.some(v => (typeof v === 'object' && ['file', 'image_url'].includes(v['type']))); + // 先剔除所有的 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",如果有则注入消息 @@ -375,17 +382,6 @@ function messagesPrepare(messages: any[]) { let hasFileOrImage = Array.isArray(latestMessage.content) && latestMessage.content.some(v => (typeof v === 'object' && ['file', 'image_url'].includes(v['type']))); 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 = { "content": "关注用户最新发送文件和消息结尾", "role": "system" @@ -406,13 +402,13 @@ function messagesPrepare(messages: any[]) { return message.content.reduce((_content, v) => { if (!_.isObject(v) || v['type'] != 'text') return _content; - return _content + (v['text'] || ''); + return _content + ('user:' + v['text'] || '') + "\n"; }, content); } return content += `${message.role || 'user'}:${wrapUrlsToTags(message.content)}\n`; }, ''); - logger.info("本次上传消息:" + { role: 'user', content }); + logger.info("上传消息:" + content); return [ { role: 'user', content } ]