How chitrangana10/legal-chatbot works
Purpose and Entry Points
This repository is an Indian-law legal guidance system using retrieval-augmented generation (RAG). It retrieves statute passages from a local corpus and produces cited answers through an LLM, with a retrieval-only fallback when generation fails or quota is unavailable.
Primary runtime entry points are the FastAPI backend at backend/app/main.py and Streamlit client at frontend/streamlit_app.py. The backend API contract and route layer are in backend/app/api/routes.py and backend/app/api/schemas.py.
API and Application Boundary
FastAPI owns request validation, API responses, and orchestration handoff. backend/app/main.py creates the service, while backend/app/api/routes.py accepts legal questions and invokes the RAG pipeline. backend/app/config.py centralizes environment-driven settings such as model/provider and data locations.
The Streamlit frontend is a separate Python process that calls the backend rather than directly accessing indexes or LLM credentials: frontend/streamlit_app.py.
RAG Orchestration
backend/app/services/rag_engine.py is the core control-flow layer. It takes a user query, obtains ranked legal context, decides whether the query is sufficiently supported/in scope, requests answer generation, and returns citations or a retrieval fallback.
This separates legal retrieval from answer composition and is the principal boundary for behavior such as unsupported-question refusal and LLM-failure degradation.
Hybrid Retrieval Layer
backend/app/services/hybrid_search.py combines semantic/vector retrieval with BM25 keyword retrieval. This is important for legal text: semantic matching supports paraphrased questions, while BM25 improves precision for statute names, section numbers, and legal terminology.
The vector side is managed by backend/app/services/vector_store.py, backed by persisted FAISS artifacts at backend/data/faiss_index/index.faiss and backend/data/faiss_index/metadata.json. Retrieval returns source metadata needed for citations and fallback responses.
Corpus and Indexing Pipeline
The source-of-truth legal corpus is JSON statute material under backend/data/raw/, including backend/data/raw/ipc_full.json, backend/data/raw/constitution_of_india.json, and backend/data/raw/code_of_criminal_procedure_1973.json. backend/app/services/raw_corpus.py loads or normalizes this corpus for retrieval/index operations.
backend/scripts/build_combined_index.py is the offline rebuild entry point. When raw statutes change, it chunks/processes source content, creates embeddings, and regenerates the FAISS index plus metadata. Raw data acquisition can be extended through backend/ingestion/scrapers/indian_kanoon.py.
LLM Generation and External Dependency
backend/app/services/llm_service.py abstracts answer generation. Gemini is the default external provider, configured through environment variables such as GEMINI_API_KEY; the key is required only for generated answers, not local retrieval. The service is expected to ground generated text in retrieved statute context and preserve citations.
Deployment and Runtime Infrastructure
The system runs on Python 3.11+ as two services: FastAPI/Uvicorn backend and Streamlit frontend. Container definitions are docker/backend.Dockerfile and docker/frontend.Dockerfile; local and production orchestration are docker-compose.yml and docker-compose.prod.yml. nginx.conf represents the reverse-proxy boundary for deployed traffic.
Persistent retrieval state is file-based in backend/data/faiss_index/; no database or remote vector service is indicated.
Main entry points: rag_engine.py RAG engine · build_combined_index.py Index rebuild · routes.py Question routes · vector_store.py Vector store
Components 20
Client
- Streamlit client · Separate process that calls the backend API.
frontend/streamlit_app.py
Backend API & RAG
- FastAPI service
backend/app/main.py - Question routes
backend/app/api/routes.py - API schemas
backend/app/api/schemas.py - Runtime settings · Provides provider and data-location settings.
backend/app/config.py - RAG engine · Controls support checks, generation, citations, and fallback.
backend/app/services/rag_engine.py - LLM service · Grounds generated answers in retrieved legal context.
backend/app/services/llm_service.py
Retrieval & Corpus
- Hybrid search · Combines semantic and BM25 retrieval.
backend/app/services/hybrid_search.py - Vector store
backend/app/services/vector_store.py - FAISS index
backend/data/faiss_index/index.faiss - Citation metadata
backend/data/faiss_index/metadata.json - Statute corpus loader
backend/app/services/raw_corpus.py - Raw statute JSON · IPC, Constitution, and CrPC statute material.
- Index rebuild · Chunks corpus, creates embeddings, and regenerates FAISS artifacts.
backend/scripts/build_combined_index.py - Indian Kanoon scraper
backend/ingestion/scrapers/indian_kanoon.py
Deployment
- Nginx proxy
nginx.conf - Container orchestration · Defines local and production backend/frontend services.
docker-compose.yml - Backend container
docker/backend.Dockerfile - Frontend container
docker/frontend.Dockerfile
Other components
- Gemini · Required only for generated answers.
Connections 21
- Streamlit client → FastAPI service: HTTP API calls
- FastAPI service → Question routes: registers
- Question routes → API schemas: validates with
- Question routes → RAG engine: submits question
- Runtime settings → RAG engine: configures
- RAG engine → Hybrid search: retrieves context
- RAG engine → LLM service: generates grounded answer
- RAG engine → Hybrid search: fallback passages
- Hybrid search → Vector store: semantic retrieval
- Vector store → FAISS index: searches
- Vector store → Citation metadata: returns source metadata
- LLM service → Gemini: generation request
- Indian Kanoon scraper → Raw statute JSON: extends corpus
- Statute corpus loader → Raw statute JSON: loads and normalizes
- Index rebuild → Statute corpus loader: reads corpus
- Index rebuild → FAISS index: builds
- Index rebuild → Citation metadata: writes
- Nginx proxy → Streamlit client: proxies client traffic
- Nginx proxy → FastAPI service: proxies API traffic
- Container orchestration → Backend container: runs
- Container orchestration → Frontend container: runs