An implementation of Corrective Retrieval-Augmented Generation (CRAG) based on the research paper Corrective Retrieval Augmented Generation (arXiv: 2401.15884) using LangGraph, Ollama (Qwen2.5-Coder:7b), and monoT5-Large for document and strip-level evaluation.
This pipeline implements the core concepts from the CRAG paper:
- Lightweight Retrieval Evaluator: Integrates a local
castorini/monot5-large-msmarcopointwise reranker. - Confidence-Based Action Triggering:
- CORRECT: Refines retrieved internal documents.
- INCORRECT: Ignores internal documents and triggers a web search fallback.
- AMBIGUOUS: Merges and refines both internal and external web search documents.
- Decompose-then-Recompose (Knowledge Refinement): Sentence-level decomposition and filtering using the T5 evaluator.
- Query Rewriter: Translates natural language questions into search engine keywords.
-
Install the required dependencies:
pip install -r requirements.txt
-
Create a
.envfile based on.env.exampleand add your Tavily API Key:TAVILY_API_KEY=your_tavily_api_key_here
-
Ensure Ollama is running locally with the target model:
ollama run qwen2.5-coder:7b
-
Place your PDF files under a directory named
documents/. -
Verify your environment setup before running:
python verify_env.py
-
Run the pipeline:
python crag_pipeline.py
The pipeline's behavior can be tuned in the crag_pipeline.py configuration section using the following parameters:
UPPER_TH(default0.70): Documents scoring above this threshold are deemed highly relevant, triggering the CORRECT path.LOWER_TH(default0.30): If all documents score below this threshold, the INCORRECT path is triggered, initiating web search fallback.STRIP_TH(default0.40): Used during the decompose-then-recompose step to filter out individual sentence strips.