Skip to content

Anurag-M1/Corrective-RAG

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Corrective RAG

A LangGraph-based Corrective RAG pipeline that grades retrieval quality, falls back to web search when needed, refines context, and generates grounded answers.

Corrective RAG Pipeline

Overview

This project implements a corrective retrieval flow:

  1. Retrieve relevant chunks from local PDFs.
  2. Score each chunk for relevance using an LLM.
  3. Route based on retrieval quality:
    • CORRECT: answer from local context.
    • INCORRECT: rewrite query and use web search.
    • AMBIGUOUS: combine local and web context.
  4. Refine context at sentence level with a strict relevance filter.
  5. Generate the final answer using only refined context.

Project Structure

  • Corrective RAG.ipynb - main implementation notebook.
  • Documents/book1.pdf, Documents/book2.pdf, Documents/book3.pdf - local knowledge base.
  • Corrective RAG.png - workflow image.

Tech Stack

  • LangChain
  • LangGraph
  • OpenAI (text-embedding-3-large, gpt-4o-mini)
  • FAISS
  • Tavily Search
  • Python 3.11+

Setup

python -m venv .venv
source .venv/bin/activate
pip install -U langchain langchain-community langchain-openai langgraph faiss-cpu pypdf python-dotenv tavily-python

Create a .env file in the project root:

OPENAI_API_KEY=your_openai_key
TAVILY_API_KEY=your_tavily_key

Run

  1. Open Corrective RAG.ipynb in Jupyter Notebook or VS Code.
  2. Run all cells to load PDFs, build FAISS index, and compile the graph.
  3. Change the question in the example invocation cell.
  4. Execute the final cell to get:
    • retrieval verdict (CORRECT, INCORRECT, AMBIGUOUS)
    • reason
    • web query (if used)
    • generated answer

Key Parameters

  • UPPER_TH = 0.7 - confident relevance threshold.
  • LOWER_TH = 0.3 - low relevance threshold.
  • Retriever default: k = 4.

Notes

  • The generator is instructed to answer only from provided context.
  • If context is insufficient, it returns: I don't know.
  • Web search is only used when local retrieval is weak or uncertain.

About

Corrective RAG with LangGraph: evaluates retrieval quality, routes to web search when needed, refines context, and generates grounded answers.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors