How antonymittul/multi_tenant_rag works
Purpose and Runtime Topology
This is a multi-tenant RAG system: organizations upload documents, background workers ingest and embed them, and users query isolated tenant knowledge to generate answers. The primary backend is FastAPI, paired with PostgreSQL tenant metadata, Qdrant vector retrieval, Redis/Celery asynchronous jobs, Gemini generation, and a React/Vite frontend served through Nginx. Container wiring is defined in docker-compose.yml; backend startup is app/main.py.
API and Request Boundary
FastAPI mounts the API router from app/api/api.py, with endpoint modules separating tenant administration, document ingestion, search, answer generation, and evaluation:
• app/api/endpoints/tenants.py
• app/api/endpoints/documents.py
• app/api/endpoints/search.py
• app/api/endpoints/generate.py
• app/api/endpoints/evaluate.py
Request dependencies in app/api/deps.py establish the API’s tenant/authentication boundary. Tenant identity must propagate into ingestion, retrieval, and generation so vector queries cannot cross organization boundaries.
Tenant Persistence and Isolation
Tenant profiles and API-key-backed identity are persisted in PostgreSQL through:
• app/models/tenant.py
• app/schemas/tenant.py
• app/crud/crud_tenant.py
Database lifecycle and ORM setup live in app/db/session.py and app/db/base_class.py. PostgreSQL holds relational tenant metadata; Qdrant holds document vectors and must receive tenant filters on every retrieval operation.
Asynchronous Document Ingestion
Document submission enters through app/api/endpoints/documents.py and is delegated to Celery rather than processed in the API request path. The worker runtime is defined by:
• app/worker/celery_app.py
• app/worker/tasks.py
• app/services/ingestion.py
This stage performs document processing, chunking, and embedding before writing tenant-scoped records to the vector store. Redis is the Celery broker/cache layer, represented by app/services/cache.py.
Retrieval Pipeline
Query retrieval is encapsulated in app/services/retrieval.py and exposed independently through app/api/endpoints/search.py or as context for generation. The intended search path is hybrid dense-vector plus sparse BM25 retrieval, reciprocal-rank fusion, then cross-encoder reranking. Tenant filtering is a mandatory boundary at the Qdrant query layer.
Generation, Guardrails, and Evaluation
Generation endpoints accept a tenant-scoped question, retrieve relevant context, apply safety/policy checks, and invoke Gemini:
• app/services/generation.py
• app/services/guardrails.py
• app/schemas/generation.py
Evaluation is a separate API/service path in app/api/endpoints/evaluate.py and app/services/evaluation.py, allowing quality assessment without conflating it with the serving flow.
Frontend and Deployment Edges
The React client starts at frontend/src/main.tsx and is composed from frontend/src/App.tsx; backend communication is centralized in frontend/src/api.ts. Its production container uses frontend/nginx.conf as the proxy/security edge. Local deployment builds the service cluster through docker-compose.yml; cloud deployment can split frontend, API, worker, Redis, PostgreSQL, and Qdrant into managed services. Runtime configuration, including Gemini and infrastructure credentials, is centralized in app/core/config.py and .env.example.
Main entry points: api.py API router · search.py Search, answer & evaluation · celery_app.py Celery worker · docker-compose.yml Service cluster
Components 21
Frontend
- React client
frontend/src/main.tsx - Backend client
frontend/src/api.ts - Proxy edge
frontend/nginx.conf
FastAPI
- Backend startup
app/main.py - API router
app/api/api.py - Tenant auth boundary · Establishes tenant identity for every protected flow.
app/api/deps.py - Tenant administration
app/api/endpoints/tenants.py - Document submission
app/api/endpoints/documents.py - Search, answer & evaluation
app/api/endpoints/search.py
Async ingestion
- Celery worker
app/worker/celery_app.py - Ingestion tasks
app/worker/tasks.py - Process, chunk & embed
app/services/ingestion.py
RAG pipeline
- Hybrid retrieval · Dense and BM25 retrieval, rank fusion, and reranking.
app/services/retrieval.py - Guarded generation · Applies safety checks to tenant-scoped retrieved context.
app/services/generation.py - Quality evaluation · Runs separately from the serving path.
app/services/evaluation.py
Data & external services
- Tenant repository
app/crud/crud_tenant.py - Tenant metadata · Stores tenant profiles and API-key identity.
app/models/tenant.py - Job broker & cache
app/services/cache.py - Tenant vector store · Retrieval must apply tenant filters.
- Gemini
Other components
- Service cluster · Builds and wires local runtime services.
docker-compose.yml
Connections 24
- Service cluster → Proxy edge: builds
- Service cluster → Backend startup: runs
- Service cluster → Celery worker: runs
- React client → Backend client: uses
- Backend client → Proxy edge: requests
- Proxy edge → Backend startup: proxies
- Backend startup → API router: mounts
- API router → Tenant auth boundary: applies
- API router → Tenant administration: routes
- API router → Document submission: routes
- API router → Search, answer & evaluation: routes
- Tenant auth boundary → Tenant repository: resolves identity
- Tenant repository → Tenant metadata: persists
- Document submission → Celery worker: queues ingestion
- Celery worker → Job broker & cache: uses broker
- Celery worker → Ingestion tasks: executes
- Ingestion tasks → Process, chunk & embed: processes
- Process, chunk & embed → Tenant vector store: writes tenant vectors
- Search, answer & evaluation → Hybrid retrieval: searches
- Hybrid retrieval → Tenant vector store: queries with tenant filter
- Search, answer & evaluation → Guarded generation: answers
- Guarded generation → Hybrid retrieval: gets context
- Guarded generation → Gemini: invokes
- Search, answer & evaluation → Quality evaluation: evaluates