From 6284628d9973bf45494e3f80fd6fa8279ad362ce Mon Sep 17 00:00:00 2001 From: Vinlic Date: Sat, 9 Mar 2024 16:48:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=9A=E8=BD=AE=E5=AF=B9?= =?UTF-8?q?=E8=AF=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/controllers/chat.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/api/controllers/chat.ts b/src/api/controllers/chat.ts index 53ba25d..5246c3f 100644 --- a/src/api/controllers/chat.ts +++ b/src/api/controllers/chat.ts @@ -85,7 +85,7 @@ async function createCompletion(messages: any[], refreshToken: string, useSearch const convId = await createConversation(`cmpl-${util.uuid(false)}`, refreshToken); const token = await acquireToken(refreshToken); const result = await axios.post(`https://kimi.moonshot.cn/api/chat/${convId}/completion/stream`, { - messages, + messages: messagesPrepare(messages), use_search: useSearch }, { headers: { @@ -107,7 +107,7 @@ async function createCompletionStream(messages: any[], refreshToken: string, use const convId = await createConversation(`cmpl-${util.uuid(false)}`, refreshToken); const token = await acquireToken(refreshToken); const result = await axios.post(`https://kimi.moonshot.cn/api/chat/${convId}/completion/stream`, { - messages, + messages: messagesPrepare(messages), use_search: useSearch }, { headers: { @@ -124,6 +124,15 @@ async function createCompletionStream(messages: any[], refreshToken: string, use }); } +function messagesPrepare(messages: any[]) { + const content = messages.reduce((content, message) => { + return content += `${message.role || 'user'}:${message.content}\n`; + }, ''); + return [ + { role: 'user', content } + ] +} + function checkResult(result: AxiosResponse, refreshToken: string) { if(result.status == 401) { accessTokenMap.delete(refreshToken);