VigilantRAG

Technical Reference Manual & Interview Preparation Guide

Version 1.0.0

1. Executive Summary & Project Vision

VigilantRAG is an advanced, production-grade Self-Correcting Retrieval-Augmented Generation (RAG) engine. Traditional RAG setups suffer from critical vulnerabilities when deployed in production:

VigilantRAG addresses these challenges by introducing an active, self-correcting feedback loop:

  1. It evaluates the quality of retrieved contexts using a **Cross-Encoder re-ranker**. If context relevance drops below a threshold, it triggers **Query Expansion** (synonyms/LLM rephrase) to fetch better data.
  2. It runs a **Natural Language Inference (NLI)** auditor against the LLM's draft answer.
  3. If a contradiction is detected, the draft is blocked, LLM parameters are adjusted (increasing temperature, adding strict prompt constraints), and the answer is regenerated until it passes the audit.

2. Tech Stack Architecture & Tradeoffs

This project uses a fully local, resource-efficient stack designed to run on low-tier CPUs (such as free Hugging Face instances) while maintaining high precision. Below is the technical breakdown and design tradeoffs:

Component Selected Tech Why We Used It Why Not Alternatives?
Programming Language Python 3.11 Standard for AI/ML engineering. Native libraries for tensor operations, vector spaces, and transformers. Node.js / Go: Immature tooling for local model weights and execution.
Web Server FastAPI Asynchronous, highly performant, handles concurrent loops, native Pydantic validation, auto-generated OpenAPI docs. Flask: Synchronous blockages, requires manual plugins.
Django: Heavyweight, bloated for a single-page engine.
Dense Index FAISS High-performance local vector similarity search. In-memory, runs on CPU without background services. Pinecone/Milvus: Costly, require internet APIs or complex docker containers.
Sparse Index Rank-BM25 Term-frequency matching. Essential for exact names, specific serial numbers, and technical jargon. SQL LIKE: Inefficient, does not compute statistical term frequency.
Bi-Encoder all-MiniLM-L6-v2 384-dimensional dense embedding model. Extremely lightweight (90MB), runs in milliseconds on CPU. OpenAI embeddings: Introduces network dependency, API costs, and privacy concerns.
Re-ranker Model ms-marco-MiniLM-L-6-v2 Cross-Encoder model. Scores query-document pairs jointly to capture deep semantic relevance. Cosine Similarity alone: Fails to capture subtle word interactions.
Factual Auditor nli-deberta-v3-xsmall Natural Language Inference classifier. Detects logical conflicts between answer and source context. LLM-as-a-Judge: Slow, non-deterministic, expensive, and can hallucinate the audit.
Generative LLM Qwen2.5-0.5B-Instruct Lightweight, 0.5B instruction-tuned model. Runs locally on CPU, handles system prompts cleanly. Llama-3-8B: Too heavy for basic servers (requires dedicated GPU).

3. Pipeline Architecture & Data Flow

The following text diagram details how data flows through the multi-stage engine for every query:

[User Query] │ ▼ 1. [Hybrid Retrieval] ────► Extracts top 25 chunks from FAISS (Dense) │ ────► Extracts top 25 chunks from BM25 (Sparse) ▼ 2. [Merge & Deduplicate] ──► Combines candidate pools into top 50 unique chunks │ ▼ 3. [Cross-Encoder Re-rank] ─► Evaluates exact query-context pairs │ ├───► [Relevance Check] ──► Highest Score < 0.4? │ │ │ ├───► (Yes) ──► 3a. [Query Expansion] │ │ ├──► Synonym Thesaurus lookup │ │ ├──► LLM Query rephrase │ │ └──► Loop back to Hybrid Retrieval (Max 1 rewrite) │ │ │ └───► (No) ──► Proceed to Generation ▼ 4. [LLM Response Draft] ───► Generates answer based on top 5 re-ranked chunks │ ▼ 5. [NLI Factuality Audit] ─► Grades draft against source contexts │ ├───► [Hallucination Check] ──► Entailment Score < 0.6? │ │ │ ├───► (Yes) ──► 5a. [Self-Correction Loop] │ │ ├──► Increment attempt counter │ │ ├──► Increase LLM Temperature (0.2 -> 0.7) │ │ ├──► Inject strict factual prompts │ │ └──► Loop back to LLM Response Draft (Max 3 attempts) │ │ │ └───► (No) ──► Response Approved ▼ [Display Output + Telemetry UI]

4. Detailed Codebase File Directory

Here is what each file in the workspace is responsible for:

5. Interactive Dashboard UI Walkthrough

The dashboard is built with a premium glassmorphic dark-mode CSS theme. Here is how each visual component works:

A. Sidebar Control Panel

B. Pipeline Trace Map (Timeline)

A visual timeline representing the stages of execution. Clicking on any step dynamically populates the details panel on the right with internal execution variables:

C. Telemetry & Answer Cards

6. Key Technical Definitions

7. Interview Q&A Cheat Sheet

Q1: What happens when the RAG retriever fails to find relevant documents on the first pass?
A: VigilantRAG monitors the Cross-Encoder score of the top-ranked chunk. If it falls below the relevance threshold (e.g. 0.40), the engine flags it as irrelevant context. It stops, expands the query with domain synonyms or an LLM rewrite, and re-executes the search. This prevents the model from generating answers based on unrelated, garbage context.
Q2: Why use an NLI model for hallucination checking instead of another LLM query?
A: NLI models are specialized classifiers trained specifically to grade logical entailment. They are deterministic, fast (running in milliseconds on CPU), and output exact probability logits. Using an LLM to check another LLM is slow, expensive, prone to prompt injection, and suffers from the same hallucination issues it is trying to detect.
Q3: How does the system handle "unanswerable" questions where facts do not exist in the corpus?
A: When a user asks an unanswerable question, the initial relevance check fails, triggering query expansion. If the second retrieval pass still fails to find relevant chunks (relevance remains < 0.40), the engine instructs the LLM to output a fallback response ("I do not have sufficient information in the context to answer this question.") which passes the NLI check because it makes no active assertions.
Q4: Why run the models locally inside the container instead of calling API endpoints?
A: Local model execution ensures absolute data privacy, eliminates API costs, avoids network latency/outages, and ensures the application is completely self-contained. By using optimized, small-footprint models (like MiniLM and Qwen-0.5B), we run inference quickly on basic CPU servers.
VigilantRAG project documentation © 2026. Prepared for technical portfolio review.