forked from XiaoMo/ChatGPT-Next-Web
chore: fix memory prompt
This commit is contained in:
parent
4d97c269ff
commit
12f52bf252
34
app/store.ts
34
app/store.ts
@ -36,7 +36,7 @@ export interface ChatConfig {
|
|||||||
|
|
||||||
const DEFAULT_CONFIG: ChatConfig = {
|
const DEFAULT_CONFIG: ChatConfig = {
|
||||||
historyMessageCount: 4,
|
historyMessageCount: 4,
|
||||||
compressMessageLengthThreshold: 500,
|
compressMessageLengthThreshold: 1000,
|
||||||
sendBotMessages: true as boolean,
|
sendBotMessages: true as boolean,
|
||||||
submitKey: SubmitKey.CtrlEnter as SubmitKey,
|
submitKey: SubmitKey.CtrlEnter as SubmitKey,
|
||||||
avatar: "1f603",
|
avatar: "1f603",
|
||||||
@ -105,6 +105,7 @@ interface ChatStore {
|
|||||||
updater: (message?: Message) => void
|
updater: (message?: Message) => void
|
||||||
) => void;
|
) => void;
|
||||||
getMessagesWithMemory: () => Message[];
|
getMessagesWithMemory: () => Message[];
|
||||||
|
getMemoryPrompt: () => Message,
|
||||||
|
|
||||||
getConfig: () => ChatConfig;
|
getConfig: () => ChatConfig;
|
||||||
resetConfig: () => void;
|
resetConfig: () => void;
|
||||||
@ -241,17 +242,23 @@ export const useChatStore = create<ChatStore>()(
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
getMemoryPrompt() {
|
||||||
|
const session = get().currentSession()
|
||||||
|
|
||||||
|
return {
|
||||||
|
role: 'system',
|
||||||
|
content: '这是 ai 和用户的历史聊天总结作为前情提要:' + session.memoryPrompt,
|
||||||
|
date: ''
|
||||||
|
} as Message
|
||||||
|
},
|
||||||
|
|
||||||
getMessagesWithMemory() {
|
getMessagesWithMemory() {
|
||||||
const session = get().currentSession()
|
const session = get().currentSession()
|
||||||
const config = get().config
|
const config = get().config
|
||||||
const n = session.messages.length
|
const n = session.messages.length
|
||||||
const recentMessages = session.messages.slice(n - config.historyMessageCount);
|
const recentMessages = session.messages.slice(n - config.historyMessageCount);
|
||||||
|
|
||||||
const memoryPrompt: Message = {
|
const memoryPrompt = get().getMemoryPrompt()
|
||||||
role: 'system',
|
|
||||||
content: '这是 ai 和用户的历史聊天总结作为前情提要:' + session.memoryPrompt,
|
|
||||||
date: ''
|
|
||||||
}
|
|
||||||
|
|
||||||
if (session.memoryPrompt) {
|
if (session.memoryPrompt) {
|
||||||
recentMessages.unshift(memoryPrompt)
|
recentMessages.unshift(memoryPrompt)
|
||||||
@ -288,17 +295,24 @@ export const useChatStore = create<ChatStore>()(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const config = get().config
|
const config = get().config
|
||||||
const messages = get().getMessagesWithMemory()
|
let toBeSummarizedMsgs = session.messages.slice(session.lastSummarizeIndex)
|
||||||
const toBeSummarizedMsgs = get().getMessagesWithMemory()
|
|
||||||
const historyMsgLength = toBeSummarizedMsgs.reduce((pre, cur) => pre + cur.content.length, 0)
|
const historyMsgLength = toBeSummarizedMsgs.reduce((pre, cur) => pre + cur.content.length, 0)
|
||||||
|
|
||||||
|
if (historyMsgLength > 4000) {
|
||||||
|
toBeSummarizedMsgs = toBeSummarizedMsgs.slice(-config.historyMessageCount)
|
||||||
|
}
|
||||||
|
|
||||||
|
// add memory prompt
|
||||||
|
toBeSummarizedMsgs.unshift(get().getMemoryPrompt())
|
||||||
|
|
||||||
const lastSummarizeIndex = session.messages.length
|
const lastSummarizeIndex = session.messages.length
|
||||||
|
|
||||||
console.log('[Chat History] ', messages, historyMsgLength, config.compressMessageLengthThreshold)
|
console.log('[Chat History] ', toBeSummarizedMsgs, historyMsgLength, config.compressMessageLengthThreshold)
|
||||||
|
|
||||||
if (historyMsgLength > config.compressMessageLengthThreshold) {
|
if (historyMsgLength > config.compressMessageLengthThreshold) {
|
||||||
requestChatStream(toBeSummarizedMsgs.concat({
|
requestChatStream(toBeSummarizedMsgs.concat({
|
||||||
role: 'system',
|
role: 'system',
|
||||||
content: '总结一下 ai 和用户的对话,用作后续的上下文提示 prompt,控制在 100 字以内,你在回复时用 ai 自称',
|
content: '简要总结一下你和用户的对话,用作后续的上下文提示 prompt,控制在 50 字以内',
|
||||||
date: ''
|
date: ''
|
||||||
}), {
|
}), {
|
||||||
filterBot: false,
|
filterBot: false,
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
export function trimTopic(topic: string) {
|
export function trimTopic(topic: string) {
|
||||||
const s = topic.split("").slice(0, 20);
|
const s = topic.split("");
|
||||||
let lastChar = s.at(-1); // 获取 s 的最后一个字符
|
let lastChar = s.at(-1); // 获取 s 的最后一个字符
|
||||||
let pattern = /[,。!?、]/; // 定义匹配中文标点符号的正则表达式
|
let pattern = /[,。!?、]/; // 定义匹配中文标点符号的正则表达式
|
||||||
while (lastChar && pattern.test(lastChar!)) {
|
while (lastChar && pattern.test(lastChar!)) {
|
||||||
|
Loading…
Reference in New Issue
Block a user