/ examples / js / agents / context-agent.ts
context-agent.ts
 1  /**
 2   * Context Agent Example
 3   * Demonstrates RAG-enabled agent with knowledge base
 4   */
 5  
 6  import { ContextAgent, createContextAgent, KnowledgeBase } from 'praisonai';
 7  
 8  async function main() {
 9    // Create knowledge base
10    const kb = new KnowledgeBase();
11    
12    // Add documents
13    await kb.add({
14      id: 'doc1',
15      content: 'TypeScript is a strongly typed programming language that builds on JavaScript.',
16      metadata: { topic: 'typescript' }
17    });
18    
19    await kb.add({
20      id: 'doc2',
21      content: 'TypeScript adds optional static typing and class-based object-oriented programming.',
22      metadata: { topic: 'typescript' }
23    });
24  
25    await kb.add({
26      id: 'doc3',
27      content: 'PraisonAI is a framework for building AI agents in TypeScript and Python.',
28      metadata: { topic: 'praisonai' }
29    });
30  
31    // Create context agent with knowledge base
32    const agent = createContextAgent({
33      name: 'KnowledgeAgent',
34      llm: 'openai/gpt-4o-mini',
35      knowledgeBase: kb,
36      maxContextDocs: 3
37    });
38  
39    // Query with context
40    const response = await agent.chat('What is TypeScript?');
41    console.log('Response:', response.text);
42  
43    // Check conversation history
44    console.log('\nConversation history:', agent.getHistory().length, 'messages');
45  }
46  
47  main().catch(console.error);