Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix delete message in memory #826

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/LLMProviders/chainManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,6 @@ export default class ChainManager {
`system prompt: ${systemPrompt}\n` +
`chat context turns: ${chatContextTurns}\n`
);
console.log("chain RunnableSequence:", ChainManager.chain);
console.log("Chat memory:", memory);
}

for await (const chunk of chatStream) {
Expand Down Expand Up @@ -377,6 +375,12 @@ export default class ChainManager {
}
updateCurrentAiMessage("");
}
if (debug) {
console.log(
"Chat memory:",
(memory as any).chatHistory.messages.map((msg: any) => msg.content)
);
}
return fullAIResponse;
}

Expand Down
24 changes: 11 additions & 13 deletions src/chatUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,20 @@ export async function updateChatMemory(
messages: ChatMessage[],
memoryManager: MemoryManager
): Promise<void> {
// Clear existing memory
await memoryManager.clearChatMemory();

let lastUserMessage = "";
for (const msg of messages) {
// Process each message in sequence
for (let i = 0; i < messages.length - 1; i++) {
const msg = messages[i];

if (msg.sender === USER_SENDER) {
lastUserMessage = msg.message;
} else if (msg.sender === AI_SENDER && lastUserMessage) {
await memoryManager
.getMemory()
.saveContext({ input: lastUserMessage }, { output: msg.message });
lastUserMessage = ""; // Reset after saving
const nextMsg = messages[i + 1];
if (nextMsg?.sender === AI_SENDER) {
await memoryManager
.getMemory()
.saveContext({ input: msg.message }, { output: nextMsg.message });
}
}
}

// If there's a trailing user message, save it with an empty AI response
if (lastUserMessage) {
await memoryManager.getMemory().saveContext({ input: lastUserMessage }, { output: "" });
}
}
Loading