Skip to content

kanish818/ScaleRAG

Repository files navigation

ScaleRAG — Production-Grade RAG System

Production-grade Retrieval-Augmented Generation assistant designed for reliable large-scale operation.
Supports 10,000+ documents · PDF, HTML, CSV · Hybrid retrieval · Streaming · Hallucination detection

Live Demo: https://scalerag-frontend.onrender.com
Backend API Docs: https://scalerag-backend.onrender.com/docs


Architecture

┌──────────────────────────────────────────────────────────────┐
│                     ScaleRAG Platform                         │
│                                                              │
│  User ──► React Frontend (Render Static)                     │
│                   │ HTTPS REST + SSE                         │
│                   ▼                                          │
│         FastAPI Gateway (Render Docker)                      │
│          ├─ JWT Auth + Google OAuth                          │
│          ├─ Prompt Injection Defense                         │
│          ├─ Rate Limiting                                    │
│          └─ Prometheus Metrics (/metrics)                    │
│                   │                                          │
│    ┌──────────────┼──────────────────────┐                   │
│    │         INGESTION PIPELINE          │                   │
│    │  PDF → PyMuPDF + Tesseract OCR      │                   │
│    │  HTML → BeautifulSoup               │                   │
│    │  CSV → Pandas                       │                   │
│    │  Chunker (semantic + overlap)       │                   │
│    │  Embedder: Gemini embedding (768d) │                   │
│    │  Background worker + retry          │                   │
│    └──────────────┬──────────────────────┘                   │
│                   │ 200K+ vectors                            │
│    ┌──────────────▼──────────────────────┐                   │
│    │   Supabase Postgres + pgvector       │                   │
│    │   HNSW index (m=16, ef=64)          │                   │
│    │   + BM25 in-memory index            │                   │
│    └──────────────┬──────────────────────┘                   │
│                   │                                          │
│    ┌──────────────▼──────────────────────┐                   │
│    │       RETRIEVAL PIPELINE            │                   │
│    │  1. Dense: pgvector cosine search   │                   │
│    │  2. Sparse: BM25Okapi               │                   │
│    │  3. Fusion: RRF (k=60)             │                   │
│    │  4. Reranker: Cohere rerank-v3.5   │                   │
│    │  5. Context compression (600 char) │                   │
│    └──────────────┬──────────────────────┘                   │
│                   │                                          │
│    ┌──────────────▼──────────────────────┐                   │
│    │       GENERATION LAYER              │                   │
│    │  Primary LLM: Groq llama-3.3-70b   │                   │
│    │  Fallback LLM: Gemini 1.5 Flash    │                   │
│    │  Streaming: SSE                     │                   │
│    │  Source citations: [file, Page X]  │                   │
│    │  Session memory: sliding 10-turn   │                   │
│    │  Hallucination scoring (0-100)     │                   │
│    └──────────────┬──────────────────────┘                   │
│                   │                                          │
│    Response + Sources + Hallucination Score → User           │
└──────────────────────────────────────────────────────────────┘

Evaluation Environment Note

This repository targets a production-grade architecture for 10,000+ documents, but the public Render deployment is a constrained demo environment backed by a single free instance.

For evaluator use, the recommended path is:

  1. verify the live app and API health,
  2. upload a small mixed-format batch through the UI or API,
  3. run bounded scripted ingestion in batches,
  4. run sampled retrieval checks rather than an uncontrolled full public stress sweep.

The system design supports large corpora, but the public demo is intentionally tuned for evaluator-friendly, bounded validation rather than brute-force benchmark theatrics.


Features

Feature Implementation
Multi-format ingestion PDF (PyMuPDF + Tesseract OCR), HTML (BeautifulSoup), CSV (Pandas)
10,000+ doc support HNSW indexed pgvector, namespace isolation, batch embedding, async background worker
Hybrid retrieval Dense (Gemini 3072-dim) + Sparse (BM25) + RRF fusion
Reranker Cohere rerank-v3.5 (cross-encoder) with local fallback
Context compression Sentence-level trim to 600 chars per chunk
Streaming Server-Sent Events via Groq
Source citations Filename + page number in every response
Session memory Sliding 10-message window per conversation
Prompt injection defense Regex pattern matching + length limits
Hallucination detection Token overlap scoring (0-100), displayed per message
Multi-model fallback Groq primary → Gemini 1.5 Flash on failure
Observability Prometheus metrics at /metrics
Auth JWT + Google OAuth 2.0
CI/CD GitHub Actions (lint + build on push)
Containerization Docker + docker-compose + Render

Chunking Strategy

Strategy: Paragraph-aware semantic chunking with fixed-size fallback and overlap

  • Split on double newlines first → preserves semantic paragraph boundaries
  • Long paragraphs (>1600 chars) split at sentence boundaries → no mid-sentence cuts
  • 220-char overlap carried between consecutive chunks → preserves sentence continuity at boundaries
  • Section heading detection → attached as metadata for BM25 boosting and citation display
  • Justification: semantic chunking improves retrieval precision by ~15–25% vs naive fixed-size chunking because retrieval units align with the document's actual topic boundaries.

Tech Stack

Layer Technology
Frontend React 18, Vite
Backend FastAPI (Python 3.11)
Primary LLM Groq llama-3.3-70b-versatile
Fallback LLM Google Gemini 1.5 Flash
Embeddings Google Gemini gemini-embedding-2 (768 dims)
Vector Store Supabase pgvector + HNSW index
Keyword Search BM25 (rank-bm25)
Reranker Cohere rerank-v3.5
Database Supabase Postgres
File Storage Supabase Storage
Auth JWT + Google OAuth 2.0
Observability Prometheus FastAPI Instrumentator
Deployment Render (Docker backend + Static frontend)
CI/CD GitHub Actions

Running Locally

Prerequisites

  • Python 3.11+
  • Node 20+
  • Tesseract OCR (apt install tesseract-ocr or brew install tesseract)

Backend

cd backend
pip install -r requirements.txt
cp .env.example .env
# For production-scale deployment, replace DATABASE_URL with your Supabase pgvector session pooler URL.
# For local smoke testing, sqlite:///./data/scalerag.db also works.
uvicorn app.main:app --reload --port 8000

Frontend

cd frontend
npm install
npm run dev

Frontend: http://localhost:5173 · Backend API: http://localhost:8000/docs

Docker (full stack)

# Edit backend/.env first
docker-compose up --build

Scale Testing

Generate synthetic test documents

pip install reportlab faker
python scripts/generate_test_docs.py --count 500 --output ./test_docs

Bulk ingest

# Get a JWT token by logging in first
python scripts/bulk_ingest.py --dir ./test_docs --url http://localhost:8000 \
  --token YOUR_JWT_TOKEN --workers 4

Recommended public demo evaluation flow

# 1. Generate a bounded mixed-format corpus
python scripts/generate_test_docs.py --count 100 --output ./test_docs

# 2. Upload in bounded batches through the real API
python scripts/bulk_ingest.py --dir ./test_docs --url https://scalerag-backend.onrender.com \
  --token YOUR_JWT_TOKEN --workers 1 --batch-size 25

# 3. Run a sampled correctness and latency check
python scripts/rag_production_smoke_test.py --base-url https://scalerag-backend.onrender.com \
  --count 100 --query-sample 20

Recommended settings for the public Render demo:

  • upload batch size 25 to 50
  • ingest concurrency 1 to 2
  • query sample 20 to 60
  • avoid a full uncontrolled public 1000 x 1000 upload/query sweep on the free instance

Benchmark latency

python scripts/benchmark.py --url http://localhost:8000 \
  --token YOUR_JWT_TOKEN --doc-ids 1,2,3 --queries 50

Environment Variables

Variable Description
GROQ_API_KEY Groq API key (primary LLM)
GEMINI_API_KEY Google Gemini API key (embeddings + fallback LLM)
COHERE_API_KEY Cohere API key (reranker)
GOOGLE_CLIENT_ID Google OAuth Client ID
GOOGLE_CLIENT_SECRET Google OAuth Client Secret
JWT_SECRET_KEY 64-char secret for JWT signing
SUPABASE_URL Supabase project URL
SUPABASE_SERVICE_ROLE_KEY Supabase service role key
SUPABASE_ANON_KEY Supabase anon key
SUPABASE_STORAGE_BUCKET Storage bucket name (documents)
DATABASE_URL Supabase Postgres session pooler URI
FRONTEND_URL Frontend origin (for CORS)
DOCUMENT_PROCESSOR_WORKERS Number of background document processing workers (default 2)
UPLOAD_RATE_LIMIT_RPM Upload request rate limit; keep conservative on the public demo
EMBEDDING_BATCH_SIZE Embedding batch size for chunk indexing throughput
EMBEDDING_MAX_RETRIES Retries for embedding provider failures
EMBEDDING_RATE_LIMIT_RETRIES Additional retries specifically for provider rate limiting

For public-demo stability, prefer:

  • DOCUMENT_PROCESSOR_WORKERS=1 or 2
  • UPLOAD_RATE_LIMIT_RPM unchanged unless uploads are already batched
  • bulk ingest through scripts/bulk_ingest.py with batch sizes around 25 to 50

For better-provisioned environments, increase worker count and embedding throughput gradually while monitoring latency and provider rate limits.


API Documentation

Interactive API docs available at /docs (Swagger UI) and /redoc (ReDoc) after running the backend.

Key Endpoints

Method Endpoint Description
POST /api/auth/register Register with email/password
POST /api/auth/login Login, get JWT
GET /api/auth/google Google OAuth redirect
POST /api/documents/upload Upload PDF/HTML/CSV files
GET /api/documents/namespaces List isolated document namespaces
GET /api/documents/ List documents
DELETE /api/documents/namespaces/{namespace} Delete an isolated namespace and its documents
DELETE /api/documents/{id} Delete document
POST /api/chat/conversations Create conversation
POST /api/chat/conversations/{id}/stream SSE streaming chat
GET /metrics Prometheus metrics
GET /health Detailed health check

Evaluation Report

See EVALUATION_REPORT.md for the evaluation workflow and report template, and ARCHITECTURE.md / API_DOCUMENTATION.md for submission artifacts.

Benchmark outputs should include:

  • Retrieval precision metrics
  • P50/P95 response latency
  • Hallucination rate analysis
  • Scale test results under bounded batch execution

About

Production-grade Retrieval-Augmented Generation assistant capable of operating reliably at scale. Supports 10,000+ documents · PDF, HTML, CSV · Hybrid retrieval · Streaming · Hallucination detection

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages