Memory / history fixes
This commit is contained in:
37
src/llm.ts
37
src/llm.ts
@@ -171,8 +171,8 @@ class LLM {
|
||||
let abort = () => {};
|
||||
return Object.assign(new Promise<string>(async res => {
|
||||
let tools: AiTool[] = options.tools || this.ai.options.llm?.tools || [];
|
||||
const prompts: string[] = [options.system || this.ai.options.llm?.system || ''];
|
||||
if(!options.history) options.history = [];
|
||||
const prompts: string[] = [];
|
||||
let history = options.history || [];
|
||||
|
||||
// MCP
|
||||
const mcp = options.mcp || this.ai.options?.llm?.mcp;
|
||||
@@ -192,30 +192,33 @@ class LLM {
|
||||
|
||||
// Memory
|
||||
if(options.memory) {
|
||||
const relevant = await this.memoryManager.recollect(message, options.memory);
|
||||
if(relevant.length) {
|
||||
const context = relevant.map(m => `### ${m.name}\n${m.content}`).join('\n\n');
|
||||
options.history.push({
|
||||
id: 'auto_recall_' + Math.random().toString(), role: 'tool', name: 'recall', args: {},
|
||||
content: `Knowledge Documents:\n\n${context}`
|
||||
});
|
||||
}
|
||||
prompts.unshift('You have access to a knowledge base. Relevant documents are injected automatically before each message. Use this knowledge to inform your responses.');
|
||||
const relevant = await this.memoryManager.recollect(message, options.memory, 1);
|
||||
prompts.unshift(`You have access to the following memory files:
|
||||
${options.memory.map(m => `- ${m.name}: ${m.description}`).join('\n')}
|
||||
${relevant.length ? `
|
||||
The closest memory has been added primitively:
|
||||
\`\`\`
|
||||
Name: ${relevant[0].name}
|
||||
Description: ${relevant[0].description}
|
||||
${relevant[0].content}
|
||||
\`\`\`
|
||||
`: ''}`.trim());
|
||||
tools.push(this.memoryManager.tools.read(<Memory[]>options.memory));
|
||||
}
|
||||
|
||||
prompts.unshift(options.system || this.ai.options.llm?.system || '');
|
||||
const resp = await this.models[m].ask(message, {...options, tools, system: prompts.filter(Boolean).join('\n\n')});
|
||||
|
||||
// Trim memory injections from history
|
||||
if(options.memory) {
|
||||
options.history.splice(0, options.history.length, ...options.history.filter(h =>
|
||||
h.role !== 'tool' || h.name !== 'recall'));
|
||||
history.splice(0, history.length, ...history.filter(h => h.role !== 'tool' || h.name !== 'recall'));
|
||||
}
|
||||
|
||||
// Auto-memorize before compressing
|
||||
if(options.compress) {
|
||||
if(options.memory) await this.memoryManager.memorize(options.history, options.memory, options);
|
||||
const compressed = await this.compressHistory(options.history, options.compress.max, options.compress.min, options);
|
||||
options.history.splice(0, options.history.length, ...compressed);
|
||||
if(options.compress && this.estimateTokens(history) >= options.compress.max) {
|
||||
if(options.memory) await this.memoryManager.memorize(history, options.memory, options);
|
||||
const compressed = await this.compressHistory(history, options.compress.max, options.compress.min, options);
|
||||
if(options.history) options.history.splice(0, options.history.length, ...compressed);
|
||||
}
|
||||
|
||||
return res(resp);
|
||||
|
||||
Reference in New Issue
Block a user