Dyutiman-03/Production_RAG
๐ Production RAG System with Full Observability
A domain-specific "Ask My Docs" system with hybrid retrieval (BM25 + vector search), cross-encoder reranking, citation enforcement, Langfuse observability, RAGAS evaluation, and CI-gated regression testing.
100% free and open-source โ no payment required.
Architecture
Query โ Hybrid Retrieval (Vector + BM25) โ Cross-Encoder Re-Ranking โ Citation Enforcement โ LLM Generation
โ โ โ โ
Langfuse Trace Score Stats Grounded? Token Usage
โ No โ Refuse
โ Yes โ Cited AnswerTech Stack
Quick Start
1. Clone & Setup
cd RAG
python -m venv venv
venv\Scripts\activate # Windows
# source venv/bin/activate # macOS/Linux
pip install -r requirements.txt2. Configure Environment
copy .env.example .env
# Edit .env with your GOOGLE_API_KEY
# Get a free key at: https://aistudio.google.com/apikey3. Start Langfuse (Optional)
docker compose -f docker-compose.langfuse.yml up -d
# Visit http://localhost:3000 to create an account
# Generate API keys and add to .env4. Ingest Documents
# Via API after starting the server, or manually:
python -c "
from src.ingestion.loader import load_documents
from src.ingestion.chunker import TokenAwareChunker
from src.retrieval.vector_store import VectorStore
from src.retrieval.bm25_search import BM25Search
docs = load_documents('./data/documents')
chunker = TokenAwareChunker()
chunks = chunker.chunk_documents(docs)
store = VectorStore()
store.add_chunks(chunks)
bm25 = BM25Search()
bm25.build_index(store.get_all_chunks())
bm25.save_index()
print(f'Ingested {len(docs)} documents โ {len(chunks)} chunks')
"5. Run the Server
python -m src.api.main
# Visit http://localhost:8000Features
Hybrid Retrieval
Combines BM25 keyword search with vector semantic search. Vector search captures meaning; BM25 captures exact terms. Configurable weight blending (default: 60/40).
Cross-Encoder Re-Ranking
After initial retrieval, a cross-encoder evaluates (query, chunk) pairs jointly for dramatically improved precision. Reduces 20 candidates to top 5.
Citation Enforcement
Hard rule, not a soft guideline. If the re-ranker scores fall below the confidence threshold, the system explicitly declines to answer. No hallucination.
Prompt Versioning
All prompts stored in config/prompts.yaml with version numbers. Every response is traceable to the exact prompt version that generated it.
Observability (Langfuse)
Every request traces: chunks retrieved, prompt sent, response generated, tokens consumed. P50/P95 latency, cost tracking, citation coverage, re-ranker score distribution.
CI-Gated Evaluation
GitHub Actions runs RAGAS evaluation on every PR. If faithfulness or other quality metrics drop below thresholds, the build fails.
Project Structure
RAG/
โโโ config/
โ โโโ settings.yaml # All tunable parameters
โ โโโ prompts.yaml # Versioned prompt templates
โโโ src/
โ โโโ ingestion/ # Document loading & chunking
โ โโโ retrieval/ # Vector, BM25, hybrid, reranker
โ โโโ generation/ # LLM, prompts, RAG pipeline
โ โโโ observability/ # Langfuse tracing, metrics
โ โโโ evaluation/ # RAGAS evaluation & golden dataset
โ โโโ api/ # FastAPI endpoints
โ โโโ web/ # Frontend UI
โโโ tests/ # Unit & integration tests
โโโ data/documents/ # Source document corpus
โโโ docker-compose.langfuse.yml
โโโ .github/workflows/ # CI evaluation pipelineAPI Endpoints
Testing
python -m pytest tests/ -vEvaluation
python -m src.evaluation.evaluate --output evaluation_report.jsonLicense
MIT
