Advanced RAG Patterns & Optimization
From the course AI Embeddings & Vector Databases
Basic RAG (retrieve then generate) works but has well-known limitations. Advanced patterns address these systematically.
Hybrid Search
Combine vector similarity with keyword search (BM25) for the best of both worlds:
- Vector search catches semantic matches ("automobile" ↔ "car")
- Keyword search catches exact matches (product IDs, names, codes)
- Reciprocal Rank Fusion (RRF) merges results from both
Most production RAG systems use hybrid search. Weaviate and Qdrant support it natively.
Multi-Query Retrieval
A single user query might be ambiguous. Generate multiple query variations and retrieve for each:
- Original: "How does authentication work?"
- Variation: "What are the login methods available?"
- Variation: "How do users sign up and verify their identity?"
Union the results for higher recall.
Contextual Compression
Retrieved chunks often contain irrelevant content. An LLM can extract only the relevant sentences before passing to the generator, reducing noise and token costs.
Parent Document Retrieval
Index small chunks for precise matching, but retrieve the larger parent document for context. This gives you the precision of small chunks with the context of large ones.
Self-RAG
The model decides whether it needs retrieval, retrieves if necessary, then self-evaluates the relevance of retrieved documents before generating. This prevents hallucinations from irrelevant retrievals.
Evaluation Metrics
Measure your RAG system with:
- Retrieval precision@k: What fraction of retrieved documents are relevant?
- Retrieval recall@k: What fraction of relevant documents were retrieved?
- Answer faithfulness: Does the generated answer stay true to retrieved context?
- Answer relevance: Does the answer actually address the question?
Tools like RAGAS, DeepEval, and TruLens automate these evaluations.
Key Takeaways
- •Hybrid search (vector + BM25) outperforms either approach alone in production
- •Multi-query retrieval improves recall by searching with query variations
- •Parent document retrieval balances precision of small chunks with full context
- •Evaluate with retrieval precision/recall and answer faithfulness/relevance
Progress is kept in this browser only.