An open-source collection of tutorials and runnable scripts for over 30 advanced Retrieval-Augmented Generation (RAG) techniques, including Graph RAG, Agentic RAG, and various retrieval optimization strategies, implemented primarily with LangChain.
RAG_Techniques Project Overview#
Project Positioning#
RAG_Techniques is an open-source project initiated and maintained by Nir Diamant, dedicated to becoming the "technical encyclopedia" in the RAG field. The project provides not only theoretical explanations but also executable code based on Jupyter Notebooks and standalone Python scripts. Content ranges from simple vector retrieval to complex Graph RAG and Agentic RAG architectures.
Core Problems Solved#
- Insufficient Accuracy: Addresses inaccurate retrieval in traditional simple RAG when handling complex queries
- Context Loss: Resolves context fragmentation after chunking long documents
- Evaluation Difficulty: Provides standardized evaluation methods (DeepEval, GroUSE) to measure RAG system performance
- High Implementation Barrier: Reduces difficulty of integrating advanced RAG techniques into production through modular Notebooks and utility functions
Technology Categories (34 Techniques)#
Basic RAG Techniques#
- Simple RAG - Entry-level retrieval-based QA system for beginners
- Reliable RAG - Adds verification and refinement mechanisms to ensure accuracy
- Optimizing Chunk Sizes - Text chunking optimization balancing context retention and retrieval efficiency
- Proposition Chunking - Decomposes text into concise, complete, meaningful sentences
Query Enhancement Techniques#
- Query Transformations - Query rewriting, step-back prompting, sub-query decomposition
- HyDE (Hypothetical Document Embedding) - Generates hypothetical questions to improve query-data matching
- HyPE (Hypothetical Prompt Embeddings) - Pre-computes hypothetical prompts, improving retrieval accuracy by up to 42%
Context Enhancement Techniques#
- Contextual Chunk Headers - Creates document-level and paragraph-level context headers
- Relevant Segment Extraction - Dynamically constructs multi-chunk text segments
- Semantic Chunking - Splits documents based on semantic coherence rather than fixed size
- Contextual Compression - Compresses retrieved information while preserving query-relevant content
Advanced Retrieval Methods#
- Fusion Retrieval - Combines keyword search and vector search
- Reranking - LLM scoring, cross-encoders, metadata-enhanced sorting
- Hierarchical Indices - Dual-layer system of document summaries and detailed chunks
- Multi-modal RAG - Multi-modal retrieval (caption generation and Colpali methods)
Advanced Architectures#
- Graph RAG with LangChain - Knowledge graph integration
- Microsoft GraphRAG - Microsoft's open-source GraphRAG system
- RAPTOR - Tree-organized recursive abstraction processing for retrieval
- Self-RAG - Adaptively decides whether to use retrieved information
- Corrective RAG (CRAG) - Dynamically evaluates and corrects retrieval process
- Agentic RAG with Contextual AI - Building production-grade agentic RAG pipelines
Application Scenarios#
- Financial Document Analysis: Agentic RAG for complex query resolution
- Knowledge Base QA: Enterprise-level document retrieval and QA systems
- Multi-modal Content Processing: PDF, PPT and other multimedia data processing
- Complex Reasoning Tasks: Multi-hop questions and complex query processing
Project Structure#
RAG_Techniques/
├── all_rag_techniques/ # Jupyter notebook implementations
├── all_rag_techniques_runnable_scripts/ # Runnable Python scripts
├── data/ # Sample data
├── evaluation/ # Evaluation methods
├── images/ # Technical diagrams
├── helper_functions.py # Core helper functions
└── CONTRIBUTING.md # Contribution guidelines
Core Helper Functions#
encode_pdf(): Encodes PDF documents to vector storeretrieve_context_per_question(): Retrieves context for questionsbm25_retrieval(): BM25 retrievalexponential_backoff(): Exponential backoff retry mechanism
Quick Start#
# Clone repository
git clone https://github.com/NirDiamant/RAG_Techniques.git
# Navigate to technique of interest
cd all_rag_techniques/[technique-name]
# Configure environment variables
export OPENAI_API_KEY="your_key_here"
Core Dependencies#
- langchain-community
- langchain-text-splitters
- langchain-openai
- langchain-core
- pydantic
- rank_bm25
- PyMuPDF (fitz)
- numpy
- faiss-cpu
Configuration Capabilities#
Embedding Providers#
class EmbeddingProvider(Enum):
OPENAI = "openai"
COHERE = "cohere"
AMAZON_BEDROCK = "bedrock"
Model Providers#
class ModelProvider(Enum):
OPENAI = "openai"
GROQ = "groq"
ANTHROPIC = "anthropic"
AMAZON_BEDROCK = "bedrock"
Notebook Structure Standards#
Each technique Notebook includes: Title and overview, detailed explanation, visual representation (Mermaid diagrams), implementation steps, usage examples, comparative analysis, and reference resources.
Related Projects#
- Agents Towards Production: Production-grade GenAI agent building
- GenAI Agents Repository: AI agent implementation collection
- Prompt Engineering Techniques: Prompt engineering strategies