Update README.md
Browse files
README.md
CHANGED
|
@@ -1,116 +1,171 @@
|
|
| 1 |
-
📄 Gemini RAG
|
| 2 |
|
| 3 |
-
|
| 4 |
|
| 5 |
-
This
|
| 6 |
|
| 7 |
-
🚀
|
| 8 |
|
| 9 |
-
|
| 10 |
|
| 11 |
-
|
| 12 |
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
| 16 |
|
| 17 |
-
|
| 18 |
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
↓
|
| 30 |
FastAPI Backend
|
| 31 |
↓
|
| 32 |
Document Ingestion (PDF / TXT)
|
| 33 |
↓
|
|
|
|
|
|
|
| 34 |
Embeddings (SentenceTransformers)
|
| 35 |
↓
|
| 36 |
-
FAISS
|
| 37 |
↓
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
↓
|
| 40 |
Prompt Assembly
|
| 41 |
↓
|
| 42 |
Google Gemini LLM
|
| 43 |
↓
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
1. FastAPI Fundamentals
|
| 48 |
-
|
| 49 |
-
GET and POST endpoints
|
| 50 |
-
|
| 51 |
-
Request/response lifecycle
|
| 52 |
-
|
| 53 |
-
Input validation using Pydantic models
|
| 54 |
-
|
| 55 |
-
Async endpoints for non-blocking LLM calls
|
| 56 |
-
|
| 57 |
-
2. Real LLM Integration
|
| 58 |
-
|
| 59 |
-
Secure API key handling via environment variables
|
| 60 |
-
|
| 61 |
-
Structured prompts for strict input/output control
|
| 62 |
-
|
| 63 |
-
Handling rate limits and safety-filtered responses
|
| 64 |
-
|
| 65 |
-
Graceful error handling and fallbacks
|
| 66 |
-
|
| 67 |
-
3. Retrieval-Augmented Generation (RAG)
|
| 68 |
|
| 69 |
-
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
|
| 73 |
-
|
| 74 |
|
| 75 |
-
|
| 76 |
|
| 77 |
-
|
| 78 |
|
| 79 |
-
|
| 80 |
|
| 81 |
-
|
| 82 |
|
| 83 |
-
|
| 84 |
|
| 85 |
-
|
| 86 |
|
| 87 |
-
|
| 88 |
|
| 89 |
-
|
| 90 |
|
| 91 |
-
|
| 92 |
|
| 93 |
-
|
| 94 |
|
| 95 |
-
|
| 96 |
|
| 97 |
-
|
| 98 |
|
| 99 |
-
|
| 100 |
|
| 101 |
-
|
| 102 |
|
| 103 |
-
|
| 104 |
|
| 105 |
-
|
| 106 |
|
| 107 |
-
|
| 108 |
|
| 109 |
-
|
| 110 |
|
| 111 |
-
|
| 112 |
|
| 113 |
-
|
| 114 |
|
| 115 |
🛠️ Tech Stack
|
| 116 |
Backend
|
|
@@ -119,10 +174,12 @@ Python
|
|
| 119 |
|
| 120 |
FastAPI
|
| 121 |
|
| 122 |
-
FAISS
|
| 123 |
|
| 124 |
SentenceTransformers
|
| 125 |
|
|
|
|
|
|
|
| 126 |
Google Gemini API
|
| 127 |
|
| 128 |
PyPDF
|
|
@@ -137,73 +194,49 @@ CSS
|
|
| 137 |
|
| 138 |
Vanilla JavaScript (Fetch API)
|
| 139 |
|
| 140 |
-
|
| 141 |
|
| 142 |
VS Code
|
| 143 |
|
| 144 |
Git & GitHub
|
| 145 |
|
|
|
|
|
|
|
| 146 |
Hugging Face Spaces (deployment)
|
| 147 |
|
| 148 |
Virtual Environments (venv)
|
| 149 |
|
| 150 |
-
⚙️ Setup
|
| 151 |
-
1️⃣ Clone the repository
|
| 152 |
-
git clone https://github.com/your-username/your-repo-name.git
|
| 153 |
-
cd your-repo-name
|
| 154 |
|
| 155 |
-
|
|
|
|
|
|
|
| 156 |
python -m venv venv
|
| 157 |
-
|
| 158 |
-
venv\Scripts\activate # Windows
|
| 159 |
-
|
| 160 |
-
3️⃣ Install dependencies
|
| 161 |
pip install -r requirements.txt
|
| 162 |
-
|
| 163 |
-
4️⃣ Set environment variables
|
| 164 |
-
|
| 165 |
-
Create a .env file:
|
| 166 |
-
|
| 167 |
GEMINI_API_KEY=your_api_key_here
|
| 168 |
-
|
| 169 |
-
5️⃣ Run the server
|
| 170 |
uvicorn main:app --reload
|
| 171 |
|
| 172 |
-
|
| 173 |
-
Open in browser:
|
| 174 |
-
|
| 175 |
-
http://127.0.0.1:8000
|
| 176 |
-
|
| 177 |
-
Test and use my RAG project on Hugging face : https://huggingface.co/spaces/lvvignesh2122/Gemini-Rag-Fastapi-Pro
|
| 178 |
-
|
| 179 |
-
🧪 Mock Mode (Development)
|
| 180 |
-
|
| 181 |
-
To test the UI without consuming Gemini API quota:
|
| 182 |
-
|
| 183 |
-
Enable mock responses in main.py
|
| 184 |
-
|
| 185 |
-
Allows frontend and flow testing without LLM calls
|
| 186 |
-
|
| 187 |
-
This mirrors real production workflows.
|
| 188 |
-
|
| 189 |
⚠️ Known Limitations
|
| 190 |
|
| 191 |
-
Scanned/image-
|
|
|
|
|
|
|
| 192 |
|
| 193 |
-
|
| 194 |
|
| 195 |
-
|
| 196 |
|
| 197 |
-
|
| 198 |
|
| 199 |
-
|
| 200 |
|
| 201 |
-
|
| 202 |
|
| 203 |
-
|
|
|
|
| 204 |
|
| 205 |
-
|
| 206 |
|
| 207 |
-
|
| 208 |
|
| 209 |
-
Auth & user-specific document stores
|
|
|
|
| 1 |
+
📄 Gemini RAG Backend System (FastAPI)
|
| 2 |
|
| 3 |
+
Production-grade Retrieval-Augmented Generation (RAG) backend built with FastAPI, FAISS (ANN), and Google Gemini — featuring hybrid retrieval, HNSW indexing, cross-encoder reranking, evaluation logging, and analytics.
|
| 4 |
|
| 5 |
+
This repository demonstrates how modern AI backend systems are actually built in industry, not toy demos.
|
| 6 |
|
| 7 |
+
🚀 What This Project Is
|
| 8 |
|
| 9 |
+
This is a full RAG backend system that:
|
| 10 |
|
| 11 |
+
Ingests large PDF/TXT documents
|
| 12 |
|
| 13 |
+
Builds vector indexes with Approximate Nearest Neighbor (ANN) search
|
| 14 |
|
| 15 |
+
Answers questions using grounded LLM responses
|
| 16 |
|
| 17 |
+
Tracks confidence, known/unknown answers, and usage analytics
|
| 18 |
|
| 19 |
+
Supports production constraints (file limits, caching, logging)
|
| 20 |
|
| 21 |
+
The project evolved from RAG v1 → RAG v2, adding real-world scalability and observability.
|
| 22 |
|
| 23 |
+
✨ Key Features (RAG v2)
|
| 24 |
+
📥 Document Ingestion
|
| 25 |
|
| 26 |
+
Upload PDF and TXT files
|
| 27 |
|
| 28 |
+
Sentence-aware chunking with overlap
|
| 29 |
+
|
| 30 |
+
Page-level metadata for citations
|
| 31 |
+
|
| 32 |
+
🔍 Retrieval (Hybrid + ANN)
|
| 33 |
+
|
| 34 |
+
FAISS HNSW ANN index for scalable similarity search
|
| 35 |
+
|
| 36 |
+
Cosine similarity via normalized embeddings
|
| 37 |
+
|
| 38 |
+
Keyword boosting for lexical relevance
|
| 39 |
+
|
| 40 |
+
🧠 Reranking (Quality Boost)
|
| 41 |
+
|
| 42 |
+
Cross-Encoder (ms-marco-MiniLM) reranking
|
| 43 |
+
|
| 44 |
+
Improves relevance beyond raw vector similarity
|
| 45 |
+
|
| 46 |
+
Mimics production search stacks (retrieve → rerank)
|
| 47 |
+
|
| 48 |
+
🤖 LLM Generation
|
| 49 |
+
|
| 50 |
+
Google Gemini 2.5 Flash
|
| 51 |
+
|
| 52 |
+
Strict grounding: answers only from retrieved context
|
| 53 |
+
|
| 54 |
+
Honest fallback: "I don't know" when unsupported
|
| 55 |
+
|
| 56 |
+
📊 Evaluation & Monitoring
|
| 57 |
+
|
| 58 |
+
Logs every query:
|
| 59 |
+
|
| 60 |
+
retrieved chunk count
|
| 61 |
+
|
| 62 |
+
confidence score
|
| 63 |
+
|
| 64 |
+
known vs unknown answers
|
| 65 |
+
|
| 66 |
+
JSONL logs for offline analysis
|
| 67 |
+
|
| 68 |
+
Built-in analytics dashboard
|
| 69 |
+
|
| 70 |
+
📈 Analytics Dashboard
|
| 71 |
+
|
| 72 |
+
Total queries
|
| 73 |
+
|
| 74 |
+
Knowledge rate
|
| 75 |
+
|
| 76 |
+
Average confidence
|
| 77 |
+
|
| 78 |
+
Unknown query tracking
|
| 79 |
+
|
| 80 |
+
Recent query history
|
| 81 |
+
|
| 82 |
+
Dark / Light mode UI
|
| 83 |
+
|
| 84 |
+
🛡️ Production Safeguards
|
| 85 |
+
|
| 86 |
+
File upload size limits (configurable)
|
| 87 |
+
|
| 88 |
+
API quota handling
|
| 89 |
+
|
| 90 |
+
Caching to reduce LLM calls
|
| 91 |
+
|
| 92 |
+
Clean error handling
|
| 93 |
+
|
| 94 |
+
Persistent vector store
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
🏗️ System Architecture
|
| 98 |
+
|
| 99 |
+
Frontend (HTML / JS)
|
| 100 |
↓
|
| 101 |
FastAPI Backend
|
| 102 |
↓
|
| 103 |
Document Ingestion (PDF / TXT)
|
| 104 |
↓
|
| 105 |
+
Sentence Chunking + Metadata
|
| 106 |
+
↓
|
| 107 |
Embeddings (SentenceTransformers)
|
| 108 |
↓
|
| 109 |
+
FAISS ANN Index (HNSW)
|
| 110 |
↓
|
| 111 |
+
Hybrid Retrieval (Vector + Keyword)
|
| 112 |
+
↓
|
| 113 |
+
Cross-Encoder Reranking
|
| 114 |
↓
|
| 115 |
Prompt Assembly
|
| 116 |
↓
|
| 117 |
Google Gemini LLM
|
| 118 |
↓
|
| 119 |
+
Answer + Confidence + Citations
|
| 120 |
+
↓
|
| 121 |
+
Evaluation Logging + Analytics
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
+
🧠 Core Concepts Demonstrated
|
| 124 |
+
Retrieval-Augmented Generation (RAG)
|
| 125 |
|
| 126 |
+
Why pure LLMs hallucinate
|
| 127 |
|
| 128 |
+
How grounding fixes factual accuracy
|
| 129 |
|
| 130 |
+
Vector search vs keyword search
|
| 131 |
|
| 132 |
+
Hybrid retrieval strategies
|
| 133 |
|
| 134 |
+
Approximate Nearest Neighbor (ANN)
|
| 135 |
|
| 136 |
+
Why brute-force search fails at scale
|
| 137 |
|
| 138 |
+
HNSW indexing for fast similarity search
|
| 139 |
|
| 140 |
+
efConstruction vs efSearch trade-offs
|
| 141 |
|
| 142 |
+
Reranking
|
| 143 |
|
| 144 |
+
Why top-K vectors ≠ best answers
|
| 145 |
|
| 146 |
+
Cross-encoder reranking for relevance
|
| 147 |
|
| 148 |
+
Industry-standard retrieval pipelines
|
| 149 |
|
| 150 |
+
Evaluation & Observability
|
| 151 |
|
| 152 |
+
Measuring known vs unknown
|
| 153 |
|
| 154 |
+
Confidence as a heuristic, not truth
|
| 155 |
|
| 156 |
+
Logging for iterative improvement
|
| 157 |
|
| 158 |
+
Analytics-driven RAG tuning
|
| 159 |
|
| 160 |
+
Real Backend Engineering
|
| 161 |
|
| 162 |
+
API limits & retries
|
| 163 |
|
| 164 |
+
Persistent storage
|
| 165 |
|
| 166 |
+
Clean Git hygiene
|
| 167 |
|
| 168 |
+
Incremental system evolution
|
| 169 |
|
| 170 |
🛠️ Tech Stack
|
| 171 |
Backend
|
|
|
|
| 174 |
|
| 175 |
FastAPI
|
| 176 |
|
| 177 |
+
FAISS (HNSW ANN)
|
| 178 |
|
| 179 |
SentenceTransformers
|
| 180 |
|
| 181 |
+
Cross-Encoder (MS MARCO)
|
| 182 |
+
|
| 183 |
Google Gemini API
|
| 184 |
|
| 185 |
PyPDF
|
|
|
|
| 194 |
|
| 195 |
Vanilla JavaScript (Fetch API)
|
| 196 |
|
| 197 |
+
Tooling & Platform
|
| 198 |
|
| 199 |
VS Code
|
| 200 |
|
| 201 |
Git & GitHub
|
| 202 |
|
| 203 |
+
Docker
|
| 204 |
+
|
| 205 |
Hugging Face Spaces (deployment)
|
| 206 |
|
| 207 |
Virtual Environments (venv)
|
| 208 |
|
| 209 |
+
⚙️ Setup & Run Locally
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
+
1️⃣ Clone Repository
|
| 212 |
+
git clone https://github.com/LVVignesh/gemini-rag-fastapi.git
|
| 213 |
+
cd gemini-rag-fastapi
|
| 214 |
python -m venv venv
|
| 215 |
+
venv\Scripts\activate
|
|
|
|
|
|
|
|
|
|
| 216 |
pip install -r requirements.txt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
GEMINI_API_KEY=your_api_key_here
|
|
|
|
|
|
|
| 218 |
uvicorn main:app --reload
|
| 219 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
⚠️ Known Limitations
|
| 221 |
|
| 222 |
+
Scanned/image-only PDFs require OCR (not included)
|
| 223 |
+
|
| 224 |
+
Confidence score is heuristic
|
| 225 |
|
| 226 |
+
Very large corpora may require:
|
| 227 |
|
| 228 |
+
batch ingestion
|
| 229 |
|
| 230 |
+
sharding
|
| 231 |
|
| 232 |
+
background workers
|
| 233 |
|
| 234 |
+
🚀 Live Demo
|
| 235 |
|
| 236 |
+
👉 Hugging Face Spaces
|
| 237 |
+
https://huggingface.co/spaces/lvvignesh2122/Gemini-Rag-Fastapi-Pro
|
| 238 |
|
| 239 |
+
📜 License
|
| 240 |
|
| 241 |
+
MIT License
|
| 242 |
|
|
|