Production-Grade Multi-Tenant Retrieval-Augmented Generation with Hard Namespace Isolation, Hybrid Search, Cross-Encoder Re-Ranking, and RAGAS Benchmarks.
- Overview
- Architecture
- Key Engineering Decisions
- Enterprise Security & Isolation
- Frontend Design & Mobile Optimization
- Measured Retrieval Quality (RAGAS)
- Tech Stack
- Project Structure
- Quickstart & Local Setup
- API Reference
- Testing & Verification
DocuMind is an enterprise-ready knowledge assistant and document intelligence platform designed to eliminate hallucination, guarantee multi-tenant data isolation, and provide provable retrieval quality.
Unlike generic RAG prototypes that rely purely on basic vector cosine similarity, DocuMind implements:
-
Two-Stage Hybrid Retrieval (
Dense Vector Semantic Search+BM25 Lexical Keyword Matching). -
Reciprocal Rank Fusion (RRF) (
$k=60$ ) combining disparate scoring spaces. -
Cross-Attention Re-Ranking via
cross-encoder/ms-marco-MiniLM-L-6-v2for high signal-to-noise ratio. - Hard Tenant Isolation enforced at the API key authentication boundary and vector namespace level.
- Real-Time Streaming SSE Synthesis with grounded citations.
- Automated Voice Input with speech recognition pause & silence auto-stop.
-
Claude-Inspired UI System in warm charcoal grey (
#18181b) and warm terracotta (#d97757). - Full Mobile Optimization with single-column responsive switching, compact navbar, and touch controls.
INGESTION PIPELINE
ββββββββββββββββ Upload PDF/DOCX/TXT βββββββββββββββββββββββββ
β Admin Client β ββββββββββββββββββββββΆ β FastAPI Gateway β
β (X-API-Key) β β - SHA-256 Auth Guard β
ββββββββββββββββ β - Tenant Namespace β
ββββββββββββ¬βββββββββββββ
β Async Processing
βΌ
βββββββββββββββββββββββββ
β Ingestion Worker β
β - PyPDF / python-docx β
β - 512-Token Chunking β
β - Dense Vectorization β
β - BM25 Inverted Index β
ββββββββββββ¬βββββββββββββ
βΌ
βββββββββββββββββββββββββ
β Namespaced Store β
β [namespace=tenant_id] β
βββββββββββββββββββββββββ
QUERY PIPELINE
ββββββββββββββββ Natural Language Q βββββββββββββββββββββββββ
β End User β ββββββββββββββββββββββΆ β FastAPI Gateway β
β / Voice Mic β ββββββββββββ¬βββββββββββββ
ββββββββββββββββ β
βΌ
βββββββββββββββββββββββββ
β Retrieval Engine β
β 1. Query Embedding β
β 2. Top-20 Dense Scan β
β 3. Top-20 BM25 Match β
β 4. RRF Fusion (k=60) β
β 5. Cross-Encoder Top-5β
ββββββββββββ¬βββββββββββββ
βΌ
βββββββββββββββββββββββββ
β Synthesis Engine β
β - Groq Cloud Llama β
β - Streaming SSE β
β - Grounded Citations β
ββββββββββββ¬βββββββββββββ
βΌ
Streamed Response + Reasoning Trace
- Problem: Pure vector embeddings excel at semantic matching, but fail on exact part numbers, contract clauses, SKUs, and numerical thresholds.
- Solution: DocuMind runs dense vector retrieval in parallel with an inverted-index BM25 lexical search, capturing both conceptual intent and exact terminology.
-
Problem: Raw similarity scores (cosine similarity
$[-1, 1]$ vs. unbounded BM25 scores $[0, \infty)$) cannot be added or normalized reliably without corpus-level bias. -
Solution: Rank-based fusion calculates:
$$\text{RRF_Score}(d) = \sum_{m \in M} \frac{1}{k + r_m(d)}$$ with constant$k=60$ to stabilize outlier effects.
- Problem: Bi-encoders compute query and document representations separately without joint cross-attention.
- Solution: DocuMind implements a two-stage retrieval strategy: retrieve broad (top-20 per engine), re-rank narrow (top-5) via cross-attention with
cross-encoder/ms-marco-MiniLM-L-6-v2. This delivers an average +14.3% accuracy boost over baseline.
- Structural Namespace Isolation: Documents and vectors are strictly stored in dedicated tenant namespaces. Tenant A physically cannot query or access Tenant B's vectors.
- Cryptographic Key Binding: Tenant IDs are never accepted as client parameters in the request body. They are derived server-side via SHA-256 hash lookup of the incoming
X-API-Key. - Strict Factual Grounding: Prompts enforce verifiable passage citations, preventing generative hallucinations.
- Claude-Inspired Warm Grey Theme:
- Velvet charcoal surfaces (
#18181b,#26262a,rgba(38, 38, 42, 0.78)). - Claude signature terracotta accent (
#d97757) and stone off-white typography (#f5f5f4). - Zero neon glows or navy/blue undertones.
- Velvet charcoal surfaces (
- Desktop & Mobile Responsiveness:
- Desktop (
>= 1024px): Side-by-side split screen with real-time Chat Studio and Document Ingestion Manager. - Mobile (
< 1024px): Seamless single-column switching. Dedicated header buttonDocs (N)opens ingestion; inside the switcher, an identically sizedChatbutton returns to conversation. - Mobile Attach Symbol (
Paperclip): Positioned next to the voice microphone exclusively on mobile viewports for quick document access. - Web Speech API Voice Input: Smart automated silence timer auto-stops listening after 1.5 seconds of silence, or immediately on query submission.
- Desktop (
DocuMind includes an automated evaluation suite based on standard RAGAS metrics:
| Metric | Target | Baseline (Dense Only) | DocuMind Two-Stage SOTA |
|---|---|---|---|
| Faithfulness | |||
| Answer Relevancy | |||
| Context Precision | |||
| Context Recall | |||
| End-to-End P95 Latency |
- Framework: FastAPI (Python 3.10+)
- LLM Provider: Groq Cloud API (
openai/gpt-oss-120b/llama-3.3-70b-versatile) - Embeddings: HuggingFace
sentence-transformers(all-MiniLM-L6-v2) - Lexical Search:
rank-bm25 - Re-Ranking:
sentence-transformers(cross-encoder/ms-marco-MiniLM-L-6-v2) - Document Parsers: PyPDF, python-docx
- Testing: pytest
- Framework: React 18 with Vite
- Styling: Vanilla CSS Design Tokens (Claude Warm Palette)
- Icons: Lucide React
- Markdown Rendering: react-markdown + remark-gfm
- Voice Recognition: Web Speech API (
SpeechRecognition)
DocuMind/
βββ .env.example # Environment variables template (safe for git)
βββ .gitignore # Comprehensive ignore rules (secrets, data, cache)
βββ README.md # Project documentation & architecture overview
βββ requirements.txt # Python backend dependencies
βββ backend/
β βββ main.py # FastAPI application, routing, & CORS setup
β βββ auth.py # Tenant API key authentication & SHA-256 hashing
β βββ ingestion.py # Document parsing, chunking, & vectorization
β βββ retrieval.py # Dense + BM25 hybrid search & cross-encoder re-ranking
β βββ generation.py # Grounded prompt engineering & streaming SSE synthesis
β βββ eval.py # RAGAS evaluation benchmark runner
β βββ models.py # Pydantic schemas & data transfer objects
βββ frontend/
β βββ package.json # Node dependencies & build scripts
β βββ vite.config.js # Vite build & server configuration
β βββ index.html # Entry HTML with viewport meta & SEO titles
β βββ src/
β βββ App.jsx # Main orchestrator (Chat, Ingestion, & Benchmark views)
β βββ index.css # Global tokens (Claude dark grey & typography)
β βββ components/chat/
β βββ ChatComposer.jsx # Floating prompt bar, voice mic, & model controls
β βββ ScopeApprovalCard.jsx # Grounding format modal (Executive/Evidence/Audit)
β βββ ThinkingTrace.jsx # Multi-stage reasoning & retrieval trace
β βββ SourceContextCards.jsx # Expandable citations & grounding snippets
β βββ MessageActions.jsx # Copy, retry, & sources toggle
β βββ PixelLoader.jsx # 3x3 pixel animation & elapsed counter
β βββ beautiful-ui.css # Design tokens & responsive mobile media queries
βββ tests/
βββ test_auth.py # API key & tenant isolation test cases
βββ test_retrieval.py # Hybrid search & cross-encoder test cases
βββ test_ingestion.py # Document chunking & parsing test cases
- Python 3.10+
- Node.js 18+ and npm
- Groq API Key (console.groq.com)
Copy the template and add your credentials:
cp .env.example .envEdit .env:
GROQ_API_KEY=your_actual_groq_api_key_here
GROQ_MODEL=openai/gpt-oss-120b
LLM_MODEL=openai/gpt-oss-120b# Create and activate virtual environment (optional)
python -m venv venv
# On Windows:
venv\Scripts\activate
# On Linux/macOS:
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt
# Start FastAPI server
python -m uvicorn backend.main:app --host 0.0.0.0 --port 8000 --reloadAPI Documentation will be live at: http://localhost:8000/docs.
In a new terminal window:
cd frontend
# Install packages
npm install
# Start Vite development server
npm run devOpen your browser at: http://localhost:5173.
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
POST |
/documents |
Upload PDF, DOCX, or TXT file for chunking & indexing | Yes (X-API-Key) |
GET |
/documents |
List indexed documents for current tenant | Yes (X-API-Key) |
DELETE |
/documents/{doc_id} |
Delete document and remove associated vectors | Yes (X-API-Key) |
POST |
/query |
Execute hybrid search & generate grounded answer (SSE) | Yes (X-API-Key) |
GET |
/eval/latest |
Retrieve latest cached RAGAS benchmark metrics | No |
POST |
/eval/run |
Execute 15-question benchmark evaluation run | Yes (X-API-Key) |
GET |
/usage |
Retrieve tenant token and document quotas | Yes (X-API-Key) |
Run the automated backend test suite:
python -m pytest tests/ -vValidate frontend production build:
cd frontend
npm run buildRun code quality linter:
cd frontend
npx oxlintThis project is open-source under the MIT License.
