Retrieval Augmented Generation (RAG) enhances LLM responses by fetching relevant context from external knowledge sources.
Pipeline:
- Index: Load → Split → Embed → Store
- Retrieve: Query → Embed → Search → Return docs
- Generate: Docs + Query → LLM → Response
Key Components:
- Document Loaders: Ingest data from files, web, databases
- Text Splitters: Break documents into chunks
- Embeddings: Convert text to vectors
- Vector Stores: Store and search embeddings
| Vector Store | Use Case | Persistence |
|---|
| InMemory | Testing | Memory only |
| FAISS | Local, high performance | Disk |
| Chroma | Development | Disk |
| Pinecone | Production, managed | Cloud |
Complete RAG Pipeline
End-to-end RAG pipeline: load documents, split into chunks, embed, store, retrieve, and generate a response.
End-to-end RAG pipeline: load documents, split into chunks, embed, store, retrieve, and generate a response.
Document Loaders
Load a PDF file and extract each page as a separate document.
Load a PDF file and extract each page as a separate document.
Fetch and parse content from a web URL into a document.
Fetch and parse content from a web URL into a document using Cheerio.
Load all text files from a directory using a glob pattern.
Text Splitting
Split documents into chunks using RecursiveCharacterTextSplitter with configurable size and overlap.
Vector Stores
Create a persistent Chroma vector store and reload it from disk.
Create a Chroma vector store connected to a running Chroma server.
Create a FAISS vector store, save it to disk, and reload it.
Create a FAISS vector store, save it to disk, and reload it.
Retrieval
Perform similarity search and retrieve results with relevance scores.
Perform similarity search and retrieve results with relevance scores.
Use MMR (Maximal Marginal Relevance) to balance relevance and diversity in search results.
Add metadata to documents and filter search results by metadata properties.
Create an agent that uses RAG as a tool for answering questions.
Create an agent that uses RAG as a tool for answering questions.
### What You CAN Configure
- Chunk size/overlap
- Embedding model
- Number of results (k)
- Metadata filters
- Search algorithms: Similarity, MMR
- Embedding dimensions (per model)
- Mix embeddings from different models in same store
Chunk size 500-1500 is typically good.
Chunk size 500-1500 is typically good.
Use overlap (10-20% of chunk size) to maintain context at boundaries.
Use persistent vector store instead of in-memory to avoid data loss.
Use persistent vector store instead of in-memory to avoid data loss.
Use the same embedding model for indexing and querying.
Use the same embedding model for indexing and querying.
Only opt in to FAISS deserialization for trusted local indexes. Python FAISS indexes include pickle-backed metadata, and untrusted pickle files can execute arbitrary code during loading.
If you cannot guarantee the provenance of a persisted index, do not load it with allow_dangerous_deserialization=True. Rebuild the index from trusted source documents or use a vector store/backend that does not require pickle deserialization for untrusted files.
Ensure embedding dimensions match the vector store index dimensions.