From db63a5a67020e399f16b47a3e541506df645ec3f Mon Sep 17 00:00:00 2001 From: Yidadaa Date: Sun, 18 Jun 2023 00:51:16 +0800 Subject: [PATCH] fix: #1771 should not lose chat context when sumindex > n - count --- app/store/chat.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/app/store/chat.ts b/app/store/chat.ts index e1bd2eb4..629eeab9 100644 --- a/app/store/chat.ts +++ b/app/store/chat.ts @@ -370,28 +370,30 @@ export const useChatStore = create()( context.push(memoryPrompt); } - // get short term and unmemoried long term memory + // get short term and unmemorized long term memory const shortTermMemoryMessageIndex = Math.max( 0, n - modelConfig.historyMessageCount, ); const longTermMemoryMessageIndex = session.lastSummarizeIndex; - const mostRecentIndex = Math.max( + + // try to concat history messages + const memoryStartIndex = Math.min( shortTermMemoryMessageIndex, longTermMemoryMessageIndex, ); - const threshold = modelConfig.compressMessageLengthThreshold * 2; + const threshold = modelConfig.max_tokens; // get recent messages as many as possible const reversedRecentMessages = []; for ( let i = n - 1, count = 0; - i >= mostRecentIndex && count < threshold; + i >= memoryStartIndex && count < threshold; i -= 1 ) { const msg = messages[i]; if (!msg || msg.isError) continue; - count += msg.content.length; + count += estimateTokenLength(msg.content); reversedRecentMessages.push(msg); }