forked from XiaoMo/ChatGPT-Next-Web
fix: #1771 should not lose chat context when sumindex > n - count
This commit is contained in:
parent
0a2cccfd6e
commit
db63a5a670
@ -370,28 +370,30 @@ export const useChatStore = create<ChatStore>()(
|
|||||||
context.push(memoryPrompt);
|
context.push(memoryPrompt);
|
||||||
}
|
}
|
||||||
|
|
||||||
// get short term and unmemoried long term memory
|
// get short term and unmemorized long term memory
|
||||||
const shortTermMemoryMessageIndex = Math.max(
|
const shortTermMemoryMessageIndex = Math.max(
|
||||||
0,
|
0,
|
||||||
n - modelConfig.historyMessageCount,
|
n - modelConfig.historyMessageCount,
|
||||||
);
|
);
|
||||||
const longTermMemoryMessageIndex = session.lastSummarizeIndex;
|
const longTermMemoryMessageIndex = session.lastSummarizeIndex;
|
||||||
const mostRecentIndex = Math.max(
|
|
||||||
|
// try to concat history messages
|
||||||
|
const memoryStartIndex = Math.min(
|
||||||
shortTermMemoryMessageIndex,
|
shortTermMemoryMessageIndex,
|
||||||
longTermMemoryMessageIndex,
|
longTermMemoryMessageIndex,
|
||||||
);
|
);
|
||||||
const threshold = modelConfig.compressMessageLengthThreshold * 2;
|
const threshold = modelConfig.max_tokens;
|
||||||
|
|
||||||
// get recent messages as many as possible
|
// get recent messages as many as possible
|
||||||
const reversedRecentMessages = [];
|
const reversedRecentMessages = [];
|
||||||
for (
|
for (
|
||||||
let i = n - 1, count = 0;
|
let i = n - 1, count = 0;
|
||||||
i >= mostRecentIndex && count < threshold;
|
i >= memoryStartIndex && count < threshold;
|
||||||
i -= 1
|
i -= 1
|
||||||
) {
|
) {
|
||||||
const msg = messages[i];
|
const msg = messages[i];
|
||||||
if (!msg || msg.isError) continue;
|
if (!msg || msg.isError) continue;
|
||||||
count += msg.content.length;
|
count += estimateTokenLength(msg.content);
|
||||||
reversedRecentMessages.push(msg);
|
reversedRecentMessages.push(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user