# chitrangana10/legal-chatbot architecture

> Architecture diagram of the GitHub repository chitrangana10/legal-chatbot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/chitrangana10/legal-chatbot
- Repository: https://github.com/chitrangana10/legal-chatbot

## Overview

#### 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: [`backend/app/services/rag_engine.py`](https://github.com/chitrangana10/legal-chatbot/blob/main/backend/app/services/rag_engine.py) (RAG engine), [`backend/scripts/build_combined_index.py`](https://github.com/chitrangana10/legal-chatbot/blob/main/backend/scripts/build_combined_index.py) (Index rebuild), [`backend/app/api/routes.py`](https://github.com/chitrangana10/legal-chatbot/blob/main/backend/app/api/routes.py) (Question routes), [`backend/app/services/vector_store.py`](https://github.com/chitrangana10/legal-chatbot/blob/main/backend/app/services/vector_store.py) (Vector store)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client"]
  node_streamlit["Streamlit client<br/>Python UI<br/>[streamlit_app.py]"]
end

subgraph group_backend["Backend API &amp; RAG"]
  node_fastapi_main["FastAPI service<br/>API entry point<br/>[main.py]"]
  node_routes["Question routes<br/>route layer<br/>[routes.py]"]
  node_schemas["API schemas<br/>request/response contract<br/>[schemas.py]"]
  node_config["Runtime settings<br/>environment config<br/>[config.py]"]
  node_rag_engine{{"RAG engine<br/>orchestrator<br/>[rag_engine.py]"}}
  node_llm_service["LLM service<br/>generation adapter<br/>[llm_service.py]"]
end

subgraph group_retrieval["Retrieval &amp; Corpus"]
  node_hybrid_search["Hybrid search<br/>retrieval service<br/>[hybrid_search.py]"]
  node_vector_store["Vector store<br/>FAISS retrieval<br/>[vector_store.py]"]
  node_faiss_index[("FAISS index<br/>persistent index<br/>[index.faiss]")]
  node_faiss_metadata["Citation metadata<br/>index metadata<br/>[metadata.json]"]
  node_raw_corpus["Statute corpus loader<br/>corpus service<br/>[raw_corpus.py]"]
  node_raw_statutes["Raw statute JSON<br/>source corpus"]
  node_index_builder{{"Index rebuild<br/>offline pipeline"}}
  node_scraper["Indian Kanoon scraper<br/>optional ingestion<br/>[indian_kanoon.py]"]
end

subgraph group_deployment["Deployment"]
  node_nginx["Nginx proxy<br/>reverse proxy<br/>[nginx.conf]"]
  node_compose["Container orchestration<br/>deployment manifests<br/>[docker-compose.yml]"]
  node_backend_image["Backend container<br/>Docker image<br/>[backend.Dockerfile]"]
  node_frontend_image["Frontend container<br/>Docker image"]
end

node_gemini(("Gemini<br/>external LLM provider"))

node_streamlit -->|"HTTP API calls"| node_fastapi_main
node_fastapi_main -->|"registers"| node_routes
node_routes -->|"validates with"| node_schemas
node_routes -->|"submits question"| node_rag_engine
node_config -.->|"configures"| node_rag_engine
node_rag_engine -->|"retrieves context"| node_hybrid_search
node_rag_engine -->|"generates grounded answer"| node_llm_service
node_rag_engine -.->|"fallback passages"| node_hybrid_search
node_hybrid_search -->|"semantic retrieval"| node_vector_store
node_vector_store -->|"searches"| node_faiss_index
node_vector_store -->|"returns source metadata"| node_faiss_metadata
node_llm_service -->|"generation request"| node_gemini
node_scraper -.->|"extends corpus"| node_raw_statutes
node_raw_corpus -->|"loads and normalizes"| node_raw_statutes
node_index_builder -->|"reads corpus"| node_raw_corpus
node_index_builder -->|"builds"| node_faiss_index
node_index_builder -->|"writes"| node_faiss_metadata
node_nginx -->|"proxies client traffic"| node_streamlit
node_nginx -->|"proxies API traffic"| node_fastapi_main
node_compose -->|"runs"| node_backend_image
node_compose -->|"runs"| node_frontend_image

click node_streamlit "https://github.com/chitrangana10/legal-chatbot/blob/main/frontend/streamlit_app.py"
click node_fastapi_main "https://github.com/chitrangana10/legal-chatbot/blob/main/backend/app/main.py"
click node_routes "https://github.com/chitrangana10/legal-chatbot/blob/main/backend/app/api/routes.py"
click node_schemas "https://github.com/chitrangana10/legal-chatbot/blob/main/backend/app/api/schemas.py"
click node_config "https://github.com/chitrangana10/legal-chatbot/blob/main/backend/app/config.py"
click node_rag_engine "https://github.com/chitrangana10/legal-chatbot/blob/main/backend/app/services/rag_engine.py"
click node_hybrid_search "https://github.com/chitrangana10/legal-chatbot/blob/main/backend/app/services/hybrid_search.py"
click node_vector_store "https://github.com/chitrangana10/legal-chatbot/blob/main/backend/app/services/vector_store.py"
click node_faiss_index "https://github.com/chitrangana10/legal-chatbot/blob/main/backend/data/faiss_index/index.faiss"
click node_faiss_metadata "https://github.com/chitrangana10/legal-chatbot/blob/main/backend/data/faiss_index/metadata.json"
click node_raw_corpus "https://github.com/chitrangana10/legal-chatbot/blob/main/backend/app/services/raw_corpus.py"
click node_index_builder "https://github.com/chitrangana10/legal-chatbot/blob/main/backend/scripts/build_combined_index.py"
click node_scraper "https://github.com/chitrangana10/legal-chatbot/blob/main/backend/ingestion/scrapers/indian_kanoon.py"
click node_llm_service "https://github.com/chitrangana10/legal-chatbot/blob/main/backend/app/services/llm_service.py"
click node_nginx "https://github.com/chitrangana10/legal-chatbot/blob/main/nginx.conf"
click node_compose "https://github.com/chitrangana10/legal-chatbot/blob/main/docker-compose.yml"
click node_backend_image "https://github.com/chitrangana10/legal-chatbot/blob/main/docker/backend.Dockerfile"
click node_frontend_image "https://github.com/chitrangana10/legal-chatbot/blob/main/docker/frontend.Dockerfile"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_streamlit toneBlue
class node_fastapi_main,node_routes,node_schemas,node_config,node_rag_engine,node_llm_service toneAmber
class node_hybrid_search,node_vector_store,node_faiss_index,node_faiss_metadata,node_raw_corpus,node_raw_statutes,node_index_builder,node_scraper toneMint
class node_nginx,node_compose,node_backend_image,node_frontend_image toneRose
class node_gemini toneNeutral
```

## Components

### Client

- [Streamlit client](https://github.com/chitrangana10/legal-chatbot/blob/main/frontend/streamlit_app.py): Python UI · Separate process that calls the backend API. · `frontend/streamlit_app.py`

### Backend API & RAG

- [FastAPI service](https://github.com/chitrangana10/legal-chatbot/blob/main/backend/app/main.py): API entry point · `backend/app/main.py`
- [Question routes](https://github.com/chitrangana10/legal-chatbot/blob/main/backend/app/api/routes.py): route layer · `backend/app/api/routes.py`
- [API schemas](https://github.com/chitrangana10/legal-chatbot/blob/main/backend/app/api/schemas.py): request/response contract · `backend/app/api/schemas.py`
- [Runtime settings](https://github.com/chitrangana10/legal-chatbot/blob/main/backend/app/config.py): environment config · Provides provider and data-location settings. · `backend/app/config.py`
- [RAG engine](https://github.com/chitrangana10/legal-chatbot/blob/main/backend/app/services/rag_engine.py): orchestrator · Controls support checks, generation, citations, and fallback. · `backend/app/services/rag_engine.py`
- [LLM service](https://github.com/chitrangana10/legal-chatbot/blob/main/backend/app/services/llm_service.py): generation adapter · Grounds generated answers in retrieved legal context. · `backend/app/services/llm_service.py`

### Retrieval & Corpus

- [Hybrid search](https://github.com/chitrangana10/legal-chatbot/blob/main/backend/app/services/hybrid_search.py): retrieval service · Combines semantic and BM25 retrieval. · `backend/app/services/hybrid_search.py`
- [Vector store](https://github.com/chitrangana10/legal-chatbot/blob/main/backend/app/services/vector_store.py): FAISS retrieval · `backend/app/services/vector_store.py`
- [FAISS index](https://github.com/chitrangana10/legal-chatbot/blob/main/backend/data/faiss_index/index.faiss): persistent index · `backend/data/faiss_index/index.faiss`
- [Citation metadata](https://github.com/chitrangana10/legal-chatbot/blob/main/backend/data/faiss_index/metadata.json): index metadata · `backend/data/faiss_index/metadata.json`
- [Statute corpus loader](https://github.com/chitrangana10/legal-chatbot/blob/main/backend/app/services/raw_corpus.py): corpus service · `backend/app/services/raw_corpus.py`
- **Raw statute JSON**: source corpus · IPC, Constitution, and CrPC statute material.
- [Index rebuild](https://github.com/chitrangana10/legal-chatbot/blob/main/backend/scripts/build_combined_index.py): offline pipeline · Chunks corpus, creates embeddings, and regenerates FAISS artifacts. · `backend/scripts/build_combined_index.py`
- [Indian Kanoon scraper](https://github.com/chitrangana10/legal-chatbot/blob/main/backend/ingestion/scrapers/indian_kanoon.py): optional ingestion · `backend/ingestion/scrapers/indian_kanoon.py`

### Deployment

- [Nginx proxy](https://github.com/chitrangana10/legal-chatbot/blob/main/nginx.conf): reverse proxy · `nginx.conf`
- [Container orchestration](https://github.com/chitrangana10/legal-chatbot/blob/main/docker-compose.yml): deployment manifests · Defines local and production backend/frontend services. · `docker-compose.yml`
- [Backend container](https://github.com/chitrangana10/legal-chatbot/blob/main/docker/backend.Dockerfile): Docker image · `docker/backend.Dockerfile`
- [Frontend container](https://github.com/chitrangana10/legal-chatbot/blob/main/docker/frontend.Dockerfile): Docker image · `docker/frontend.Dockerfile`

### Other components

- **Gemini**: external LLM provider · Required only for generated answers.

## Connections

- 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
