New memory system
This commit is contained in:
31
src/llm.ts
31
src/llm.ts
@@ -6,7 +6,7 @@ import {AiTool, AiToolArg} from './tools.ts';
|
||||
import {fileURLToPath} from 'url';
|
||||
import {dirname, join} from 'path';
|
||||
import {spawn} from 'node:child_process';
|
||||
import {Memory, MemoryManager} from './memory.ts';
|
||||
import {Memory, MemoryCache, MemoryManager} from './memory.ts';
|
||||
|
||||
export type AnthropicConfig = {proto: 'anthropic', token: string};
|
||||
export type OpenAiConfig = {proto: 'openai', host?: string, token: string};
|
||||
@@ -55,7 +55,7 @@ export type LLMRequest = {
|
||||
/** Compress old messages in the chat to free up context */
|
||||
compress?: {max: number; min: number};
|
||||
/** User's memory documents - RAG injected automatically each turn */
|
||||
memory?: Memory[];
|
||||
memory?: Memory[] | MemoryCache;
|
||||
/** Model to use for memory operations */
|
||||
memoryModel?: string;
|
||||
/** Skill documents the AI can browse and read on demand */
|
||||
@@ -190,19 +190,20 @@ class LLM {
|
||||
}
|
||||
|
||||
// Memory
|
||||
if(options.memory) {
|
||||
const relevant = await this.memoryManager.recollect(message, options.memory, 1);
|
||||
if (options.memory) {
|
||||
const mems = options.memory instanceof MemoryCache ? options.memory.memories : options.memory;
|
||||
const relevant = await this.memoryManager.recollect(message, options.memory, 5);
|
||||
prompts.unshift(`You have access to the following memory files:
|
||||
${options.memory.map(m => `- ${m.name}: ${m.description}`).join('\n')}
|
||||
${mems.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));
|
||||
Relevant memories have been preloaded:
|
||||
${relevant.map(r => `
|
||||
**${r.name}**
|
||||
${r.description}
|
||||
${r.content}
|
||||
`).join('\n---\n')}
|
||||
` : ''}`.trim());
|
||||
tools.push(this.memoryManager.tools.read(options.memory));
|
||||
}
|
||||
|
||||
prompts.unshift(options.system || this.ai.options.llm?.system || '');
|
||||
@@ -215,7 +216,7 @@ ${relevant[0].content}
|
||||
|
||||
// Auto-memorize before compressing
|
||||
if(options.compress && this.estimateTokens(history) >= options.compress.max) {
|
||||
if(options.memory) await this.memoryManager.memorize(history, options.memory, options);
|
||||
if(options.memory) await this.memoryManager.memorize(history, options.memory, {model: options.memoryModel || this.defaultModel, ...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);
|
||||
}
|
||||
@@ -228,7 +229,7 @@ ${relevant[0].content}
|
||||
* Digest full conversation history into memory documents.
|
||||
* Call on session end to persist the conversation.
|
||||
*/
|
||||
async updateMemory(history: LLMMessage[], memories: Memory[], options: LLMRequest = {}): Promise<void> {
|
||||
async updateMemory(history: LLMMessage[], memories: Memory[] | MemoryCache, options: LLMRequest = {}): Promise<void> {
|
||||
await this.memoryManager.memorize(history, memories, {model: this.defaultModel, ...options});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user