feat: close #864 improve long term history

This commit is contained in:
Yifei Zhang 2023-04-18 11:42:08 +08:00 committed by GitHub
parent 146ef1bf49
commit d75b7d49b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 23 deletions

View File

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

View File

@ -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<ChatStore>()(
.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<ChatStore>()(
get().onNewMessage(botMessage);
ControllerPool.remove(
sessionIndex,
botMessage.id ?? messageIndex,
botMessage.id ?? messageIndex
);
} else {
botMessage.content = content;
@ -436,7 +436,7 @@ export const useChatStore = create<ChatStore>()(
ControllerPool.addController(
sessionIndex,
botMessage.id ?? messageIndex,
controller,
controller
);
},
filterBot: !get().config.sendBotMessages,
@ -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 = 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<ChatStore>()(
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<ChatStore>()(
(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<ChatStore>()(
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<ChatStore>()(
"[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<ChatStore>()(
onError(error) {
console.error("[Summarize] ", error);
},
},
}
);
}
},
@ -603,6 +631,6 @@ export const useChatStore = create<ChatStore>()(
return state;
},
},
),
}
)
);