Skip to content

Commit

Permalink
Merge pull request ChatGPTNextWeb#892 from Yidadaa/improve-memory
Browse files Browse the repository at this point in the history
feat: close ChatGPTNextWeb#864 improve long term history
  • Loading branch information
Yidadaa authored Apr 18, 2023
2 parents 6b30e2b + ad1c8ff commit 9e46ca3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
10 changes: 5 additions & 5 deletions app/locales/cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ const cn = {
MessageFromChatGPT: "来自 ChatGPT 的消息",
},
Memory: {
Title: "历史记忆",
EmptyContent: "尚未记忆",
Send: "发送记忆",
Copy: "复制记忆",
Title: "历史摘要",
EmptyContent: "尚未总结",
Send: "启用总结并发送摘要",
Copy: "复制摘要",
Reset: "重置对话",
ResetConfirm: "重置后将清空当前对话记录以及历史记忆,确认重置?",
ResetConfirm: "重置后将清空当前对话记录以及历史摘要,确认重置?",
},
Home: {
NewChat: "新的聊天",
Expand Down
34 changes: 31 additions & 3 deletions app/store/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ export const useChatStore = create<ChatStore>()(

const context = session.context.slice();

// long term memory
if (
session.sendMemory &&
session.memoryPrompt &&
Expand All @@ -471,9 +472,33 @@ export const useChatStore = create<ChatStore>()(
context.push(memoryPrompt);
}

const recentMessages = context.concat(
messages.slice(Math.max(0, n - config.historyMessageCount)),
// get short term and unmemoried long term memory
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;
},
Expand Down Expand Up @@ -542,7 +567,10 @@ export const useChatStore = create<ChatStore>()(
config.compressMessageLengthThreshold,
);

if (historyMsgLength > config.compressMessageLengthThreshold) {
if (
historyMsgLength > config.compressMessageLengthThreshold &&
session.sendMemory
) {
requestChatStream(
toBeSummarizedMsgs.concat({
role: "system",
Expand Down

0 comments on commit 9e46ca3

Please sign in to comment.