forked from XiaoMo/ChatGPT-Next-Web
Merge pull request #892 from Yidadaa/improve-memory
feat: close #864 improve long term history
This commit is contained in:
commit
9e46ca31dd
@ -38,12 +38,12 @@ const cn = {
|
|||||||
MessageFromChatGPT: "来自 ChatGPT 的消息",
|
MessageFromChatGPT: "来自 ChatGPT 的消息",
|
||||||
},
|
},
|
||||||
Memory: {
|
Memory: {
|
||||||
Title: "历史记忆",
|
Title: "历史摘要",
|
||||||
EmptyContent: "尚未记忆",
|
EmptyContent: "尚未总结",
|
||||||
Send: "发送记忆",
|
Send: "启用总结并发送摘要",
|
||||||
Copy: "复制记忆",
|
Copy: "复制摘要",
|
||||||
Reset: "重置对话",
|
Reset: "重置对话",
|
||||||
ResetConfirm: "重置后将清空当前对话记录以及历史记忆,确认重置?",
|
ResetConfirm: "重置后将清空当前对话记录以及历史摘要,确认重置?",
|
||||||
},
|
},
|
||||||
Home: {
|
Home: {
|
||||||
NewChat: "新的聊天",
|
NewChat: "新的聊天",
|
||||||
|
@ -462,6 +462,7 @@ export const useChatStore = create<ChatStore>()(
|
|||||||
|
|
||||||
const context = session.context.slice();
|
const context = session.context.slice();
|
||||||
|
|
||||||
|
// long term memory
|
||||||
if (
|
if (
|
||||||
session.sendMemory &&
|
session.sendMemory &&
|
||||||
session.memoryPrompt &&
|
session.memoryPrompt &&
|
||||||
@ -471,9 +472,33 @@ export const useChatStore = create<ChatStore>()(
|
|||||||
context.push(memoryPrompt);
|
context.push(memoryPrompt);
|
||||||
}
|
}
|
||||||
|
|
||||||
const recentMessages = context.concat(
|
// get short term and unmemoried long term memory
|
||||||
messages.slice(Math.max(0, n - config.historyMessageCount)),
|
const shortTermMemoryMessageIndex = Math.max(
|
||||||
|
0,
|
||||||
|
n - config.historyMessageCount,
|
||||||
);
|
);
|
||||||
|
const longTermMemoryMessageIndex = session.lastSummarizeIndex;
|
||||||
|
const oldestIndex = Math.min(
|
||||||
|
shortTermMemoryMessageIndex,
|
||||||
|
longTermMemoryMessageIndex,
|
||||||
|
);
|
||||||
|
const threshold = config.compressMessageLengthThreshold;
|
||||||
|
|
||||||
|
// get recent messages as many as possible
|
||||||
|
const reversedRecentMessages = [];
|
||||||
|
for (
|
||||||
|
let i = n - 1, count = 0;
|
||||||
|
i >= oldestIndex && count < threshold;
|
||||||
|
i -= 1
|
||||||
|
) {
|
||||||
|
const msg = messages[i];
|
||||||
|
if (!msg || msg.isError) continue;
|
||||||
|
count += msg.content.length;
|
||||||
|
reversedRecentMessages.push(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
// concat
|
||||||
|
const recentMessages = context.concat(reversedRecentMessages.reverse());
|
||||||
|
|
||||||
return recentMessages;
|
return recentMessages;
|
||||||
},
|
},
|
||||||
@ -542,7 +567,10 @@ export const useChatStore = create<ChatStore>()(
|
|||||||
config.compressMessageLengthThreshold,
|
config.compressMessageLengthThreshold,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (historyMsgLength > config.compressMessageLengthThreshold) {
|
if (
|
||||||
|
historyMsgLength > config.compressMessageLengthThreshold &&
|
||||||
|
session.sendMemory
|
||||||
|
) {
|
||||||
requestChatStream(
|
requestChatStream(
|
||||||
toBeSummarizedMsgs.concat({
|
toBeSummarizedMsgs.concat({
|
||||||
role: "system",
|
role: "system",
|
||||||
|
Loading…
Reference in New Issue
Block a user