Overview
This guide walks you through building a complete Retrieval-Augmented Generation (RAG) system step by step. By the end, your agent will answer questions using your own documents.
Prerequisites
Install the required dependencies:
Complete Example
Step-by-Step Breakdown
1. Embedding Provider
The embedding provider converts text into vector representations for semantic search. The vector_size in your vector database config must match the embedding model’s output dimension (1536 for text-embedding-3-small).
2. Vector Database
The vector database stores embedded document chunks for fast similarity search. Choose any supported provider — this example uses Chroma in embedded mode.
3. Knowledge Base
KnowledgeBase orchestrates the entire pipeline: it loads documents, chunks text, generates embeddings, and stores them.
If you don’t specify loaders, KnowledgeBase auto-detects the appropriate loader based on file extension. Explicit loaders give you more control over parsing behavior.
4. Agent + Task
Pass the knowledge base as context to the Task. The agent automatically queries it for relevant chunks before generating a response.
What Happens Behind the Scenes
- Document Loading — KnowledgeBase detects the file type and loads the document
- Text Chunking — The document is split into smaller chunks optimized for retrieval
- Embedding Generation — Each chunk is converted to a vector embedding
- Vector Storage — Embeddings are stored in the vector database
- Query Processing — When the task runs, the description is embedded and matched against stored chunks
- Context Injection — The most relevant chunks are injected into the agent’s context
- Response Generation — The agent uses the retrieved context to generate an answer
Next Steps
- Examples — More patterns: async, streaming, multiple KBs, custom splitters
- Using as Tool — Let agents actively search the KB (instead of auto-injection)
- Vector Stores — Compare and choose the right vector database
- Query Control — Fine-tune when RAG context is injected