Browse Source

Merge pull request #892 from Yidadaa/improve-memory

feat: close #864 improve long term history
Yifei Zhang 1 year ago
parent
commit
9e46ca31dd
2 changed files with 36 additions and 8 deletions
  1. 5 5
      app/locales/cn.ts
  2. 31 3
      app/store/app.ts

+ 5 - 5
app/locales/cn.ts

@@ -38,12 +38,12 @@ const cn = {
     MessageFromChatGPT: "来自 ChatGPT 的消息",
   },
   Memory: {
-    Title: "历史记忆",
-    EmptyContent: "尚未记忆",
-    Send: "发送记忆",
-    Copy: "复制记忆",
+    Title: "历史摘要",
+    EmptyContent: "尚未总结",
+    Send: "启用总结并发送摘要",
+    Copy: "复制摘要",
     Reset: "重置对话",
-    ResetConfirm: "重置后将清空当前对话记录以及历史记忆,确认重置?",
+    ResetConfirm: "重置后将清空当前对话记录以及历史摘要,确认重置?",
   },
   Home: {
     NewChat: "新的聊天",

+ 31 - 3
app/store/app.ts

@@ -462,6 +462,7 @@ export const useChatStore = create<ChatStore>()(
 
         const context = session.context.slice();
 
+        // long term memory
         if (
           session.sendMemory &&
           session.memoryPrompt &&
@@ -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;
       },
@@ -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",