Spaces:
Sleeping
Sleeping
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,710 +1,23 @@
|
|
| 1 |
-
# 🧠 Agentic Corrective RAG — Document Q&A with Self-Correction
|
| 2 |
-
|
| 3 |
-
<div align="center">
|
| 4 |
-
|
| 5 |
-
**Production-grade document retrieval system with self-correcting agent reasoning**
|
| 6 |
-
|
| 7 |
-
[](https://huggingface.co/spaces/Hitan2004/agentic-corrective-rag-ui)
|
| 8 |
-
[](https://huggingface.co/spaces/Hitan2004/agentic-corrective-rag)
|
| 9 |
-
[](https://hitan2004-agentic-corrective-rag.hf.space/docs)
|
| 10 |
-
[](https://github.com/Hitan547/agentic-corrective-rag)
|
| 11 |
-
[](#tech-stack)
|
| 12 |
-
|
| 13 |
-
*Upload documents, ask questions, get answers grounded in source material with automated hallucination detection and self-correction.*
|
| 14 |
-
|
| 15 |
-
## 🎯 Overview
|
| 16 |
-
|
| 17 |
-
Agentic Corrective RAG is a production-grade document Q&A system that combines advanced retrieval techniques with intelligent agent reasoning. Unlike naive RAG systems that often hallucinate, this system automatically validates every answer against source material and retries up to 3 times if validation fails.
|
| 18 |
-
|
| 19 |
-
### ⚡ Core Features
|
| 20 |
-
|
| 21 |
-
| Feature | Capability |
|
| 22 |
-
|---------|-----------|
|
| 23 |
-
| **Hybrid Retrieval** | FAISS semantic + BM25 keyword search with RRF fusion |
|
| 24 |
-
| **Intelligent Reranking** | Cross-encoder re-scores top-k candidates for precision |
|
| 25 |
-
| **Self-Correcting Agent** | LangGraph pipeline validates answers and auto-retries |
|
| 26 |
-
| **Hallucination Detection** | Second LLM call verifies every claim against context |
|
| 27 |
-
| **Session Memory** | Remembers last 5 conversation turns per session |
|
| 28 |
-
| **Streaming Ingestion** | Synchronous indexing with FAISS + BM25 persistence |
|
| 29 |
-
| **CI/CD Pipeline** | GitHub Actions with unit + integration test separation |
|
| 30 |
-
| **Multi-Service Deployment** | Backend API + separate frontend UI on HuggingFace Spaces |
|
| 31 |
-
|
| 32 |
-
---
|
| 33 |
-
|
| 34 |
-
## 🏗️ Architecture
|
| 35 |
-
|
| 36 |
-
### System Diagram
|
| 37 |
-
|
| 38 |
-
```
|
| 39 |
-
┌─────────────────────────────────────────────────────────┐
|
| 40 |
-
│ Agentic Corrective RAG Pipeline │
|
| 41 |
-
└─────────────────────────────────────────────────────────┘
|
| 42 |
-
|
| 43 |
-
Document Upload
|
| 44 |
-
↓
|
| 45 |
-
┌─────────────────────────────────────────┐
|
| 46 |
-
│ Ingestion Pipeline │
|
| 47 |
-
│ ┌─────────────────────────────────┐ │
|
| 48 |
-
│ │ PyMuPDF / TXT Parser │ │
|
| 49 |
-
│ │ Split into 512-token chunks │ │
|
| 50 |
-
│ │ 20-token overlap for context │ │
|
| 51 |
-
│ └────────────┬────────────────────┘ │
|
| 52 |
-
│ │ │
|
| 53 |
-
│ ┌────────────▼───────────────────┐ │
|
| 54 |
-
│ │ Embedding Generation │ │
|
| 55 |
-
│ │ all-MiniLM-L6-v2 (384-dim) │ │
|
| 56 |
-
│ └────────────┬───────────────────┘ │
|
| 57 |
-
│ │ │
|
| 58 |
-
│ ┌────────────▼──────────────────┐ │
|
| 59 |
-
│ │ Index Creation │ │
|
| 60 |
-
│ │ FAISS (dense vectors) │ │
|
| 61 |
-
│ │ BM25 (sparse inverted index) │ │
|
| 62 |
-
│ └──────────────────────────────┘ │
|
| 63 |
-
└─────────────────────────────────────────┘
|
| 64 |
-
|
| 65 |
-
Query Processing
|
| 66 |
-
↓
|
| 67 |
-
┌─────────────────────────────────────────┐
|
| 68 |
-
│ Hybrid Retrieval Pipeline │
|
| 69 |
-
│ │
|
| 70 |
-
│ ┌──────────┐ ┌──────────┐ │
|
| 71 |
-
│ │FAISS Top │ │BM25 Top │ │
|
| 72 |
-
│ │ 10 Hits │ │ 10 Hits │ │
|
| 73 |
-
│ └────┬─────┘ └────┬─────┘ │
|
| 74 |
-
│ └────────┬─────────┘ │
|
| 75 |
-
│ │ │
|
| 76 |
-
│ ┌───────▼──────────┐ │
|
| 77 |
-
│ │ RRF Fusion │ │
|
| 78 |
-
│ │ (Top 5 combined) │ │
|
| 79 |
-
│ └───────┬──────────┘ │
|
| 80 |
-
│ │ │
|
| 81 |
-
│ ┌───────▼──────────────────┐ │
|
| 82 |
-
│ │ Cross-Encoder Reranking │ │
|
| 83 |
-
│ │ ms-marco-MiniLM-L-6-v2 │ │
|
| 84 |
-
│ │ Re-score + sort │ │
|
| 85 |
-
│ └───────┬──────────────────┘ │
|
| 86 |
-
└─────────────────────────────────────────┘
|
| 87 |
-
|
| 88 |
-
Agent Reasoning Loop
|
| 89 |
-
↓
|
| 90 |
-
┌─────────────────────────────────────────┐
|
| 91 |
-
│ Corrective RAG Agent (LangGraph) │
|
| 92 |
-
│ │
|
| 93 |
-
│ Generate (LLaMA 3.3 70B) │
|
| 94 |
-
│ ├─ Answer using top-3 chunks │
|
| 95 |
-
│ └─ Confidence score │
|
| 96 |
-
│ ↓ │
|
| 97 |
-
│ Validate (LLM Validation Call) │
|
| 98 |
-
│ ├─ Is answer grounded? │
|
| 99 |
-
│ └─ All claims supported? │
|
| 100 |
-
│ ↓ │
|
| 101 |
-
│ Retry Logic (up to 3 times) │
|
| 102 |
-
│ ├─ If PASS → Return answer │
|
| 103 |
-
│ ├─ If FAIL & retries left: │
|
| 104 |
-
│ │ → Use failure reason as feedback │
|
| 105 |
-
│ │ → Re-retrieve with new query │
|
| 106 |
-
│ │ → Regenerate answer │
|
| 107 |
-
│ └─ If 3 retries exhausted → Return │
|
| 108 |
-
│ best attempt with FAIL verdict │
|
| 109 |
-
└─────────────────────────────────────────┘
|
| 110 |
-
|
| 111 |
-
Response
|
| 112 |
-
↓
|
| 113 |
-
JSON with:
|
| 114 |
-
- answer (generated text)
|
| 115 |
-
- source_chunks (exact matched context)
|
| 116 |
-
- validation_verdict (PASS/FAIL)
|
| 117 |
-
- retry_count (0-3)
|
| 118 |
-
- confidence (0.0-1.0)
|
| 119 |
-
```
|
| 120 |
-
|
| 121 |
-
### Component Breakdown
|
| 122 |
-
|
| 123 |
-
#### 1. **Ingestion (`ingestion.py`)**
|
| 124 |
-
Converts documents to searchable indexes
|
| 125 |
-
|
| 126 |
-
```python
|
| 127 |
-
def ingest_documents(file_path: str) -> Dict:
|
| 128 |
-
"""
|
| 129 |
-
Input: PDF or TXT file
|
| 130 |
-
Process:
|
| 131 |
-
1. Extract text with PyMuPDF or plain read
|
| 132 |
-
2. Split into 512-token chunks (20-token overlap)
|
| 133 |
-
3. Generate embeddings (all-MiniLM-L6-v2)
|
| 134 |
-
4. Create FAISS dense index
|
| 135 |
-
5. Create BM25 sparse index
|
| 136 |
-
Output: Ready for retrieval
|
| 137 |
-
"""
|
| 138 |
-
```
|
| 139 |
-
|
| 140 |
-
**Supported Formats:**
|
| 141 |
-
- PDF (single/multi-page)
|
| 142 |
-
- TXT (plain text)
|
| 143 |
-
- Auto-detects and routes to correct parser
|
| 144 |
-
|
| 145 |
-
#### 2. **Retriever (`retriever.py`)**
|
| 146 |
-
Hybrid search with intelligent ranking
|
| 147 |
-
|
| 148 |
-
```python
|
| 149 |
-
def hybrid_retrieve(query: str, k: int = 5) -> List[Chunk]:
|
| 150 |
-
"""
|
| 151 |
-
Process:
|
| 152 |
-
1. Dense retrieval: FAISS semantic search (top 10)
|
| 153 |
-
2. Sparse retrieval: BM25 keyword search (top 10)
|
| 154 |
-
3. RRF Fusion: Merge and rank by reciprocal rank
|
| 155 |
-
4. Cross-Encoder: Re-rank top-5 using semantic + lexical
|
| 156 |
-
Output: Top-k chunks with scores
|
| 157 |
-
"""
|
| 158 |
-
```
|
| 159 |
-
|
| 160 |
-
**Fusion Algorithm (RRF):**
|
| 161 |
-
```
|
| 162 |
-
For each document d:
|
| 163 |
-
score(d) = Σ(1 / (rank_dense(d) + k)) + Σ(1 / (rank_sparse(d) + k))
|
| 164 |
-
|
| 165 |
-
Where k=60 (typical offset to avoid division by zero)
|
| 166 |
-
```
|
| 167 |
-
|
| 168 |
-
#### 3. **Agent (`agent.py`)**
|
| 169 |
-
Self-correcting reasoning loop using LangGraph
|
| 170 |
-
|
| 171 |
-
```python
|
| 172 |
-
class CorrectiveRAGAgent:
|
| 173 |
-
"""
|
| 174 |
-
State machine with 4 nodes:
|
| 175 |
-
|
| 176 |
-
Generate Node:
|
| 177 |
-
- Takes query + top-3 chunks
|
| 178 |
-
- Calls LLaMA 3.3 70B
|
| 179 |
-
- Returns answer + initial confidence
|
| 180 |
-
|
| 181 |
-
Validate Node:
|
| 182 |
-
- Takes answer + source chunks
|
| 183 |
-
- Calls validation LLM (fact-checking)
|
| 184 |
-
- Checks: Is answer grounded? All claims supported?
|
| 185 |
-
- Returns verdict (PASS/FAIL)
|
| 186 |
-
|
| 187 |
-
Retry Logic:
|
| 188 |
-
- If PASS → End, return answer
|
| 189 |
-
- If FAIL and retry_count < 3:
|
| 190 |
-
→ Inform agent of failure reason
|
| 191 |
-
→ Re-retrieve with modified query
|
| 192 |
-
→ Regenerate answer
|
| 193 |
-
- If 3 retries exhausted → Return best attempt
|
| 194 |
-
|
| 195 |
-
Output Node:
|
| 196 |
-
- Formats response
|
| 197 |
-
- Includes source chunks
|
| 198 |
-
- Validation verdict
|
| 199 |
-
- Retry count
|
| 200 |
-
"""
|
| 201 |
-
```
|
| 202 |
-
|
| 203 |
-
#### 4. **FastAPI Backend (`main.py`)**
|
| 204 |
-
REST API orchestrating the full pipeline
|
| 205 |
-
|
| 206 |
-
```python
|
| 207 |
-
@app.post("/upload")
|
| 208 |
-
async def upload_document(file: UploadFile) -> Dict:
|
| 209 |
-
"""
|
| 210 |
-
- Receives PDF/TXT file
|
| 211 |
-
- Calls ingestion pipeline
|
| 212 |
-
- Returns: {status, message, doc_size, chunk_count}
|
| 213 |
-
"""
|
| 214 |
-
|
| 215 |
-
@app.post("/query")
|
| 216 |
-
async def query_documents(query: str, session_id: str) -> Dict:
|
| 217 |
-
"""
|
| 218 |
-
- Receives question
|
| 219 |
-
- Runs corrective agent
|
| 220 |
-
- Returns:
|
| 221 |
-
{
|
| 222 |
-
"answer": str,
|
| 223 |
-
"source_chunks": [chunk1, chunk2, chunk3],
|
| 224 |
-
"validation_verdict": "PASS" or "FAIL",
|
| 225 |
-
"retry_count": 0-3,
|
| 226 |
-
"confidence": 0.0-1.0
|
| 227 |
-
}
|
| 228 |
-
"""
|
| 229 |
-
```
|
| 230 |
-
|
| 231 |
-
---
|
| 232 |
-
|
| 233 |
-
## 🧪 Testing Architecture
|
| 234 |
-
|
| 235 |
-
### Unit Tests (`tests/test_unit.py`)
|
| 236 |
-
|
| 237 |
-
```python
|
| 238 |
-
✅ test_rrf_fusion
|
| 239 |
-
- Verifies Reciprocal Rank Fusion math
|
| 240 |
-
- Checks score normalization
|
| 241 |
-
|
| 242 |
-
✅ test_cross_encoder_reranking
|
| 243 |
-
- Validates reranking modifies order
|
| 244 |
-
- Confirms scores are properly scaled
|
| 245 |
-
|
| 246 |
-
✅ test_config_validation
|
| 247 |
-
- Ensures chunk_size > 0
|
| 248 |
-
- Validates max_retries in range
|
| 249 |
-
|
| 250 |
-
✅ test_chunk_processing
|
| 251 |
-
- Tests document splitting logic
|
| 252 |
-
- Checks overlap preservation
|
| 253 |
-
|
| 254 |
-
✅ test_agent_routing
|
| 255 |
-
- Verifies state machine transitions
|
| 256 |
-
- Confirms node execution order
|
| 257 |
-
```
|
| 258 |
-
|
| 259 |
-
**Run locally:**
|
| 260 |
-
```bash
|
| 261 |
-
pytest tests/test_unit.py -v
|
| 262 |
-
```
|
| 263 |
-
|
| 264 |
-
### Integration Tests (`tests/test_integration.py`)
|
| 265 |
-
|
| 266 |
-
```python
|
| 267 |
-
✅ test_full_pipeline_end_to_end
|
| 268 |
-
- Upload document
|
| 269 |
-
- Index with FAISS + BM25
|
| 270 |
-
- Query with agent
|
| 271 |
-
- Validate response structure
|
| 272 |
-
- Requires GROQ_API_KEY
|
| 273 |
-
|
| 274 |
-
✅ test_groq_api_connection
|
| 275 |
-
- Confirms Groq API is reachable
|
| 276 |
-
- Tests actual LLM inference
|
| 277 |
-
- Validates response format
|
| 278 |
-
|
| 279 |
-
✅ test_retrieval_quality
|
| 280 |
-
- Uploads test document
|
| 281 |
-
- Queries for information
|
| 282 |
-
- Verifies retrieved chunks contain answer
|
| 283 |
-
|
| 284 |
-
✅ test_agent_hallucination_detection
|
| 285 |
-
- Forces out-of-context query
|
| 286 |
-
- Confirms validation catches hallucination
|
| 287 |
-
- Checks retry mechanism
|
| 288 |
-
```
|
| 289 |
-
|
| 290 |
-
**Run locally (requires API key):**
|
| 291 |
-
```bash
|
| 292 |
-
export GROQ_API_KEY=your_key
|
| 293 |
-
pytest tests/test_integration.py -v -m integration
|
| 294 |
-
```
|
| 295 |
-
|
| 296 |
-
### CI/CD Test Strategy
|
| 297 |
-
|
| 298 |
-
**GitHub Actions:**
|
| 299 |
-
```yaml
|
| 300 |
-
on: [push, pull_request]
|
| 301 |
-
|
| 302 |
-
jobs:
|
| 303 |
-
test:
|
| 304 |
-
runs-on: ubuntu-latest
|
| 305 |
-
steps:
|
| 306 |
-
- uses: actions/checkout@v3
|
| 307 |
-
- uses: actions/setup-python@v4
|
| 308 |
-
- run: pip install -r requirements.txt
|
| 309 |
-
- run: pytest tests/test_unit.py -v
|
| 310 |
-
# ✅ Unit tests run (fast, no API)
|
| 311 |
-
- run: pytest tests/test_integration.py -v -m "not integration"
|
| 312 |
-
# ✅ Integration tests skip (expensive API calls)
|
| 313 |
-
```
|
| 314 |
-
|
| 315 |
-
**Key Insight:** Tests marked with `@pytest.mark.integration` are automatically skipped in CI but run locally with API key. This prevents wasting API credits while maintaining code quality.
|
| 316 |
-
|
| 317 |
---
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
| **Dense Embeddings** | `all-MiniLM-L6-v2` | 384-dim vectors, optimized for retrieval |
|
| 326 |
-
| **Sparse Search** | BM25 (rank-bm25 lib) | Keyword indexing, recall enhancement |
|
| 327 |
-
| **Reranker** | `cross-encoder/ms-marco-MiniLM-L-6-v2` | Semantic + lexical re-scoring |
|
| 328 |
-
|
| 329 |
-
### Reasoning Engine
|
| 330 |
-
|
| 331 |
-
| Component | Model | Role |
|
| 332 |
-
|-----------|-------|------|
|
| 333 |
-
| **Main Generator** | LLaMA 3.3 70B (Groq API) | Answer generation from context |
|
| 334 |
-
| **Validator** | LLaMA 3.3 70B (Groq API) | Hallucination detection & fact-checking |
|
| 335 |
-
|
| 336 |
-
### Why These Choices?
|
| 337 |
-
|
| 338 |
-
✅ **all-MiniLM-L6-v2**
|
| 339 |
-
- 384-dim embeddings (good balance of size/quality)
|
| 340 |
-
- Specifically trained for retrieval tasks
|
| 341 |
-
- Fast inference, low memory
|
| 342 |
-
|
| 343 |
-
✅ **BM25**
|
| 344 |
-
- Complementary to dense embeddings (catches keyword matches)
|
| 345 |
-
- Sparse representation (memory efficient)
|
| 346 |
-
- Proven effective in hybrid search
|
| 347 |
-
|
| 348 |
-
✅ **Cross-Encoder Reranking**
|
| 349 |
-
- Reads query + chunk together (interaction model)
|
| 350 |
-
- Higher precision than encoding separately
|
| 351 |
-
- Scales to top-k reranking
|
| 352 |
-
|
| 353 |
-
✅ **LLaMA 3.3 70B via Groq**
|
| 354 |
-
- Strong reasoning on diverse topics
|
| 355 |
-
- Fast inference (Groq's optimized runtime)
|
| 356 |
-
- Production-grade availability
|
| 357 |
-
- Cost-effective for hobby projects
|
| 358 |
-
|
| 359 |
-
---
|
| 360 |
-
|
| 361 |
-
## 🚀 Quick Start
|
| 362 |
-
|
| 363 |
-
### Prerequisites
|
| 364 |
-
- Python 3.10+
|
| 365 |
-
- Free Groq API key (from console.groq.com)
|
| 366 |
-
- 1GB disk for models + indexes
|
| 367 |
-
|
| 368 |
-
### Local Setup (10 minutes)
|
| 369 |
-
|
| 370 |
-
```bash
|
| 371 |
-
# 1. Clone repository
|
| 372 |
-
git clone https://github.com/Hitan547/agentic-corrective-rag.git
|
| 373 |
-
cd agentic-corrective-rag
|
| 374 |
-
|
| 375 |
-
# 2. Create virtual environment
|
| 376 |
-
python -m venv venv
|
| 377 |
-
source venv/bin/activate # Windows: venv\Scripts\activate
|
| 378 |
-
|
| 379 |
-
# 3. Install dependencies
|
| 380 |
-
pip install -r requirements.txt
|
| 381 |
-
|
| 382 |
-
# 4. Set up environment
|
| 383 |
-
echo "GROQ_API_KEY=your_api_key_here" > .env
|
| 384 |
-
|
| 385 |
-
# 5. Run backend
|
| 386 |
-
uvicorn main:app --reload --port 8000
|
| 387 |
-
|
| 388 |
-
# 6. In another terminal, serve frontend
|
| 389 |
-
python -m http.server 3000 --directory ui
|
| 390 |
-
|
| 391 |
-
# 7. Open browser
|
| 392 |
-
# → http://localhost:3000/index.html
|
| 393 |
-
```
|
| 394 |
-
|
| 395 |
-
### Docker Setup
|
| 396 |
-
|
| 397 |
-
```bash
|
| 398 |
-
# Build
|
| 399 |
-
docker build -t agentic-rag:latest .
|
| 400 |
-
|
| 401 |
-
# Run
|
| 402 |
-
docker run -e GROQ_API_KEY=your_key -p 8000:8000 agentic-rag:latest
|
| 403 |
-
|
| 404 |
-
# Access at http://localhost:8000
|
| 405 |
-
```
|
| 406 |
-
|
| 407 |
-
### HuggingFace Spaces Deployment
|
| 408 |
-
|
| 409 |
-
**Backend Space:**
|
| 410 |
-
1. Create new Space (Python)
|
| 411 |
-
2. Add secret: `GROQ_API_KEY`
|
| 412 |
-
3. Push repo (includes Dockerfile)
|
| 413 |
-
4. Auto-deploys as FastAPI service
|
| 414 |
-
|
| 415 |
-
**Frontend Space:**
|
| 416 |
-
1. Create new Space (Static)
|
| 417 |
-
2. Push `ui/` directory
|
| 418 |
-
3. Serves HTML directly
|
| 419 |
-
|
| 420 |
-
---
|
| 421 |
-
|
| 422 |
-
## 🔌 REST API Reference
|
| 423 |
-
|
| 424 |
-
### GET `/health`
|
| 425 |
-
System health check
|
| 426 |
-
|
| 427 |
-
**Response:**
|
| 428 |
-
```json
|
| 429 |
-
{
|
| 430 |
-
"status": "online",
|
| 431 |
-
"model": "corrective-rag-v1",
|
| 432 |
-
"indexes": {
|
| 433 |
-
"faiss": "ready",
|
| 434 |
-
"bm25": "ready"
|
| 435 |
-
},
|
| 436 |
-
"sessions": 42
|
| 437 |
-
}
|
| 438 |
-
```
|
| 439 |
-
|
| 440 |
-
### POST `/upload`
|
| 441 |
-
Upload and index a document
|
| 442 |
-
|
| 443 |
-
**Request:**
|
| 444 |
-
```bash
|
| 445 |
-
curl -X POST \
|
| 446 |
-
-F "file=@document.pdf" \
|
| 447 |
-
http://localhost:8000/upload
|
| 448 |
-
```
|
| 449 |
-
|
| 450 |
-
**Response:**
|
| 451 |
-
```json
|
| 452 |
-
{
|
| 453 |
-
"status": "success",
|
| 454 |
-
"message": "Document indexed successfully",
|
| 455 |
-
"doc_name": "document.pdf",
|
| 456 |
-
"chunk_count": 24,
|
| 457 |
-
"token_count": 12345,
|
| 458 |
-
"file_size_bytes": 2048000
|
| 459 |
-
}
|
| 460 |
-
```
|
| 461 |
-
|
| 462 |
-
### POST `/query`
|
| 463 |
-
Ask a question about uploaded documents
|
| 464 |
-
|
| 465 |
-
**Request:**
|
| 466 |
-
```json
|
| 467 |
-
{
|
| 468 |
-
"query": "What is the main thesis?",
|
| 469 |
-
"session_id": "user_123",
|
| 470 |
-
"temperature": 0.7,
|
| 471 |
-
"max_retries": 3
|
| 472 |
-
}
|
| 473 |
-
```
|
| 474 |
-
|
| 475 |
-
**Response:**
|
| 476 |
-
```json
|
| 477 |
-
{
|
| 478 |
-
"answer": "The main thesis argues that...",
|
| 479 |
-
"source_chunks": [
|
| 480 |
-
{
|
| 481 |
-
"text": "The thesis states that...",
|
| 482 |
-
"chunk_id": 3,
|
| 483 |
-
"score": 0.92
|
| 484 |
-
},
|
| 485 |
-
{
|
| 486 |
-
"text": "This is supported by...",
|
| 487 |
-
"chunk_id": 5,
|
| 488 |
-
"score": 0.87
|
| 489 |
-
}
|
| 490 |
-
],
|
| 491 |
-
"validation_verdict": "PASS",
|
| 492 |
-
"retry_count": 0,
|
| 493 |
-
"confidence": 0.94,
|
| 494 |
-
"processing_time_ms": 3200
|
| 495 |
-
}
|
| 496 |
-
```
|
| 497 |
-
|
| 498 |
-
### DELETE `/session/{id}`
|
| 499 |
-
Clear conversation history for a session
|
| 500 |
-
|
| 501 |
-
**Response:**
|
| 502 |
-
```json
|
| 503 |
-
{
|
| 504 |
-
"status": "success",
|
| 505 |
-
"message": "Session cleared"
|
| 506 |
-
}
|
| 507 |
-
```
|
| 508 |
-
|
| 509 |
-
### GET `/docs`
|
| 510 |
-
Interactive Swagger UI
|
| 511 |
-
|
| 512 |
-
Navigate to: `http://localhost:8000/docs`
|
| 513 |
-
|
| 514 |
-
---
|
| 515 |
-
|
| 516 |
-
## 📁 Project Structure
|
| 517 |
-
|
| 518 |
-
```
|
| 519 |
-
agentic-corrective-rag/
|
| 520 |
-
├── agent.py
|
| 521 |
-
│ └── CorrectiveRAGAgent
|
| 522 |
-
│ ├── generate(query, chunks) → answer
|
| 523 |
-
│ ├── validate(answer, chunks) → verdict
|
| 524 |
-
│ └── retry_loop() → final_answer
|
| 525 |
-
├── retriever.py
|
| 526 |
-
│ ├── hybrid_retrieve() → RRF + reranking
|
| 527 |
-
│ ├── faiss_search() → dense vectors
|
| 528 |
-
│ └── bm25_search() → keyword search
|
| 529 |
-
├── ingestion.py
|
| 530 |
-
│ ├── ingest_pdf()
|
| 531 |
-
│ ├── ingest_txt()
|
| 532 |
-
│ └── create_indexes() → FAISS + BM25
|
| 533 |
-
├── main.py
|
| 534 |
-
│ ├── FastAPI app
|
| 535 |
-
│ ├── /upload endpoint
|
| 536 |
-
│ ├── /query endpoint
|
| 537 |
-
│ └── /session/{id} endpoint
|
| 538 |
-
├── config.py
|
| 539 |
-
│ ├── CHUNK_SIZE = 512
|
| 540 |
-
│ ├── CHUNK_OVERLAP = 20
|
| 541 |
-
│ ├── MAX_RETRIES = 3
|
| 542 |
-
│ └── MODEL_PARAMS = {...}
|
| 543 |
-
├── requirements.txt
|
| 544 |
-
├── Dockerfile
|
| 545 |
-
├── .github/workflows/ci.yml
|
| 546 |
-
├── ui/
|
| 547 |
-
│ └── index.html (static HTML/JS frontend)
|
| 548 |
-
├── tests/
|
| 549 |
-
│ ├── test_unit.py
|
| 550 |
-
│ │ ├── test_rrf_fusion
|
| 551 |
-
│ │ ├── test_cross_encoder_reranking
|
| 552 |
-
│ │ └── test_config_validation
|
| 553 |
-
│ └── test_integration.py
|
| 554 |
-
│ ├── test_full_pipeline_end_to_end
|
| 555 |
-
│ ├── test_groq_api_connection
|
| 556 |
-
│ └── test_agent_hallucination_detection
|
| 557 |
-
└── README.md
|
| 558 |
-
```
|
| 559 |
-
|
| 560 |
-
---
|
| 561 |
-
|
| 562 |
-
## 🔄 CI/CD Pipeline
|
| 563 |
-
|
| 564 |
-
### GitHub Actions Workflow
|
| 565 |
-
|
| 566 |
-
**Trigger:** Push to main or PR
|
| 567 |
-
|
| 568 |
-
```yaml
|
| 569 |
-
jobs:
|
| 570 |
-
test:
|
| 571 |
-
runs-on: ubuntu-latest
|
| 572 |
-
|
| 573 |
-
steps:
|
| 574 |
-
- uses: actions/checkout@v3
|
| 575 |
-
- uses: actions/setup-python@v4
|
| 576 |
-
with:
|
| 577 |
-
python-version: '3.10'
|
| 578 |
-
|
| 579 |
-
- name: Install dependencies
|
| 580 |
-
run: pip install -r requirements.txt
|
| 581 |
-
|
| 582 |
-
- name: Run unit tests
|
| 583 |
-
run: pytest tests/test_unit.py -v
|
| 584 |
-
# ✅ Fast tests, no external API calls
|
| 585 |
-
|
| 586 |
-
- name: Skip integration tests in CI
|
| 587 |
-
run: pytest tests/test_integration.py -v -m "not integration"
|
| 588 |
-
# ✅ Prevents wasting Groq API credits
|
| 589 |
-
|
| 590 |
-
- name: Docker build test
|
| 591 |
-
run: docker build -t agentic-rag:test .
|
| 592 |
-
# ✅ Ensures Dockerfile is valid
|
| 593 |
-
```
|
| 594 |
-
|
| 595 |
-
### Deployment Pipeline
|
| 596 |
-
|
| 597 |
-
**Backend (API Service):**
|
| 598 |
-
1. HuggingFace Space (Docker runtime)
|
| 599 |
-
2. Auto-deploys on push to `main`
|
| 600 |
-
3. Exposes FastAPI at `https://hitan2004-agentic-corrective-rag.hf.space`
|
| 601 |
-
|
| 602 |
-
**Frontend (Static Service):**
|
| 603 |
-
1. HuggingFace Space (Static runtime)
|
| 604 |
-
2. Auto-deploys on push to `main`
|
| 605 |
-
3. Serves HTML at `https://hitan2004-agentic-corrective-rag-ui.hf.space`
|
| 606 |
-
|
| 607 |
-
---
|
| 608 |
-
|
| 609 |
-
## 🎓 What I Learned
|
| 610 |
-
|
| 611 |
-
✅ **Advanced Retrieval**
|
| 612 |
-
- Hybrid search (dense + sparse) outperforms single modality
|
| 613 |
-
- RRF fusion effectively combines different ranking signals
|
| 614 |
-
- Cross-encoders improve precision over bi-encoders
|
| 615 |
-
- Trade-off: reranking adds latency but improves quality
|
| 616 |
-
|
| 617 |
-
✅ **Agent-Based Reasoning**
|
| 618 |
-
- State machines (LangGraph) cleanly express retry logic
|
| 619 |
-
- Validation is critical for production RAG systems
|
| 620 |
-
- Feedback loops enable graceful degradation
|
| 621 |
-
- Session memory prevents repeated errors
|
| 622 |
-
|
| 623 |
-
✅ **Production ML System Design**
|
| 624 |
-
- Test separation (unit vs. integration) reduces CI/CD costs
|
| 625 |
-
- Configuration as code improves reproducibility
|
| 626 |
-
- Synchronous indexing ensures consistency
|
| 627 |
-
- Proper error handling for external API calls
|
| 628 |
-
|
| 629 |
-
✅ **LLM Integration**
|
| 630 |
-
- Groq API's speed enables interactive applications
|
| 631 |
-
- Temperature tuning affects consistency vs. creativity
|
| 632 |
-
- Prompt engineering for specific tasks (validation vs. generation)
|
| 633 |
-
- Cost-benefit of multi-turn API calls
|
| 634 |
-
|
| 635 |
-
✅ **Full-Stack Web Development**
|
| 636 |
-
- FastAPI for modern async backends
|
| 637 |
-
- Static HTML/JS for simple UIs
|
| 638 |
-
- Docker for reproducible deployments
|
| 639 |
-
- GitHub Actions for automated testing and CI/CD
|
| 640 |
-
|
| 641 |
-
---
|
| 642 |
-
|
| 643 |
-
## 📈 Performance Metrics
|
| 644 |
-
|
| 645 |
-
### Retrieval Quality
|
| 646 |
-
|
| 647 |
-
| Scenario | Metric | Value |
|
| 648 |
-
|----------|--------|-------|
|
| 649 |
-
| Exact answer in docs | Recall@3 | 94% |
|
| 650 |
-
| Paraphrased answer | Recall@5 | 87% |
|
| 651 |
-
| Complex multi-doc answer | Recall@10 | 92% |
|
| 652 |
-
|
| 653 |
-
### Agent Performance
|
| 654 |
-
|
| 655 |
-
| Metric | Value |
|
| 656 |
-
|--------|-------|
|
| 657 |
-
| Validation PASS rate (correct answers) | 97% |
|
| 658 |
-
| Hallucination detection rate | 94% |
|
| 659 |
-
| Avg retries (when needed) | 1.2 |
|
| 660 |
-
| Zero-shot success (no retries) | 89% |
|
| 661 |
-
|
| 662 |
-
### Latency (end-to-end, on Groq API)
|
| 663 |
-
|
| 664 |
-
| Operation | Time |
|
| 665 |
-
|-----------|------|
|
| 666 |
-
| Hybrid retrieval | 200ms |
|
| 667 |
-
| Reranking (top-10) | 150ms |
|
| 668 |
-
| LLM generation | 1500ms |
|
| 669 |
-
| Validation call | 1200ms |
|
| 670 |
-
| **Total (no retries)** | **3050ms** |
|
| 671 |
-
|
| 672 |
-
---
|
| 673 |
-
|
| 674 |
-
## 🤝 Contributing
|
| 675 |
-
|
| 676 |
-
This is a portfolio project. Contributions are welcome!
|
| 677 |
-
|
| 678 |
-
**Ideas for enhancement:**
|
| 679 |
-
- [ ] Add multi-document support (merge indexes)
|
| 680 |
-
- [ ] Implement persistent vector DB (Pinecone/Weaviate)
|
| 681 |
-
- [ ] Add citation highlighting in frontend
|
| 682 |
-
- [ ] Implement streaming responses with Server-Sent Events
|
| 683 |
-
- [ ] Add support for images (multimodal embeddings)
|
| 684 |
-
|
| 685 |
-
---
|
| 686 |
-
|
| 687 |
-
## 📜 License
|
| 688 |
-
|
| 689 |
-
MIT License — Use freely for learning or commercial purposes.
|
| 690 |
-
|
| 691 |
-
---
|
| 692 |
-
|
| 693 |
-
## 📞 Contact
|
| 694 |
-
|
| 695 |
-
**Hitan K** — AI Systems Engineer
|
| 696 |
-
|
| 697 |
-
- 🔗 [LinkedIn](https://linkedin.com/in/hitan-k)
|
| 698 |
-
- 🐙 [GitHub](https://github.com/Hitan547)
|
| 699 |
-
- 🤗 [HuggingFace](https://huggingface.co/Hitan2004)
|
| 700 |
-
- 📧 [Email](mailto:hitan.k@outlook.com)
|
| 701 |
-
|
| 702 |
---
|
| 703 |
|
| 704 |
-
|
| 705 |
|
| 706 |
-
|
| 707 |
|
| 708 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 709 |
|
| 710 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
---
|
| 10 |
|
| 11 |
+
# 🤖 Agentic Corrective RAG
|
| 12 |
|
| 13 |
+
AI-powered Retrieval-Augmented Generation system with self-correcting agent loop.
|
| 14 |
|
| 15 |
+
## Features
|
| 16 |
+
- Hybrid Retrieval (FAISS + BM25)
|
| 17 |
+
- Cross-Encoder Reranking
|
| 18 |
+
- Agent-based reasoning with retries
|
| 19 |
+
- Groq LLM integration
|
| 20 |
+
- CI/CD with GitHub Actions
|
| 21 |
|
| 22 |
+
## Usage
|
| 23 |
+
Upload documents and ask questions to get accurate answers with sources.
|