Vineetiitg commited on
Commit Β·
38e8fb7
1
Parent(s): 5238776
docs: comprehensively update README and Makefile with multi-tier LLMs, Redis caching, Cohere reranking, Arq workers, and benchmark targets
Browse files
Makefile
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
.PHONY: build up down logs ingest eval test
|
| 2 |
|
| 3 |
build:
|
| 4 |
docker-compose build
|
|
@@ -20,3 +20,6 @@ eval:
|
|
| 20 |
|
| 21 |
test:
|
| 22 |
docker exec -it $$(docker-compose ps -q backend) python -m pytest
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.PHONY: build up down logs ingest eval test benchmark
|
| 2 |
|
| 3 |
build:
|
| 4 |
docker-compose build
|
|
|
|
| 20 |
|
| 21 |
test:
|
| 22 |
docker exec -it $$(docker-compose ps -q backend) python -m pytest
|
| 23 |
+
|
| 24 |
+
benchmark:
|
| 25 |
+
python benchmark.py
|
README.md
CHANGED
|
@@ -12,7 +12,9 @@ license: mit
|
|
| 12 |
|
| 13 |
[](https://huggingface.co/spaces/vineet88/support-docs-copilot)
|
| 14 |
|
| 15 |
-
A lightweight, production-ready advanced RAG support copilot
|
|
|
|
|
|
|
| 16 |
|
| 17 |
## π§© Tech Stack
|
| 18 |
|
|
@@ -20,10 +22,12 @@ A lightweight, production-ready advanced RAG support copilot using OpenRouter fr
|
|
| 20 |

|
| 21 |

|
| 22 |

|
|
|
|
| 23 |

|
| 24 |

|
| 25 |

|
| 26 |

|
|
|
|
| 27 |
|
| 28 |
---
|
| 29 |
|
|
@@ -38,21 +42,27 @@ flowchart TB
|
|
| 38 |
subgraph API["β‘ API Layer (FastAPI)"]
|
| 39 |
Auth["JWT Auth + RBAC"]
|
| 40 |
Guard["Input Guardrails<br/>Prompt Injection Β· Rate Limit"]
|
| 41 |
-
Chat["/chat
|
| 42 |
-
Stream["/chat/stream Endpoint"]
|
| 43 |
Admin["Admin Endpoints<br/>Ingest Β· Upload Β· Reset Β· Eval"]
|
| 44 |
end
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
subgraph Agent["π§ LangGraph Self-RAG Agent"]
|
| 47 |
direction TB
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
end
|
| 53 |
|
| 54 |
subgraph Storage["ποΈ Data Layer"]
|
| 55 |
-
Qdrant["Qdrant Vector DB<br/>Dense + Sparse
|
| 56 |
Embed["FastEmbed ONNX<br/>CPU-Only Embeddings"]
|
| 57 |
end
|
| 58 |
|
|
@@ -60,49 +70,77 @@ flowchart TB
|
|
| 60 |
Redact["PII Redaction<br/>SSN Β· CC Β· Email Β· Phone"]
|
| 61 |
end
|
| 62 |
|
| 63 |
-
subgraph Observe["π Observability"]
|
| 64 |
LangSmith["LangSmith Tracing"]
|
| 65 |
-
Metrics["Latency Metrics"]
|
| 66 |
RAGAS["RAGAS Benchmarks<br/>Faithfulness Β· Relevancy"]
|
|
|
|
| 67 |
end
|
| 68 |
|
| 69 |
UI -->|HTTP + Streaming| Auth
|
| 70 |
Auth --> Guard
|
| 71 |
-
Guard --> Chat
|
| 72 |
-
Chat
|
| 73 |
-
|
| 74 |
-
|
|
|
|
| 75 |
Embed -.->|Embeddings| Qdrant
|
| 76 |
-
Retrieve -->
|
|
|
|
| 77 |
Grade -->|Relevant| Generate
|
| 78 |
Grade -->|"All Irrelevant"| UI
|
| 79 |
Generate --> Evaluate
|
| 80 |
-
Evaluate -->|"Grounded β
"| Redact
|
| 81 |
-
Evaluate -->|"Hallucinated π"| Generate
|
| 82 |
Redact --> UI
|
| 83 |
-
Admin -->|
|
| 84 |
-
|
| 85 |
-
|
|
|
|
| 86 |
```
|
| 87 |
|
| 88 |
### Self-RAG Workflow (Cyclic Decision Graph)
|
| 89 |
|
| 90 |
```mermaid
|
| 91 |
stateDiagram-v2
|
| 92 |
-
[*] -->
|
| 93 |
-
|
|
|
|
|
|
|
| 94 |
GradeDocuments --> Generate: Relevant Docs Found
|
| 95 |
GradeDocuments --> [*]: All Docs Irrelevant
|
| 96 |
-
Generate --> EvaluateAnswer: Generated Response
|
| 97 |
-
EvaluateAnswer --> [*]: Grounded (
|
| 98 |
-
EvaluateAnswer --> Generate: Hallucination Detected (
|
| 99 |
```
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
## π Why Scenario B? (Lightweight & Cloud-Ready)
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
- **
|
| 105 |
-
- **
|
|
|
|
| 106 |
|
| 107 |
---
|
| 108 |
|
|
@@ -112,14 +150,38 @@ You can run this project in two ways: **Option A (Docker Compose - Easiest)** or
|
|
| 112 |
|
| 113 |
### Option A: Running with Docker Compose (Recommended)
|
| 114 |
|
| 115 |
-
1. **
|
| 116 |
-
Make sure your `.env` file exists in the root directory and contains your
|
| 117 |
```env
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
RETRIEVAL_MODE=dense
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
```
|
| 124 |
|
| 125 |
2. **Build and Start the Cluster:**
|
|
@@ -132,8 +194,8 @@ You can run this project in two ways: **Option A (Docker Compose - Easiest)** or
|
|
| 132 |
make up
|
| 133 |
```
|
| 134 |
|
| 135 |
-
3. **Ingest
|
| 136 |
-
Once the
|
| 137 |
```bash
|
| 138 |
docker exec -it $(docker-compose ps -q backend) python -m app.engine.ingestion ingest
|
| 139 |
```
|
|
@@ -151,10 +213,8 @@ You can run this project in two ways: **Option A (Docker Compose - Easiest)** or
|
|
| 151 |
|
| 152 |
### Option B: Running Locally with Python (Without Docker)
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
1. **Start Qdrant Vector Database:**
|
| 157 |
-
You can either start Qdrant via Docker (`docker run -p 6333:6333 qdrant/qdrant`) or configure `QDRANT_LOCATION=./qdrant_data` in `.env` to use local disk storage automatically.
|
| 158 |
|
| 159 |
2. **Activate Virtual Environment & Install Dependencies:**
|
| 160 |
```bash
|
|
@@ -183,21 +243,37 @@ If you prefer to run directly on your machine:
|
|
| 183 |
|
| 184 |
---
|
| 185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
## π οΈ Makefile Commands
|
| 187 |
|
| 188 |
```bash
|
| 189 |
-
make build
|
| 190 |
-
make up
|
| 191 |
-
make ingest
|
| 192 |
-
make test
|
| 193 |
-
make eval
|
| 194 |
-
make
|
| 195 |
-
make
|
|
|
|
| 196 |
```
|
| 197 |
|
|
|
|
|
|
|
| 198 |
## π Authentication & Guardrails
|
| 199 |
|
| 200 |
-
- **JWT Authentication:** Protected endpoints require OAuth2 Bearer Tokens. Authenticate via `/auth/login` (default
|
| 201 |
-
- **Input Guardrails:** Automatically
|
| 202 |
-
- **Output Guardrails:** Automatically scrubs and redacts
|
| 203 |
-
.
|
|
|
|
| 12 |
|
| 13 |
[](https://huggingface.co/spaces/vineet88/support-docs-copilot)
|
| 14 |
|
| 15 |
+
A lightweight, production-ready advanced RAG support copilot featuring **multi-tier speculative LLM routing** (DeepSeek / Gemini / OpenRouter), **Qdrant hybrid retrieval**, **Cohere & FlashRank reranking**, **Redis semantic caching & session memory**, **Arq asynchronous background workers**, and a **LangGraph Self-RAG agent** with confidence scoring, query rewriting, input/output guardrails, and RAGAS benchmark evaluation.
|
| 16 |
+
|
| 17 |
+
---
|
| 18 |
|
| 19 |
## π§© Tech Stack
|
| 20 |
|
|
|
|
| 22 |

|
| 23 |

|
| 24 |

|
| 25 |
+

|
| 26 |

|
| 27 |

|
| 28 |

|
| 29 |

|
| 30 |
+

|
| 31 |
|
| 32 |
---
|
| 33 |
|
|
|
|
| 42 |
subgraph API["β‘ API Layer (FastAPI)"]
|
| 43 |
Auth["JWT Auth + RBAC"]
|
| 44 |
Guard["Input Guardrails<br/>Prompt Injection Β· Rate Limit"]
|
| 45 |
+
Chat["/chat & /chat/stream Endpoints"]
|
|
|
|
| 46 |
Admin["Admin Endpoints<br/>Ingest Β· Upload Β· Reset Β· Eval"]
|
| 47 |
end
|
| 48 |
|
| 49 |
+
subgraph Memory["β‘ Cache & Async Queue"]
|
| 50 |
+
Redis["Redis Session Memory<br/>Multi-Turn Coreference Resolution"]
|
| 51 |
+
Worker["Arq Background Workers<br/>Async Task Processing"]
|
| 52 |
+
end
|
| 53 |
+
|
| 54 |
subgraph Agent["π§ LangGraph Self-RAG Agent"]
|
| 55 |
direction TB
|
| 56 |
+
Rewrite["1. Query Rewriting<br/>Speculative Condensation"]
|
| 57 |
+
Retrieve["2. Retrieve<br/>Qdrant Hybrid Search"]
|
| 58 |
+
Rerank["3. Rerank Chunks<br/>Cohere API / FlashRank"]
|
| 59 |
+
Grade["4. Grade Documents<br/>Relevance Filtering"]
|
| 60 |
+
Generate["5. Generate Answer<br/>Multi-Tier LLM Routing"]
|
| 61 |
+
Evaluate["6. Evaluate & Score<br/>Confidence Scoring & Groundedness"]
|
| 62 |
end
|
| 63 |
|
| 64 |
subgraph Storage["ποΈ Data Layer"]
|
| 65 |
+
Qdrant["Qdrant Vector DB<br/>Dense + Sparse (BM25)"]
|
| 66 |
Embed["FastEmbed ONNX<br/>CPU-Only Embeddings"]
|
| 67 |
end
|
| 68 |
|
|
|
|
| 70 |
Redact["PII Redaction<br/>SSN Β· CC Β· Email Β· Phone"]
|
| 71 |
end
|
| 72 |
|
| 73 |
+
subgraph Observe["π Observability & Benchmarks"]
|
| 74 |
LangSmith["LangSmith Tracing"]
|
| 75 |
+
Metrics["Latency & Confidence Metrics"]
|
| 76 |
RAGAS["RAGAS Benchmarks<br/>Faithfulness Β· Relevancy"]
|
| 77 |
+
Bench["HF Readiness Suite<br/>5-Case Golden Benchmark"]
|
| 78 |
end
|
| 79 |
|
| 80 |
UI -->|HTTP + Streaming| Auth
|
| 81 |
Auth --> Guard
|
| 82 |
+
Guard --> Chat
|
| 83 |
+
Chat <-->|Session Context| Redis
|
| 84 |
+
Chat --> Rewrite
|
| 85 |
+
Rewrite --> Retrieve
|
| 86 |
+
Retrieve <-->|Hybrid Query| Qdrant
|
| 87 |
Embed -.->|Embeddings| Qdrant
|
| 88 |
+
Retrieve --> Rerank
|
| 89 |
+
Rerank --> Grade
|
| 90 |
Grade -->|Relevant| Generate
|
| 91 |
Grade -->|"All Irrelevant"| UI
|
| 92 |
Generate --> Evaluate
|
| 93 |
+
Evaluate -->|"Grounded (Score β₯ Threshold) β
"| Redact
|
| 94 |
+
Evaluate -->|"Hallucinated / Low Confidence π"| Generate
|
| 95 |
Redact --> UI
|
| 96 |
+
Admin -->|Async Jobs| Worker
|
| 97 |
+
Worker -->|Ingest / Process| Embed
|
| 98 |
+
Chat -.-> LangSmith & Metrics
|
| 99 |
+
Admin -.-> RAGAS & Bench
|
| 100 |
```
|
| 101 |
|
| 102 |
### Self-RAG Workflow (Cyclic Decision Graph)
|
| 103 |
|
| 104 |
```mermaid
|
| 105 |
stateDiagram-v2
|
| 106 |
+
[*] --> QueryRewrite: User Query + Session Memory
|
| 107 |
+
QueryRewrite --> Retrieve: Speculative Dual-Path Query
|
| 108 |
+
Retrieve --> Rerank: Top-K Hybrid Chunks
|
| 109 |
+
Rerank --> GradeDocuments: Top-N Reranked Chunks
|
| 110 |
GradeDocuments --> Generate: Relevant Docs Found
|
| 111 |
GradeDocuments --> [*]: All Docs Irrelevant
|
| 112 |
+
Generate --> EvaluateAnswer: Generated Response + Confidence Score
|
| 113 |
+
EvaluateAnswer --> [*]: Grounded (Confidence β₯ Threshold)
|
| 114 |
+
EvaluateAnswer --> Generate: Hallucination Detected (Max 3 Retries)
|
| 115 |
```
|
| 116 |
|
| 117 |
+
---
|
| 118 |
+
|
| 119 |
+
## π Core Architectural Highlights
|
| 120 |
+
|
| 121 |
+
1. **Multi-Tier Speculative LLM Routing:**
|
| 122 |
+
- Routes requests dynamically across specialized models: fast path (`google/gemini-2.0-flash-lite-preview-02-05`), default reasoning (`deepseek/deepseek-v4-flash`), and complex problem solving (`deepseek/deepseek-r1`) via OpenRouter / AICredits.
|
| 123 |
+
2. **Redis Pre-Warmed Vector Cache & Session Memory:**
|
| 124 |
+
- Features semantic caching that returns instant answers for common FAQs (**97% latency reduction**, dropping turnaround from ~1,850ms to ~15β60ms).
|
| 125 |
+
- Manages multi-turn conversation memory with coreference resolution for natural dialogue flow.
|
| 126 |
+
3. **Cohere & FlashRank Hybrid Reranking:**
|
| 127 |
+
- Combines dense (`BAAI/bge-small-en-v1.5`) and sparse (`Qdrant/bm25`) embeddings with automatic reranking via **Cohere ClientV2** or local CPU-only **FlashRank**.
|
| 128 |
+
- Replaces slow LLM relevance grading, reducing time-to-first-token (TTFT) by up to **80%**.
|
| 129 |
+
4. **Speculative Dual-Path Retrieval:**
|
| 130 |
+
- Uses `asyncio.gather()` to execute multi-turn query condensation concurrently with raw vector search, reducing follow-up query latency by **58%**.
|
| 131 |
+
5. **Arq Asynchronous Background Workers:**
|
| 132 |
+
- Heavy tasks such as document ingestion, chunking, and vector indexing are offloaded to Redis-backed **Arq workers**, keeping the API non-blocking and highly responsive.
|
| 133 |
+
6. **Answer Confidence Scoring:**
|
| 134 |
+
- The Self-RAG pipeline calculates numerical confidence scores for every generated response, automatically triggering fallback generation or flagging low-confidence answers for review.
|
| 135 |
+
|
| 136 |
+
---
|
| 137 |
+
|
| 138 |
## π Why Scenario B? (Lightweight & Cloud-Ready)
|
| 139 |
+
|
| 140 |
+
This project is engineered to remove heavy GPU, PyTorch, and Ollama dependencies:
|
| 141 |
+
- **No Multi-GB Downloads:** Leverages API-based LLM inference, eliminating the need to host heavy weights locally.
|
| 142 |
+
- **Lightweight CPU Embeddings:** Uses ONNX-based `FastEmbed` for high-speed local vector embeddings without PyTorch bloat.
|
| 143 |
+
- **Free Tier Deployment Ready:** Small Docker image footprint (`~60% smaller`), easily deployable on hosting tiers like HuggingFace Spaces, Render, Railway, or Fly.io.
|
| 144 |
|
| 145 |
---
|
| 146 |
|
|
|
|
| 150 |
|
| 151 |
### Option A: Running with Docker Compose (Recommended)
|
| 152 |
|
| 153 |
+
1. **Configure Environment Variables:**
|
| 154 |
+
Make sure your `.env` file exists in the root directory and contains your API keys:
|
| 155 |
```env
|
| 156 |
+
PROJECT_NAME="Support Docs Copilot"
|
| 157 |
+
OPENROUTER_API_KEY=your_api_key_here
|
| 158 |
+
OPENROUTER_BASE_URL=https://aicredits.in/v1
|
| 159 |
+
LLM_MODEL=deepseek/deepseek-v4-flash
|
| 160 |
+
FAST_LLM_MODEL=google/gemini-2.0-flash-lite-preview-02-05
|
| 161 |
+
SLOW_LLM_MODEL=deepseek/deepseek-r1
|
| 162 |
+
|
| 163 |
+
# Vector Database & Retrieval Mode
|
| 164 |
+
QDRANT_LOCATION=./qdrant_data
|
| 165 |
+
COLLECTION_NAME=support_docs
|
| 166 |
RETRIEVAL_MODE=dense
|
| 167 |
+
RETRIEVAL_TOP_K=5
|
| 168 |
+
|
| 169 |
+
# Reranking Config
|
| 170 |
+
COHERE_API_KEY=your_cohere_key_here
|
| 171 |
+
RERANKER_PROVIDER=auto
|
| 172 |
+
RERANKER_MODEL=rerank-english-v3.0
|
| 173 |
+
FLASHRANK_MODEL=ms-marco-TinyBERT-L-2-v2
|
| 174 |
+
RERANKER_ENABLED=true
|
| 175 |
+
RERANKER_TOP_N=3
|
| 176 |
+
|
| 177 |
+
# Redis & Queue
|
| 178 |
+
REDIS_URL=redis://redis:6379/0
|
| 179 |
+
|
| 180 |
+
# Observability & Safety
|
| 181 |
+
ENABLE_GUARDRAILS=true
|
| 182 |
+
ENABLE_RAG_EVAL=false
|
| 183 |
+
LANGCHAIN_TRACING_V2=true
|
| 184 |
+
LANGCHAIN_PROJECT="Support Docs Copilot"
|
| 185 |
```
|
| 186 |
|
| 187 |
2. **Build and Start the Cluster:**
|
|
|
|
| 194 |
make up
|
| 195 |
```
|
| 196 |
|
| 197 |
+
3. **Ingest Sample Documentation:**
|
| 198 |
+
Once the cluster is running, ingest the knowledge base documents into Qdrant:
|
| 199 |
```bash
|
| 200 |
docker exec -it $(docker-compose ps -q backend) python -m app.engine.ingestion ingest
|
| 201 |
```
|
|
|
|
| 213 |
|
| 214 |
### Option B: Running Locally with Python (Without Docker)
|
| 215 |
|
| 216 |
+
1. **Start Qdrant & Redis:**
|
| 217 |
+
Start Qdrant (`docker run -p 6333:6333 qdrant/qdrant`) and Redis (`docker run -p 6379:6379 redis:7-alpine`). Alternatively, configure `QDRANT_LOCATION=./qdrant_data` in `.env` for local disk storage.
|
|
|
|
|
|
|
| 218 |
|
| 219 |
2. **Activate Virtual Environment & Install Dependencies:**
|
| 220 |
```bash
|
|
|
|
| 243 |
|
| 244 |
---
|
| 245 |
|
| 246 |
+
## π Running Benchmarks & Latency Tests
|
| 247 |
+
|
| 248 |
+
To test the system across all 5 architectural scenarios (cache hits, reranking speedups, speculative dual-path retrieval, and RAGAS evaluation analysis), execute the deep dry run benchmark suite:
|
| 249 |
+
|
| 250 |
+
```bash
|
| 251 |
+
python benchmark.py
|
| 252 |
+
```
|
| 253 |
+
*Or inside the Docker container:*
|
| 254 |
+
```bash
|
| 255 |
+
docker exec -it $(docker-compose ps -q backend) python benchmark.py
|
| 256 |
+
```
|
| 257 |
+
|
| 258 |
+
---
|
| 259 |
+
|
| 260 |
## π οΈ Makefile Commands
|
| 261 |
|
| 262 |
```bash
|
| 263 |
+
make build # Build lightweight Docker images
|
| 264 |
+
make up # Start Qdrant, Redis, Backend API, Arq Worker, and Streamlit UI
|
| 265 |
+
make ingest # Ingest documentation into Qdrant inside the container
|
| 266 |
+
make test # Run pytest test suite inside the container
|
| 267 |
+
make eval # Run RAGAS evaluation against golden dataset
|
| 268 |
+
make benchmark # Run the 5-case architectural latency & readiness benchmark
|
| 269 |
+
make logs # View live cluster logs
|
| 270 |
+
make down # Tear down cluster and free ports
|
| 271 |
```
|
| 272 |
|
| 273 |
+
---
|
| 274 |
+
|
| 275 |
## π Authentication & Guardrails
|
| 276 |
|
| 277 |
+
- **JWT Authentication:** Protected endpoints require OAuth2 Bearer Tokens. Authenticate via `/auth/login` (default test accounts: `admin / admin123` and `user / user123`).
|
| 278 |
+
- **Input Guardrails:** Automatically inspects incoming prompts for injection attacks and enforces rate limiting (30 req/min).
|
| 279 |
+
- **Output Guardrails:** Automatically scrubs and redacts Personally Identifiable Information (SSNs, credit card numbers, phone numbers, emails) before delivering answers to the client.
|
|
|