Skip to content

Latest commit

 

History

18 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧠 Advanced RAG Cookbook

A practical collection of 10 Retrieval-Augmented Generation techniques — with working code, real benchmarks, and plain-English explanations.

Python LlamaIndex OpenAI License: MIT PRs Welcome


RAG · Advanced RAG · RAG Techniques · LlamaIndex · Hybrid RAG · HyDE · Fusion RAG · Parent-Child RAG · Retrieval Augmented Generation


What is this?

This cookbook covers 10 RAG techniques — from the simplest naive baseline to multi-stage reranking pipelines. Each technique has:

  • A README that explains what it does, why it works, and when to use it (no fluff)
  • A standalone Python script you can run directly
  • Real benchmark numbers from actual tests (not estimates)

Built with LlamaIndex + OpenAI. Tested against the same document set across all techniques so the comparisons are fair.

If you've ever Googled "which RAG technique should I use" and got a vague answer — this repo is the practical answer to that question.


Techniques

# Technique Accuracy Avg Latency Best For
01 Naive RAG Medium ~4.7s Baseline, simple docs, getting started
02 Unstructured RAG Medium ~4.9s Scanned PDFs, images, OCR-heavy files
03 Contextual Compression High ~10s Long docs, noisy datasets, high precision
04 Fusion RAG High ~7.5s Ambiguous queries, better recall
05 Hybrid RAG High ~5.6s Best all-around — keyword + semantic
06 HyDE RAG High ~12.5s Short/vague queries, Q&A over profiles
07 Parent-Child RAG Very High ~8.3s Long documents, context-heavy answers
08 RRR RAG High ~5.3s Conversational queries, chatbot interfaces
09 Sentence Compression High ~6-7s Noisy chunks, reducing context tokens
10 Rerank + Compress Very High ~10-11s Max precision, high-stakes QA

Quick Decision Guide

Default choice with no constraintsHybrid RAG — handles keyword and semantic queries, reliable across document types.

Users type conversationallyRRR RAG — rewrites vague queries before retrieval.

Scanned PDFs or imagesUnstructured RAG — OCR layer handles what text readers can't.

Short, vague questionsHyDE RAG — generates a hypothetical answer and retrieves against that.

Long documents, need context in answersParent-Child RAG — small chunks for precision, large chunks for context.

Accuracy is the only priorityRerank + Compress — two-stage post-processing, cleanest context.

Just learning RAGNaive RAG — understand the baseline before adding complexity.


Setup

1. Clone

git clone https://github.com/Jeevav62/Rag-techniques.git
cd Rag-techniques

2. Install dependencies

pip install -r requirements.txt

3. Add your OpenAI API key

cp .env.example .env
# Edit .env → add your OPENAI_API_KEY

4. (For OCR) Install Tesseract

Only needed for Unstructured RAG when working with scanned PDFs/images.

OS Command
macOS brew install tesseract
Ubuntu sudo apt-get install tesseract-ocr
Windows Download installer

Running a Technique

Each techniques/XX/ folder has a standalone script. Update SAMPLE_FILES and SAMPLE_QUERY at the top, then:

cd techniques/05_hybrid_rag
python hybrid_rag.py

Output includes: answer, latency, token count, and the exact chunks retrieved.


Interactive Demo (Gradio)

Try all 10 techniques side by side with your own documents:

python app.py

Opens at http://localhost:7860 — upload any PDF/doc/image, pick a technique, compare outputs.


Benchmark Results

All techniques tested on the same personal document QA dataset (bio/resume-style questions — "where does this person work?", "what city are they in?", etc.)

Technique Accuracy Avg Latency Extra LLM Calls
Naive RAG Medium 4.7s 0
Unstructured RAG Medium 4.9s 0
Contextual Compression High 10.0s 1 (reranker)
Fusion RAG High 7.5s 1 (query expansion)
Hybrid RAG High 5.6s 0
HyDE RAG High 12.5s 1 (hypothesis gen)
Parent-Child RAG Very High 8.3s 0
RRR RAG High 5.3s 1 (query rewrite)
Sentence Compression High 6-7s 0
Rerank + Compress Very High 10-11s 1 (reranker)

Related

Before RAG comes chunking. If you want to understand how documents should be split before retrieval, check out the companion repo: Chunking Techniques — 6 strategies from fixed-size to agentic, with the same format as this cookbook.


Stack

Component Library
RAG Framework LlamaIndex 0.10+
LLM OpenAI GPT-4o-mini
Embeddings OpenAI text-embedding-3-large
Document Parsing Unstructured
Sparse Retrieval BM25 (rank-bm25)
UI Gradio

Repo Structure

Rag-techniques/
├── app.py                          # Gradio demo — all 10 techniques
├── requirements.txt
├── .env.example
├── rag/                            # Package imported by app.py
│   ├── naive_rag.py
│   ├── unstructured_rag.py
│   ├── contextual_rag.py
│   ├── fusion_rag.py
│   ├── hybrid_rag.py
│   ├── hyde_rag.py
│   ├── parent_child_rag.py
│   ├── rrr_rag.py
│   ├── sentence_compression_rag.py
│   └── rerank_compress_rag.py
└── techniques/                     # Standalone scripts + docs per technique
    ├── 01_naive_rag/
    ├── 02_unstructured_rag/
    ├── 03_contextual_compression/
    ├── 04_fusion_rag/
    ├── 05_hybrid_rag/
    ├── 06_hyde_rag/
    ├── 07_parent_child_rag/
    ├── 08_rrr_rag/
    ├── 09_sentence_compression/
    └── 10_rerank_compress/

Contributing

Found a bug? Want to add a technique? PRs are welcome.

  1. Fork the repo
  2. Add your technique in techniques/XX_<name>/ with a README + script
  3. Add the run_rag() function to rag/<name>.py and register it in rag/__init__.py and app.py
  4. Open a PR

License

MIT — use it, modify it, share it.


If this helped you understand RAG better, consider giving it a ⭐

About

A practical cookbook of 10 Advanced RAG techniques — Naive, Hybrid, HyDE, Fusion, Parent-Child, RRR, Contextual Compression, and more. Working code + real benchmarks + plain-English explanations.

Topics

Resources

Stars

5 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages