What is RAG?
Retrieve
Convert documents to embeddings, store in a vector database, and search by semantic similarity.
Augment
Inject retrieved chunks into the LLM context before generating a response.
Generate
The model produces answers grounded in the retrieved content instead of relying only on its training data.
KnowledgeBase Class
Default search strategy.
"hybrid" combines vector + keyword search via Reciprocal Rank Fusion for the best results. See Hybrid Search.Fine-tune hybrid search weights and RRF constant. See Hybrid Search.
asTool() — Expose KB to Agents
The most powerful feature: turn a KnowledgeBase into a ToolDef that agents can call automatically.Override search mode for this tool. Inherits from KB config if not set.
Flow Diagram
1
Create KnowledgeBase
Configure name, vector store, and optional collection.
2
Initialize
Call
kb.initialize() to ensure the vector store is ready.3
Add Documents
Use
add() or addDocuments() to ingest content. Embeddings are computed if not provided.4
Expose as Tool
Call
kb.asTool() and pass the result to Agent({ tools: [...] }).5
Query
When the user asks a question, the agent calls the search tool and uses results in its response.
Next Steps
- Vector Stores — InMemory, PgVector, Qdrant, MongoDB
- Embeddings — OpenAI, Google, and EmbeddingGemma providers
- Hybrid Search — BM25, RRF, and hybrid search modes
- RAG Example — End-to-end walkthrough