A multi-stage Retrieval-Augmented Generation (RAG) system that reduces AI hallucinations through automated fact-checking and verification.
Traditional RAG systems suffer from hallucination - generating answers not supported by source documents. This project implements a 4-stage pipeline that:
- Retrieves relevant documents
- Filters for relevance
- Generates answers
- Fact-checks against sources before delivery
- ✅ 100% Free - Uses Ollama (runs locally, no API costs)
- ✅ No API Keys Required - Completely offline after setup
- ✅ Multi-Stage Verification - 4 distinct agents working together
- ✅ Hallucination Detection - Automatically rejects unsupported answers
- ✅ Privacy-First - All data stays on your machine
- LLM: Ollama (Llama 3.2)
- Framework: LangChain
- Vector Store: FAISS
- Embeddings: HuggingFace (sentence-transformers)
- Language: Python 3.9+
- macOS (or Linux)
- Python 3.9+
- Ollama
Download from: https://ollama.com/download
Or install via Homebrew:
brew install ollamaollama pull llama3.2git clone https://github.com/mchavva413/self-correcting-rag.git
cd self-correcting-ragpip3 install langchain langchain-community faiss-cpu sentence-transformerspython3 main.pyThe system will prompt you to enter questions. Try:
- "Who created Python?"
- "When was JavaScript created?"
- "What is React?"
Type quit to exit.
self-correcting-rag/
├── agents/
│ ├── __init__.py
│ ├── relevance_agent.py # Stage 1: Filters relevant documents
│ ├── generator_agent.py # Stage 2: Generates answers
│ └── factcheck_agent.py # Stage 3: Verifies factual consistency
├── utils/
│ ├── __init__.py
│ └── vector_store.py # Document embedding & retrieval
├── data/
│ └── documents.txt # Knowledge base
├── main.py # Main pipeline orchestrator
└── README.md
Uses FAISS vector similarity search to find potentially relevant documents.
An LLM agent evaluates each retrieved document for relevance to the query.
Generates an answer using ONLY the relevant documents (no external knowledge).
Verifies the answer against source documents and assigns a consistency score (0-100).
- Score ≥ 70: Answer APPROVED ✅
- Score < 70: Answer REJECTED ❌
🔎 Enter your question: Who created Python?
STAGE 1️⃣: DOCUMENT RETRIEVAL
✓ Retrieved 4 documents
STAGE 2️⃣: RELEVANCE FILTERING
✓ Found 1/4 relevant documents
STAGE 3️⃣: ANSWER GENERATION
💡 Generated Answer:
Python was created by Guido van Rossum and first released in 1991.
STAGE 4️⃣: FACT-CHECKING
✓ Fact-check complete: PASSED ✅
🏁 FINAL STATUS: APPROVED ✅
This project demonstrates:
- Multi-agent AI systems
- RAG architecture
- Vector embeddings and similarity search
- Prompt engineering
- Hallucination detection and mitigation
Pull requests are welcome! For major changes, please open an issue first.
MIT License
- Anthropic for Claude (used for development guidance)
- Meta for Llama models
- LangChain community