Ground agent responses in your documents and knowledge bases with semantic search
RAG enables agents to retrieve and reference information from your documents, knowledge bases, and data sources, providing accurate, contextual responses grounded in your content.
The RAG (Retrieval-Augmented Generation) primitive empowers agents to access and utilize information from your document collections, knowledge bases, and proprietary data. By combining semantic search with generation, agents can provide accurate answers grounded in your specific content rather than relying solely on training data.RAG is essential for:
Knowledge Base Access: Answer questions from documentation, manuals, and guides
Document Search: Find and reference specific information across large document sets
Contextual Accuracy: Provide responses grounded in verified sources
Domain Expertise: Specialize agents in specific knowledge domains
Citation Support: Back up responses with source references
Up-to-Date Information: Access latest documentation without retraining
Semantic Search
Find relevant information using natural language queries with vector embeddings
Source Attribution
Automatically cite sources and provide references for generated responses
Multi-Format Support
Index PDFs, Word docs, text files, markdown, HTML, and more
Real-Time Updates
Update knowledge base in real-time as documents change
Indexing: Documents are processed and converted to vector embeddings
Query: User question is embedded using same model
Retrieval: Most relevant document chunks are retrieved via semantic search
Context Injection: Retrieved content is added to agent’s context
Generation: Agent generates response using retrieved information
Citation: Sources are cited in the response
Semantic Understanding: RAG uses vector embeddings to understand meaning, not just keyword matching. Questions like “How do I reset my password?” will match “Password recovery steps” even without exact word overlap.
// Create support knowledge baseconst supportKB = await agentbase.createDatastore({ name: "Customer Support KB"});// Upload help articlesawait agentbase.uploadDocuments({ datastoreId: supportKB.id, files: [ './help/getting-started.md', './help/troubleshooting.md', './help/faq.md', './help/account-management.md' ], metadata: { category: "support", language: "en" }});// Support agent with RAGconst result = await agentbase.runAgent({ message: "I forgot my password. How do I reset it?", datastores: [{ id: supportKB.id }], system: `You are a customer support agent. Use the knowledge base to: - Find accurate answers to customer questions - Cite relevant help articles - Provide step-by-step instructions - Escalate if information not available`});console.log('Answer:', result.message);console.log('Help articles cited:', result.sources);
const medicalKB = await agentbase.createDatastore({ name: "Medical Knowledge Base", config: { encryption: true, hipaaCompliant: true }});await agentbase.uploadDocuments({ datastoreId: medicalKB.id, files: [ './medical/protocols/*.pdf', './medical/drug-database.json', './medical/icd-codes.csv' ], metadata: { classification: "medical", verified: true }});const result = await agentbase.runAgent({ message: "What are the treatment protocols for Type 2 diabetes?", datastores: [{ id: medicalKB.id }], system: `You are a medical information assistant. Important: - Provide evidence-based information - Cite medical sources and protocols - Never provide medical diagnosis - Always recommend consulting healthcare provider - Maintain HIPAA compliance`, rules: [ "Never provide medical diagnosis", "Always cite medical sources", "Recommend consulting healthcare provider for medical advice" ]});
const productCatalog = await agentbase.createDatastore({ name: "Product Catalog"});await agentbase.uploadDocuments({ datastoreId: productCatalog.id, files: [ './products/catalog.json', './products/specifications/*.md', './products/manuals/*.pdf' ]});const result = await agentbase.runAgent({ message: "I need a laptop with at least 16GB RAM and long battery life", datastores: [{ id: productCatalog.id, searchMode: "hybrid" // Good for product specs }], system: `You are a product recommendation assistant. Help customers: - Find products matching their needs - Compare product specifications - Provide pricing information - Suggest alternatives`});
# Good: Well-structured document## Authentication OverviewOur API uses OAuth 2.0 for authentication...## Getting Started1. Create API credentials2. Implement OAuth flow3. Make authenticated requests## Code Example[code here]## Common Issues- Issue 1: [solution]- Issue 2: [solution]---# Avoid: Wall of unstructured textAuthentication is done with OAuth 2.0 and you need to create credentials first then implement the flow and make requests but sometimes there are issues...
Specific Questions Work Best: RAG performs best with specific, targeted questions rather than broad, open-ended queries.
Copy
// Good: Specific question"What are the rate limits for the /api/users endpoint?"// Good: Targeted query"How do I implement OAuth 2.0 authentication?"// Less effective: Too broad"Tell me everything about the API"// Less effective: Too vague"How does it work?"
const result = await agentbase.runAgent({ message: "What did we discuss about the API earlier?", datastores: [{ id: apiDocs }], memory: { namespace: `user_${userId}`, enabled: true }});// Agent uses:// 1. Memory: Recalls previous conversation// 2. RAG: References API documentation// 3. Combines both for contextual answer
const result = await agentbase.runAgent({ message: "Show me the deployment guide and check current system status", datastores: [{ id: deploymentDocs }], mcpServers: [ { serverName: "monitoring", serverUrl: "https://api.company.com/monitoring" } ]});// Agent combines:// - Documentation from RAG// - Live system status from MCP tools
Remember: RAG is most effective when your documents are well-structured, current, and organized with meaningful metadata. Invest time in document preparation for best results.