This is a high-performance, asynchronous, and agentic Retrieval-Augmented Generation (RAG) system built with FastAPI, Gradio, and Ollama.
Unlike standard RAG prototypes, this system is engineered for Production Accuracy using a multi-stage retrieval pipeline and Agentic Self-Correction.
We don't just rely on vectors. Our system uses a Hybrid Search approach:
- Semantic Search (Dense): Captures conceptual meaning using
ChromaDBandall-MiniLM-L6-v2. - Keyword Search (Sparse - BM25): Ensures precise matching for specific names, dates, and rare terminology.
- Cross-Encoder Re-ranking: All candidates are re-scored by a secondary model (
bge-reranker-base) to filter out noise and ensure only the Top 3 highest-relevance chunks reach the AI.
The system evaluates its own search results. If the re-ranker score falls below a set Relevance Threshold, the AI triggers a "Safe Fallback" mode, explaining to the user exactly why it hasn't answered, rather than fabricating an answer (hallucination).
- Global Model Caching: Heavy models (
embeddings,reranker) are loaded at startup into RAM, reducing per-request latency from 8s to sub-1s. - Streaming Tokens: Responses are streamed token-by-token directly to the UI using a Python generator.
- PyMuPDF Ingestion: Industrial-grade PDF parsing (10-20x faster than standard PyPDF).
- SQL Session Memory: Conversational history is stored in SQLite (via SQLAlchemy) to provide persistent context across sessions.
- Alembic Migrations: Full database version control to manage schema evolution.
- Celery + Redis: Asynchronous background processing ensures the UI stays responsive while large documents are being vectorized.
- Backend: FastAPI
- Intelligence: Ollama (Llama 3.2), LangChain, Sentence-Transformers
- Vector DB: ChromaDB
- Relational DB: SQLite + SQLAlchemy + Alembic
- Task Queue: Celery + Redis
- Frontend: Gradio (Mounted to FastAPI)
python -m venv .venv
source .venv/bin/activate
uv pip install -r requirements.txtalembic upgrade headYou need three terminal windows open:
Window 1: Start Redis (Backend Queue)
docker-compose up -dWindow 2: Start the Celery Worker (The "Laborer")
source .venv/bin/activate
celery -A worker.celery_app worker --loglevel=info --pool=soloWindow 3: Start the FastAPI App (The "Brain")
source .venv/bin/activate
uvicorn main:app --reload- Navigate to
http://localhost:8000/chat-ui. - Upload your PDF (Wait for the worker to finish).
- Select your document in the dropdown.
- Chat with your book! (Notice the real-time streaming and citations).
This project uses a two-tier testing strategy to ensure both functional reliability and AI accuracy.
Verifies that the API endpoints, Gradio UI, and database connections are working.
- Run command:
pytest tests/test_api.py -v
Uses DeepEval with a local Llama 3.2 (Ollama) as a judge to scientifically score the RAG pipeline. We test two stages independently:
Tests if the search engine finds the right context without using the LLM.
- Metrics: Contextual Precision, Contextual Recall, Contextual Relevancy.
- Run command:
deepeval test run tests/test_retriever.py
Tests if the LLM provides truthful answers given perfect context (isolates the "Brain").
- Metrics: Faithfulness (No hallucination), Answer Relevancy.
- Run command:
deepeval test run tests/test_generator.py
- Implement DeepEval for automated accuracy scoring (Retriever & Generator).
- Add JWT-based Authentication for private multi-user access.
- Dockerize the entire deployment for cloud production.
Created by Rahul Thakur 🚀