conversation-memory.ts
1 /** 2 * Conversation Memory Example 3 * Demonstrates memory management for conversations 4 */ 5 6 import { Memory, createMemory } from 'praisonai'; 7 8 async function main() { 9 const memory = createMemory({ 10 maxEntries: 100, 11 maxTokens: 10000 12 }); 13 14 // Simulate a conversation 15 await memory.add('What is TypeScript?', 'user'); 16 await memory.add('TypeScript is a strongly typed programming language that builds on JavaScript.', 'assistant'); 17 await memory.add('What are its main benefits?', 'user'); 18 await memory.add('Key benefits include type safety, better IDE support, and easier refactoring.', 'assistant'); 19 await memory.add('Can you give an example?', 'user'); 20 21 console.log('Memory size:', memory.size, 'entries\n'); 22 23 // Get recent messages 24 console.log('=== Recent Messages ==='); 25 const recent = memory.getRecent(3); 26 recent.forEach(entry => { 27 console.log(`[${entry.role}]: ${entry.content.substring(0, 50)}...`); 28 }); 29 30 // Search memory 31 console.log('\n=== Search Results for "TypeScript" ==='); 32 const results = await memory.search('TypeScript'); 33 results.forEach(r => { 34 console.log(`Score: ${r.score.toFixed(2)} - ${r.entry.content.substring(0, 40)}...`); 35 }); 36 37 // Build context 38 console.log('\n=== Full Context ==='); 39 console.log(memory.buildContext()); 40 } 41 42 main().catch(console.error);