From 01ff5c250a73aec6b35703b02ea55c5cbc9cd683 Mon Sep 17 00:00:00 2001 From: Vinlic Date: Sun, 28 Apr 2024 17:54:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E9=A6=96=E8=BD=AE=E4=BC=A0?= =?UTF-8?q?=E9=80=81=E6=96=87=E4=BB=B6=E6=97=B6=E5=AF=BC=E8=87=B4=E5=AF=B9?= =?UTF-8?q?=E8=AF=9D=E5=90=88=E5=B9=B6=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 | 46 +++++++++++++++---------------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/src/api/controllers/chat.ts b/src/api/controllers/chat.ts index 7d889ed..e82acec 100644 --- a/src/api/controllers/chat.ts +++ b/src/api/controllers/chat.ts @@ -261,11 +261,6 @@ async function createCompletion(model = MODEL_NAME, messages: any[], refreshToke userId } = await acquireToken(refreshToken); const sendMessages = messagesPrepare(messages, !!refConvId); - console.log(convId, { - messages: sendMessages, - refs, - use_search: useSearch - }); const result = await axios.post(`https://kimi.moonshot.cn/api/chat/${convId}/completion/stream`, { messages: sendMessages, refs, @@ -461,12 +456,25 @@ function extractRefFileUrls(messages: any[]) { * @param isRefConv 是否为引用会话 */ function messagesPrepare(messages: any[], isRefConv = false) { - // 注入消息提升注意力 - 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']))); - // 第二轮开始注入system prompt - if (messages.length > 2) { + let content; + if (isRefConv || messages.length < 2) { + content = messages.reduce((content, message) => { + if (_.isArray(message.content)) { + return message.content.reduce((_content, v) => { + if (!_.isObject(v) || v['type'] != 'text') return _content; + return _content + `${v["text"] || ""}\n`; + }, content); + } + return content += `${message.role == 'user' ? wrapUrlsToTags(message.content) : message.content}\n`; + }, '') + logger.info("\n透传内容:\n" + content); + } + else { + // 注入消息提升注意力 + 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']))); + // 第二轮开始注入system prompt if (hasFileOrImage) { let newFileMessage = { "content": "关注用户最新发送文件和消息", @@ -482,22 +490,6 @@ function messagesPrepare(messages: any[], isRefConv = false) { messages.splice(messages.length - 1, 0, newTextMessage); logger.info("注入提升尾部消息注意力system prompt"); } - } - - let content; - if (isRefConv || messages.length < 2) { - content = messages.reduce((content, message) => { - if (_.isArray(message.content)) { - return message.content.reduce((_content, v) => { - if (!_.isObject(v) || v['type'] != 'text') return _content; - return _content + `${v["text"] || ""}\n`; - }, content); - } - return content += `${message.role == 'user' ? wrapUrlsToTags(message.content) : message.content}\n`; - }, '') - logger.info("\n透传内容:\n" + content); - } - else { content = messages.reduce((content, message) => { if (_.isArray(message.content)) { return message.content.reduce((_content, v) => {