From e68207be2cd9faa72ead10cf1f512dc357ac6f05 Mon Sep 17 00:00:00 2001
From: L1468999760 <1468999760@qq.com>
Date: Tue, 18 Apr 2023 11:34:33 +0800
Subject: [PATCH 1/4] fix:delete user.svg
---
app/icons/user.svg | 34 ----------------------------------
1 file changed, 34 deletions(-)
delete mode 100644 app/icons/user.svg
diff --git a/app/icons/user.svg b/app/icons/user.svg
deleted file mode 100644
index 7f91de4dc..000000000
--- a/app/icons/user.svg
+++ /dev/null
@@ -1,34 +0,0 @@
-
\ No newline at end of file
From 53173d90534616ea30aacd163033b12b8586b816 Mon Sep 17 00:00:00 2001
From: jtung4 <126780548+jtung4@users.noreply.github.com>
Date: Tue, 18 Apr 2023 11:38:26 +0800
Subject: [PATCH 2/4] Optimize topic and summary prompts in tw.ts
---
app/locales/tw.ts | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/app/locales/tw.ts b/app/locales/tw.ts
index 2fbb2e477..8d79e74fa 100644
--- a/app/locales/tw.ts
+++ b/app/locales/tw.ts
@@ -160,9 +160,9 @@ const tw: LocaleType = {
History: (content: string) =>
"這是 AI 與用戶的歷史聊天總結,作為前情提要:" + content,
Topic:
- "Summarise the conversation in a short and concise eye-catching title that instantly conveys the main topic. Use as few words as possible. Use the language used in the enquiry, e.g. use English for English enquiry, use zh-hant for traditional chinese enquiry. Don't use quotation marks at the beginning and the end.",
+ "Use the language used by the user (e.g. en for english conversation, zh-hant for chinese conversation, etc.) to generate a title (at most 6 words) summarizing our conversation without any lead-in, quotation marks, preamble like 'Title:', direct text copies, single-word replies, quotation marks, translations, or brackets. Remove enclosing quotation marks. The title should make third-party grasp the essence of the conversation in first sight.",
Summarize:
- "Summarise the conversation in at most 250 tokens for continuing the conversation in future. Use the language used in the conversation, e.g. use English for English conversation, use zh-hant for traditional chinese conversation.",
+ "Use the language used by the user (e.g. en-us for english conversation, zh-hant for chinese conversation, etc.) to summarise the conversation in at most 200 words. The summary will be used as prompt for you to continue the conversation in the future.",
},
ConfirmClearAll: "確認清除所有對話、設定數據?",
},
From d75b7d49b83362583a09884654bbbcd81f0f08ce Mon Sep 17 00:00:00 2001
From: Yifei Zhang
Date: Tue, 18 Apr 2023 11:42:08 +0800
Subject: [PATCH 3/4] feat: close #864 improve long term history
---
app/locales/cn.ts | 10 ++++----
app/store/app.ts | 64 ++++++++++++++++++++++++++++++++++-------------
2 files changed, 51 insertions(+), 23 deletions(-)
diff --git a/app/locales/cn.ts b/app/locales/cn.ts
index d0ab27ca2..7d011c7f1 100644
--- a/app/locales/cn.ts
+++ b/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: "新的聊天",
diff --git a/app/store/app.ts b/app/store/app.ts
index 39df9ef79..b3dfbd687 100644
--- a/app/store/app.ts
+++ b/app/store/app.ts
@@ -102,7 +102,7 @@ export function limitNumber(
x: number,
min: number,
max: number,
- defaultValue: number,
+ defaultValue: number
) {
if (typeof x !== "number" || isNaN(x)) {
return defaultValue;
@@ -217,7 +217,7 @@ interface ChatStore {
updateMessage: (
sessionIndex: number,
messageIndex: number,
- updater: (message?: Message) => void,
+ updater: (message?: Message) => void
) => void;
resetSession: () => void;
getMessagesWithMemory: () => Message[];
@@ -345,12 +345,12 @@ export const useChatStore = create()(
.slice(0, index)
.concat([deletedSession])
.concat(
- state.sessions.slice(index + Number(isLastSession)),
+ state.sessions.slice(index + Number(isLastSession))
),
}));
},
},
- 5000,
+ 5000
);
}
},
@@ -412,7 +412,7 @@ export const useChatStore = create()(
get().onNewMessage(botMessage);
ControllerPool.remove(
sessionIndex,
- botMessage.id ?? messageIndex,
+ botMessage.id ?? messageIndex
);
} else {
botMessage.content = content;
@@ -436,7 +436,7 @@ export const useChatStore = create()(
ControllerPool.addController(
sessionIndex,
botMessage.id ?? messageIndex,
- controller,
+ controller
);
},
filterBot: !get().config.sendBotMessages,
@@ -462,6 +462,7 @@ export const useChatStore = create()(
const context = session.context.slice();
+ // long term memory
if (
session.sendMemory &&
session.memoryPrompt &&
@@ -471,9 +472,33 @@ export const useChatStore = create()(
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 = config.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;
},
@@ -481,7 +506,7 @@ export const useChatStore = create()(
updateMessage(
sessionIndex: number,
messageIndex: number,
- updater: (message?: Message) => void,
+ updater: (message?: Message) => void
) {
const sessions = get().sessions;
const session = sessions.at(sessionIndex);
@@ -510,15 +535,15 @@ export const useChatStore = create()(
(res) => {
get().updateCurrentSession(
(session) =>
- (session.topic = res ? trimTopic(res) : DEFAULT_TOPIC),
+ (session.topic = res ? trimTopic(res) : DEFAULT_TOPIC)
);
- },
+ }
);
}
const config = get().config;
let toBeSummarizedMsgs = session.messages.slice(
- session.lastSummarizeIndex,
+ session.lastSummarizeIndex
);
const historyMsgLength = countMessages(toBeSummarizedMsgs);
@@ -526,7 +551,7 @@ export const useChatStore = create()(
if (historyMsgLength > get().config?.modelConfig?.max_tokens ?? 4000) {
const n = toBeSummarizedMsgs.length;
toBeSummarizedMsgs = toBeSummarizedMsgs.slice(
- Math.max(0, n - config.historyMessageCount),
+ Math.max(0, n - config.historyMessageCount)
);
}
@@ -539,10 +564,13 @@ export const useChatStore = create()(
"[Chat History] ",
toBeSummarizedMsgs,
historyMsgLength,
- config.compressMessageLengthThreshold,
+ config.compressMessageLengthThreshold
);
- if (historyMsgLength > config.compressMessageLengthThreshold) {
+ if (
+ historyMsgLength > config.compressMessageLengthThreshold &&
+ session.sendMemory
+ ) {
requestChatStream(
toBeSummarizedMsgs.concat({
role: "system",
@@ -561,7 +589,7 @@ export const useChatStore = create()(
onError(error) {
console.error("[Summarize] ", error);
},
- },
+ }
);
}
},
@@ -603,6 +631,6 @@ export const useChatStore = create()(
return state;
},
- },
- ),
+ }
+ )
);
From ad1c8ffe21f16d86367101e5ecfaca66860f7254 Mon Sep 17 00:00:00 2001
From: Yidadaa
Date: Tue, 18 Apr 2023 11:44:15 +0800
Subject: [PATCH 4/4] fixup
---
app/store/app.ts | 36 ++++++++++++++++++------------------
1 file changed, 18 insertions(+), 18 deletions(-)
diff --git a/app/store/app.ts b/app/store/app.ts
index b3dfbd687..da0e886ff 100644
--- a/app/store/app.ts
+++ b/app/store/app.ts
@@ -102,7 +102,7 @@ export function limitNumber(
x: number,
min: number,
max: number,
- defaultValue: number
+ defaultValue: number,
) {
if (typeof x !== "number" || isNaN(x)) {
return defaultValue;
@@ -217,7 +217,7 @@ interface ChatStore {
updateMessage: (
sessionIndex: number,
messageIndex: number,
- updater: (message?: Message) => void
+ updater: (message?: Message) => void,
) => void;
resetSession: () => void;
getMessagesWithMemory: () => Message[];
@@ -345,12 +345,12 @@ export const useChatStore = create()(
.slice(0, index)
.concat([deletedSession])
.concat(
- state.sessions.slice(index + Number(isLastSession))
+ state.sessions.slice(index + Number(isLastSession)),
),
}));
},
},
- 5000
+ 5000,
);
}
},
@@ -412,7 +412,7 @@ export const useChatStore = create()(
get().onNewMessage(botMessage);
ControllerPool.remove(
sessionIndex,
- botMessage.id ?? messageIndex
+ botMessage.id ?? messageIndex,
);
} else {
botMessage.content = content;
@@ -436,7 +436,7 @@ export const useChatStore = create()(
ControllerPool.addController(
sessionIndex,
botMessage.id ?? messageIndex,
- controller
+ controller,
);
},
filterBot: !get().config.sendBotMessages,
@@ -475,12 +475,12 @@ export const useChatStore = create()(
// get short term and unmemoried long term memory
const shortTermMemoryMessageIndex = Math.max(
0,
- n - config.historyMessageCount
+ n - config.historyMessageCount,
);
- const longTermMemoryMessageIndex = config.lastSummarizeIndex;
+ const longTermMemoryMessageIndex = session.lastSummarizeIndex;
const oldestIndex = Math.min(
shortTermMemoryMessageIndex,
- longTermMemoryMessageIndex
+ longTermMemoryMessageIndex,
);
const threshold = config.compressMessageLengthThreshold;
@@ -506,7 +506,7 @@ export const useChatStore = create()(
updateMessage(
sessionIndex: number,
messageIndex: number,
- updater: (message?: Message) => void
+ updater: (message?: Message) => void,
) {
const sessions = get().sessions;
const session = sessions.at(sessionIndex);
@@ -535,15 +535,15 @@ export const useChatStore = create()(
(res) => {
get().updateCurrentSession(
(session) =>
- (session.topic = res ? trimTopic(res) : DEFAULT_TOPIC)
+ (session.topic = res ? trimTopic(res) : DEFAULT_TOPIC),
);
- }
+ },
);
}
const config = get().config;
let toBeSummarizedMsgs = session.messages.slice(
- session.lastSummarizeIndex
+ session.lastSummarizeIndex,
);
const historyMsgLength = countMessages(toBeSummarizedMsgs);
@@ -551,7 +551,7 @@ export const useChatStore = create()(
if (historyMsgLength > get().config?.modelConfig?.max_tokens ?? 4000) {
const n = toBeSummarizedMsgs.length;
toBeSummarizedMsgs = toBeSummarizedMsgs.slice(
- Math.max(0, n - config.historyMessageCount)
+ Math.max(0, n - config.historyMessageCount),
);
}
@@ -564,7 +564,7 @@ export const useChatStore = create()(
"[Chat History] ",
toBeSummarizedMsgs,
historyMsgLength,
- config.compressMessageLengthThreshold
+ config.compressMessageLengthThreshold,
);
if (
@@ -589,7 +589,7 @@ export const useChatStore = create()(
onError(error) {
console.error("[Summarize] ", error);
},
- }
+ },
);
}
},
@@ -631,6 +631,6 @@ export const useChatStore = create()(
return state;
},
- }
- )
+ },
+ ),
);