Aryan commited on
Commit
43f08ef
Β·
1 Parent(s): 9a486df

docs: add comprehensive README and deployment guide

Browse files
Files changed (2) hide show
  1. README.md +162 -0
  2. deployment_guide.md +97 -0
README.md ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # VigilantRAG: Self-Correcting Multi-Stage RAG Engine
2
+
3
+ Standard Retrieval-Augmented Generation (RAG) pipelines suffer from **"garbage-in, garbage-out."** If the vector database retrieves irrelevant documents, the LLM hallucinates incorrect answers.
4
+
5
+ **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.
6
+
7
+ ---
8
+
9
+ ## πŸ› οΈ System Architecture
10
+
11
+ The following diagram illustrates how user queries flow through the self-correcting retrieval and generation pipelines:
12
+
13
+ ```mermaid
14
+ graph TD
15
+ UserQuery([User Query]) --> Preprocess[Tokenize & Preprocess]
16
+ Preprocess --> DenseSearch[FAISS Dense Search<br/>all-MiniLM-L6-v2]
17
+ Preprocess --> SparseSearch[BM25 Sparse Search]
18
+
19
+ DenseSearch --> RetrieveTop25D[Retrieve Top 25 Chunks]
20
+ SparseSearch --> RetrieveTop25S[Retrieve Top 25 Chunks]
21
+
22
+ RetrieveTop25D --> MergeDedup[Merge & Deduplicate<br/>Max 50 Candidates]
23
+ RetrieveTop25S --> MergeDedup
24
+
25
+ MergeDedup --> CrossEncoder[Cross-Encoder Re-ranker<br/>ms-marco-MiniLM-L-6-v2]
26
+ CrossEncoder --> ScoreChunks[Score Candidates]
27
+ ScoreChunks --> SortTop5[Sort & Select Top 5 Chunks]
28
+
29
+ SortTop5 --> EvalRelevance{Top Score >= 0.4?}
30
+
31
+ EvalRelevance -- No: Irrelevant --> QueryExpand[Query Expansion<br/>LLM Rewrite / Thesaurus]
32
+ QueryExpand -->|Retry with new query| Preprocess
33
+
34
+ EvalRelevance -- Yes: Relevant --> LLMGen[Local LLM Response Generator<br/>TinyLlama-1.1B / Qwen-0.5B]
35
+
36
+ LLMGen --> GenAnswer[Generated Answer]
37
+
38
+ SortTop5 --> NLIGuard{NLI Hallucination Guard<br/>bart-large-mnli / deberta-v3}
39
+ GenAnswer --> NLIGuard
40
+
41
+ NLIGuard -- Contradiction/Neutral (Hallucination) --> AdjustPrompt[Adjust System Prompt &<br/>Increase Temperature]
42
+ AdjustPrompt -->|Regenerate Answer| LLMGen
43
+
44
+ NLIGuard -- Entailment (Valid) --> ReturnAnswer([Return Verified Answer])
45
+ ```
46
+
47
+ ---
48
+
49
+ ## 🌟 Key Features
50
+
51
+ * **Two-Stage Hybrid Retrieval**: Combines semantic embeddings (FAISS dense search) and keyword search (BM25 sparse search) to capture both context and specific jargon.
52
+ * **Cross-Encoder Re-ranking**: Uses a highly accurate `ms-marco-MiniLM-L-6-v2` re-ranker to score and filter candidate chunks down to the 5 most relevant.
53
+ * **Self-Correcting Retrieval Loop**: If the search quality falls below a relevance threshold (relevance score < 0.4), the engine executes **Query Expansion** (generating synonyms or rewriting the query using a local model) and searches again.
54
+ * **NLI Hallucination Guard**: Audits the LLM answer against the source documents using Natural Language Inference (`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.
55
+ * **Premium Glassmorphic Dashboard**: A modern, responsive single-page web UI built with HTML/CSS/JS that visualizes the pipeline telemetry, search candidate details, and NLI scores in real time.
56
+ * **100% Free Cloud Deployment**: Fully Dockerized and configured to run on Hugging Face Spaces (free CPU tier with 16GB RAM).
57
+
58
+ ---
59
+
60
+ ## πŸ’» Tech Stack
61
+
62
+ * **Backend**: FastAPI, Uvicorn (async/thread pools)
63
+ * **Vector Indexing**: FAISS (Facebook AI Similarity Search)
64
+ * **Keyword Search**: Rank-BM25 (BM25Okapi)
65
+ * **Embedding & Re-ranking**: Sentence-Transformers, Hugging Face Tokenizers
66
+ * **Generative Model**: Qwen2.5-0.5B-Instruct (CPU-optimized local LLM, interchangeable with TinyLlama-1.1B)
67
+ * **Verification Model**: DeBERTa-v3-xsmall NLI (interchangeable with BART-large-mnli)
68
+ * **Frontend**: HTML5, Vanilla CSS3 (Glassmorphism, Flexbox/Grid, custom micro-animations), Vanilla JavaScript ES6
69
+ * **Containerization**: Docker
70
+
71
+ ---
72
+
73
+ ## πŸš€ Local Quickstart
74
+
75
+ ### Prerequisites
76
+ * Python 3.10+
77
+ * Git
78
+
79
+ ### 1. Clone the repository and navigate inside
80
+ ```bash
81
+ git clone https://github.com/your-username/VigilantRAG.git
82
+ cd VigilantRAG
83
+ ```
84
+
85
+ ### 2. Create a virtual environment and activate it
86
+ ```bash
87
+ # Windows
88
+ python -m venv venv
89
+ venv\Scripts\activate
90
+
91
+ # macOS/Linux
92
+ python3 -m venv venv
93
+ source venv/bin/activate
94
+ ```
95
+
96
+ ### 3. Install dependencies
97
+ ```bash
98
+ pip install -r requirements.txt
99
+ ```
100
+
101
+ ### 4. Run the application
102
+ ```bash
103
+ python app.py
104
+ ```
105
+ Open `http://localhost:8000` in your web browser.
106
+
107
+ ---
108
+
109
+ ## 🐳 Running with Docker
110
+
111
+ You can run the entire self-contained environment (including models) inside a Docker container:
112
+
113
+ ```bash
114
+ # Build the image (this will pre-download the models into the image)
115
+ docker build -t vigilantrag .
116
+
117
+ # Run the container
118
+ docker run -p 8000:7860 vigilantrag
119
+ ```
120
+ Open `http://localhost:8000` to interact with the containerized application.
121
+
122
+ ---
123
+
124
+ ## πŸ§ͺ Testing
125
+
126
+ To run the automated `pytest` test suite:
127
+ ```bash
128
+ pytest
129
+ ```
130
+ This tests chunking thresholds, FAISS/BM25 merging, query expansion fallbacks, NLI score mapping, and FastAPI CRUD endpoints.
131
+
132
+ ---
133
+
134
+ ## πŸ“ Project Directory Structure
135
+
136
+ ```
137
+ VigilantRAG/
138
+ β”œβ”€β”€ src/
139
+ β”‚ β”œβ”€β”€ __init__.py
140
+ β”‚ β”œβ”€β”€ config.py # Global thresholds, model configurations
141
+ β”‚ β”œβ”€β”€ retriever.py # Chunking, FAISS and BM25 index managers
142
+ β”‚ β”œβ”€β”€ reranker.py # Cross-Encoder candidate scoring
143
+ β”‚ β”œβ”€β”€ query_expansion.py # Synonym dictionary & LLM rewrite callbacks
144
+ β”‚ β”œβ”€β”€ hallucination_guard.py# NLI model entailment checks
145
+ β”‚ β”œβ”€β”€ llm_client.py # Local LLM causal generation client
146
+ β”‚ └── engine.py # Orchestrates the RAG loop & telemetry
147
+ β”œβ”€β”€ static/ # Dashboard Frontend
148
+ β”‚ β”œβ”€β”€ index.html
149
+ β”‚ β”œβ”€β”€ style.css
150
+ β”‚ └── main.js
151
+ β”œβ”€β”€ tests/ # pytest automated suite
152
+ β”‚ β”œβ”€β”€ __init__.py
153
+ β”‚ β”œβ”€β”€ test_api.py
154
+ β”‚ β”œβ”€β”€ test_retriever.py
155
+ β”‚ β”œβ”€β”€ test_reranker.py
156
+ β”‚ β”œβ”€β”€ test_query_expansion.py
157
+ β”‚ └── test_hallucination_guard.py
158
+ β”œβ”€β”€ app.py # FastAPI server entry point
159
+ β”œβ”€β”€ download_models.py # Model pre-caching utility for builds
160
+ β”œβ”€β”€ requirements.txt # Python dependencies
161
+ └── Dockerfile # Deployment image manifest
162
+ ```
deployment_guide.md ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Deployment Guide: Host VigilantRAG Live on Hugging Face Spaces
2
+
3
+ This guide walks you through deploying **VigilantRAG** to **Hugging Face Spaces** for free in under 5 minutes. Pushing this to Hugging Face provides you with a public HTTPS link (e.g. `https://huggingface.co/spaces/your-username/VigilantRAG`) that recruiters can click directly to try your project.
4
+
5
+ ---
6
+
7
+ ## Why Hugging Face Spaces?
8
+
9
+ Standard free web hosts (like Render, Fly.io, or Railway) limit free tier memory to **512 MB RAM**. Loading PyTorch, Sentence-Transformers, and a local LLM will cause these hosts to crash immediately with Out-Of-Memory (OOM) errors.
10
+
11
+ Hugging Face Spaces offers a **100% Free CPU tier with 16 GB RAM and 2 vCPUs**. This is more than enough to load and execute our lightweight models (`Qwen2.5-0.5B` and `DeBERTa-v3-xsmall`) quickly.
12
+
13
+ ---
14
+
15
+ ## Step-by-Step Deployment
16
+
17
+ There are two ways to deploy: **Option A (Web Upload - easiest)** or **Option B (Git Push - professional)**.
18
+
19
+ ### Step 1: Create a Hugging Face Account & Space
20
+ 1. Go to [Hugging Face](https://huggingface.co) and sign up for a free account.
21
+ 2. Click on your profile picture in the top-right corner and select **"New Space"**.
22
+ 3. Fill in the following details:
23
+ * **Space Name**: `VigilantRAG` (or anything you prefer)
24
+ * **License**: `mit`
25
+ * **Select the Space SDK**: **Docker** (Very important!)
26
+ * **Docker Template**: **Blank**
27
+ * **Space Hardware**: **CPU Basic (Free β€’ 16GB RAM β€’ 2 vCPUs)**
28
+ * **Privacy**: **Public** (so recruiters can access it!)
29
+ 4. Click **"Create Space"**.
30
+
31
+ ---
32
+
33
+ ### Option A: Deploy using Web Interface (No Git needed)
34
+ If you don't want to use the command line, you can drag and drop your files:
35
+ 1. In your newly created Space, click on the **"Files"** tab.
36
+ 2. Click **"Add file"** -> **"Upload files"**.
37
+ 3. Drag and drop all the project files from your local folder `C:\projects_aryan\VigilantRAG` **except** the `venv/` folder and `test_data_cache/` / `test_api_cache/` if they exist.
38
+ 4. Ensure your folder structure on the website matches this:
39
+ ```
40
+ β”œβ”€β”€ src/
41
+ β”‚ β”œβ”€β”€ __init__.py
42
+ β”‚ β”œβ”€β”€ config.py
43
+ β”‚ β”œβ”€β”€ retriever.py
44
+ β”‚ β”œβ”€β”€ reranker.py
45
+ β”‚ β”œβ”€β”€ query_expansion.py
46
+ β”‚ β”œβ”€β”€ hallucination_guard.py
47
+ β”‚ β”œβ”€β”€ llm_client.py
48
+ β”‚ └── engine.py
49
+ β”œβ”€β”€ static/
50
+ β”‚ β”œβ”€β”€ index.html
51
+ β”‚ β”œβ”€β”€ style.css
52
+ β”‚ └── main.js
53
+ β”œβ”€β”€ app.py
54
+ β”œβ”€β”€ download_models.py
55
+ β”œβ”€β”€ requirements.txt
56
+ └── Dockerfile
57
+ ```
58
+ 5. Click **"Commit changes to main"** at the bottom of the page.
59
+ 6. Skip to **Step 2 (Building & Running)**.
60
+
61
+ ---
62
+
63
+ ### Option B: Deploy using Git Push (Recommended for Resume)
64
+ This demonstrates standard developer workflows:
65
+ 1. In your local terminal, navigate to your project directory:
66
+ ```bash
67
+ cd C:\projects_aryan\VigilantRAG
68
+ ```
69
+ 2. Initialize git and commit files:
70
+ ```bash
71
+ git init
72
+ git add .
73
+ git commit -m "feat: initial commit of VigilantRAG self-correcting engine"
74
+ ```
75
+ 3. Add the Hugging Face Space as a git remote. Hugging Face provides this exact command on your Space's landing page:
76
+ ```bash
77
+ git remote add origin https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
78
+ ```
79
+ 4. Push your code (you will need to input your Hugging Face username and a **User Access Token** as your password. Generate a token in your HF profile under Settings -> Access Tokens):
80
+ ```bash
81
+ git push -u origin main --force
82
+ ```
83
+
84
+ ---
85
+
86
+ ## Step 2: Building & Running
87
+
88
+ Once you commit or push your code:
89
+ 1. Go to the **"App"** tab of your Hugging Face Space.
90
+ 2. You will see a status badge: **"Building"**.
91
+ 3. Hugging Face is currently:
92
+ * Setting up the Linux environment.
93
+ * Installing PyTorch, FastAPI, FAISS, and other dependencies.
94
+ * Running `download_models.py` to pre-download the model weights and bake them directly into the container image.
95
+ 4. The build process takes about **5 to 8 minutes** (primarily downloading the 0.5B model weights).
96
+ 5. Once the build completes, the status badge will change to a green **"Running"**.
97
+ 6. The dashboard will render inside the Space, and you can copy the URL in your address bar and paste it directly onto your resume!