Standard Retrieval-Augmented Generation (RAG) pipelines suffer from "garbage-in, garbage-out." If the vector database retrieves irrelevant documents, the LLM hallucinates incorrect answers.
VigilantRAG solves this by implementing a production-grade, self-correcting RAG pipeline that audits both its search quality and answer accuracy. It features a two-stage hybrid search, an automated query expansion loop when search results are poor, and a Natural Language Inference (NLI) "Auditor" model that blocks and regenerates responses if they contain hallucinations.
The following diagram illustrates how user queries flow through the self-correcting retrieval and generation pipelines:
graph TD
UserQuery([User Query]) --> Preprocess[Tokenize & Preprocess]
Preprocess --> DenseSearch[FAISS Dense Search<br/>all-MiniLM-L6-v2]
Preprocess --> SparseSearch[BM25 Sparse Search]
DenseSearch --> RetrieveTop25D[Retrieve Top 25 Chunks]
SparseSearch --> RetrieveTop25S[Retrieve Top 25 Chunks]
RetrieveTop25D --> MergeDedup[Merge & Deduplicate<br/>Max 50 Candidates]
RetrieveTop25S --> MergeDedup
MergeDedup --> CrossEncoder[Cross-Encoder Re-ranker<br/>ms-marco-MiniLM-L-6-v2]
CrossEncoder --> ScoreChunks[Score Candidates]
ScoreChunks --> SortTop5[Sort & Select Top 5 Chunks]
SortTop5 --> EvalRelevance{Top Score >= 0.4?}
EvalRelevance -- No: Irrelevant --> QueryExpand[Query Expansion<br/>LLM Rewrite / Thesaurus]
QueryExpand -->|Retry with new query| Preprocess
EvalRelevance -- Yes: Relevant --> LLMGen[Local LLM Response Generator<br/>TinyLlama-1.1B / Qwen-0.5B]
LLMGen --> GenAnswer[Generated Answer]
SortTop5 --> NLIGuard{NLI Hallucination Guard<br/>bart-large-mnli / deberta-v3}
GenAnswer --> NLIGuard
NLIGuard -- Contradiction/Neutral (Hallucination) --> AdjustPrompt[Adjust System Prompt &<br/>Increase Temperature]
AdjustPrompt -->|Regenerate Answer| LLMGen
NLIGuard -- Entailment (Valid) --> ReturnAnswer([Return Verified Answer])
ms-marco-MiniLM-L-6-v2 re-ranker to score and filter candidate chunks down to the 5 most relevant.cross-encoder/nli-deberta-v3-xsmall). If it detects contradictions or unverified facts, it blocks the output and forces the LLM to regenerate with a modified system prompt and higher temperature.git clone https://github.com/your-username/VigilantRAG.git
cd VigilantRAG
# Windows
python -m venv venv
venv\Scripts\activate
# macOS/Linux
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
python app.py
Open http://localhost:8000 in your web browser.
You can run the entire self-contained environment (including models) inside a Docker container:
# Build the image (this will pre-download the models into the image)
docker build -t vigilantrag .
# Run the container
docker run -p 8000:7860 vigilantrag
Open http://localhost:8000 to interact with the containerized application.
To run the automated pytest test suite:
pytest
This tests chunking thresholds, FAISS/BM25 merging, query expansion fallbacks, NLI score mapping, and FastAPI CRUD endpoints.
VigilantRAG/
βββ src/
β βββ __init__.py
β βββ config.py # Global thresholds, model configurations
β βββ retriever.py # Chunking, FAISS and BM25 index managers
β βββ reranker.py # Cross-Encoder candidate scoring
β βββ query_expansion.py # Synonym dictionary & LLM rewrite callbacks
β βββ hallucination_guard.py# NLI model entailment checks
β βββ llm_client.py # Local LLM causal generation client
β βββ engine.py # Orchestrates the RAG loop & telemetry
βββ static/ # Dashboard Frontend
β βββ index.html
β βββ style.css
β βββ main.js
βββ tests/ # pytest automated suite
β βββ __init__.py
β βββ test_api.py
β βββ test_retriever.py
β βββ test_reranker.py
β βββ test_query_expansion.py
β βββ test_hallucination_guard.py
βββ app.py # FastAPI server entry point
βββ download_models.py # Model pre-caching utility for builds
βββ requirements.txt # Python dependencies
βββ Dockerfile # Deployment image manifest
Standard Retrieval-Augmented Generation (RAG) pipelines suffer from "garbage-in, garbage-out." If the vector database retrieves irrelevant documents, the LLM hallucinates incorrect answers.
VigilantRAG solves this by implementing a production-grade, self-correcting RAG pipeline that audits both its search quality and answer accuracy. It features a two-stage hybrid search, an automated query expansion loop when search results are poor, and a Natural Language Inference (NLI) "Auditor" model that blocks and regenerates responses if they contain hallucinations.
The following diagram illustrates how user queries flow through the self-correcting retrieval and generation pipelines:
graph TD
UserQuery([User Query]) --> Preprocess[Tokenize & Preprocess]
Preprocess --> DenseSearch[FAISS Dense Search<br/>all-MiniLM-L6-v2]
Preprocess --> SparseSearch[BM25 Sparse Search]
DenseSearch --> RetrieveTop25D[Retrieve Top 25 Chunks]
SparseSearch --> RetrieveTop25S[Retrieve Top 25 Chunks]
RetrieveTop25D --> MergeDedup[Merge & Deduplicate<br/>Max 50 Candidates]
RetrieveTop25S --> MergeDedup
MergeDedup --> CrossEncoder[Cross-Encoder Re-ranker<br/>ms-marco-MiniLM-L-6-v2]
CrossEncoder --> ScoreChunks[Score Candidates]
ScoreChunks --> SortTop5[Sort & Select Top 5 Chunks]
SortTop5 --> EvalRelevance{Top Score >= 0.4?}
EvalRelevance -- No: Irrelevant --> QueryExpand[Query Expansion<br/>LLM Rewrite / Thesaurus]
QueryExpand -->|Retry with new query| Preprocess
EvalRelevance -- Yes: Relevant --> LLMGen[Local LLM Response Generator<br/>TinyLlama-1.1B / Qwen-0.5B]
LLMGen --> GenAnswer[Generated Answer]
SortTop5 --> NLIGuard{NLI Hallucination Guard<br/>bart-large-mnli / deberta-v3}
GenAnswer --> NLIGuard
NLIGuard -- Contradiction/Neutral (Hallucination) --> AdjustPrompt[Adjust System Prompt &<br/>Increase Temperature]
AdjustPrompt -->|Regenerate Answer| LLMGen
NLIGuard -- Entailment (Valid) --> ReturnAnswer([Return Verified Answer])
ms-marco-MiniLM-L-6-v2 re-ranker to score and filter candidate chunks down to the 5 most relevant.cross-encoder/nli-deberta-v3-xsmall). If it detects contradictions or unverified facts, it blocks the output and forces the LLM to regenerate with a modified system prompt and higher temperature.git clone https://github.com/your-username/VigilantRAG.git
cd VigilantRAG
# Windows
python -m venv venv
venv\Scripts\activate
# macOS/Linux
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
python app.py
Open http://localhost:8000 in your web browser.
You can run the entire self-contained environment (including models) inside a Docker container:
# Build the image (this will pre-download the models into the image)
docker build -t vigilantrag .
# Run the container
docker run -p 8000:7860 vigilantrag
Open http://localhost:8000 to interact with the containerized application.
To run the automated pytest test suite:
pytest
This tests chunking thresholds, FAISS/BM25 merging, query expansion fallbacks, NLI score mapping, and FastAPI CRUD endpoints.
VigilantRAG/
βββ src/
β βββ __init__.py
β βββ config.py # Global thresholds, model configurations
β βββ retriever.py # Chunking, FAISS and BM25 index managers
β βββ reranker.py # Cross-Encoder candidate scoring
β βββ query_expansion.py # Synonym dictionary & LLM rewrite callbacks
β βββ hallucination_guard.py# NLI model entailment checks
β βββ llm_client.py # Local LLM causal generation client
β βββ engine.py # Orchestrates the RAG loop & telemetry
βββ static/ # Dashboard Frontend
β βββ index.html
β βββ style.css
β βββ main.js
βββ tests/ # pytest automated suite
β βββ __init__.py
β βββ test_api.py
β βββ test_retriever.py
β βββ test_reranker.py
β βββ test_query_expansion.py
β βββ test_hallucination_guard.py
βββ app.py # FastAPI server entry point
βββ download_models.py # Model pre-caching utility for builds
βββ requirements.txt # Python dependencies
βββ Dockerfile # Deployment image manifest