/** * Knowledge Bases Example - RAG with Document Training * * This example demonstrates how to: * - Create a knowledge base * - Add different document types (PDF, DOCX, TXT, websites) * - Query the knowledge base * - Use knowledge base with agents */ import { Studio } from '../src'; async function main() { const studio = new Studio({ apiKey: process.env.LYZR_API_KEY }); try { // Create a knowledge base console.log('Creating knowledge base...'); const kb = await studio.createKnowledgeBase({ name: 'company-docs', vectorStore: 'qdrant', embeddingModel: 'text-embedding-3-large', description: 'Company documentation and knowledge base' }); console.log(`✓ Knowledge base created: ${kb.id}\n`); // Add documents (simulated - in real use, provide actual files) console.log('Adding documents...'); // Add text content await kb.addText( ` Our company was founded in 2020. We have 500+ employees across 5 countries. Our main products are AI platforms. Customer support hours: 9am-5pm EST. `, 'company-info' ); console.log('✓ Added company info'); // Add website (simulated) await kb.addWebsite( ['https://docs.example.com', 'https://blog.example.com'], { maxCrawlPages: 50, maxCrawlDepth: 3 } ); console.log('✓ Added websites'); // Note: In real usage with actual files: // await kb.addPdf('./documents/manual.pdf', { chunkSize: 1024 }); // await kb.addDocx('./documents/report.docx'); // await kb.addTxt('./documents/faq.txt'); // Query the knowledge base console.log('\nQuerying knowledge base...'); const results = await kb.query('What are the support hours?', { topK: 3, retrievalType: 'basic' }); console.log(`Found ${results.length} results:`); results.forEach((result, i) => { console.log(`\n Result ${i + 1} (score: ${result.score.toFixed(3)})`); console.log(` ${result.text.substring(0, 100)}...`); if (result.source) { console.log(` Source: ${result.source}`); } }); // Create an agent that uses this knowledge base console.log('\n\nCreating agent with knowledge base...'); const agent = await studio.createAgent({ name: 'Documentation Assistant', provider: 'gpt-4o', role: 'Documentation expert', goal: 'Answer questions using company documentation', instructions: 'Use the provided knowledge base to answer accurately' }); console.log('✓ Agent created\n'); // Run agent with knowledge base console.log('Running agent with knowledge base...'); const response = await agent.run( 'What is the support contact information?', { knowledgeBases: [kb] } ); console.log('Agent response:'); console.log(response.response); // List all documents in KB console.log('\n\nListing documents...'); const documents = await kb.listDocuments(); console.log(`Total documents: ${documents.length}`); documents.forEach(doc => { console.log(` - ${doc.name} (${doc.type}, ${doc.size} bytes)`); }); // Advanced query with custom retrieval config console.log('\n\nAdvanced query with MMR retrieval...'); const advancedResults = await kb.query('company history', { topK: 5, retrievalType: 'mmr', // Maximal Marginal Relevance scoreThreshold: 0.5, lambdaParam: 0.5 }); console.log(`Found ${advancedResults.length} diverse results`); // Clean up console.log('\n\nCleaning up...'); await agent.delete(); await kb.delete(); console.log('✓ Cleaned up'); } catch (error) { console.error('Error:', error); process.exit(1); } } main();