# shobhit-shukla/rag-legal-docs architecture

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

- Interactive diagram: https://gitdiagram.com/shobhit-shukla/rag-legal-docs
- Repository: https://github.com/shobhit-shukla/rag-legal-docs

## Overview

GDPR Legal RAG turns a user’s legal question into a cited, grounded answer: the Streamlit chat submits it to an in-process chain or the FastAPI backend, which retrieves and reranks legal passages before generation. A separate ingestion stage parses PDFs into article-aware chunks and builds indexes. The repository also includes an agentic four-tool router and a production pgvector retrieval option. The graph reflects documented and excerpted relationships; unsampled implementation details are omitted rather than inferred.

Main entry points: [`src/api/main.py`](https://github.com/shobhit-shukla/rag-legal-docs/blob/master/src/api/main.py) (FastAPI endpoints), [`src/generation/chain.py`](https://github.com/shobhit-shukla/rag-legal-docs/blob/master/src/generation/chain.py) (RAG chain), [`src/retrieval/retriever.py`](https://github.com/shobhit-shukla/rag-legal-docs/blob/master/src/retrieval/retriever.py) (Chroma hybrid search), [`app.py`](https://github.com/shobhit-shukla/rag-legal-docs/blob/master/app.py) (Streamlit chat)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_experience["User experience"]
  node_ui["Streamlit chat<br/>[app.py]"]
  node_api["FastAPI endpoints<br/>[main.py]"]
end

subgraph group_serving["Query and generation"]
  node_chain["RAG chain<br/>[chain.py]"]
  node_agent["Agentic router<br/>[agent.py]"]
  node_llm{{"LLM provider"}}
  node_langfuse{{"Langfuse tracing"}}
end

subgraph group_retrieval["Retrieval and storage"]
  node_reranker["Cross-encoder reranker<br/>[reranker.py]"]
  node_hybrid["Chroma hybrid search<br/>[retriever.py]"]
  node_pg["pgvector hybrid search<br/>[pg_retriever.py]"]
  node_chroma[("ChromaDB index")]
  node_postgres[("Postgres pgvector")]
  node_bm25[("BM25 index<br/>[bm25.pkl]")]
end

subgraph group_ingestion["Legal document ingestion"]
  node_pdf["Legal PDF<br/>[gdpr.pdf]"]
  node_parser["Article-aware parser<br/>[pdf_parser.py]"]
end

subgraph group_configuration["Configuration"]
  node_settings["Runtime settings<br/>[config.py]"]
end

node_user(("Questioner"))

node_user -->|"asks question"| node_ui
node_ui -->|"runs or streams"| node_chain
node_ui -->|"posts query"| node_api
node_api -->|"runs or streams"| node_chain
node_api -->|"parses PDF"| node_parser
node_api -->|"builds index"| node_hybrid
node_api -->|"builds index"| node_pg
node_chain -->|"retrieves passages"| node_hybrid
node_chain -->|"reranks shortlist"| node_reranker
node_chain -->|"generates answer"| node_llm
node_chain -.->|"traces optionally"| node_langfuse
node_agent -->|"searches articles"| node_hybrid
node_agent -->|"reranks shortlist"| node_reranker
node_agent -->|"routes and answers"| node_llm
node_hybrid -->|"queries and stores"| node_chroma
node_hybrid -->|"fuses sparse results"| node_bm25
node_pg -->|"queries and stores"| node_postgres
node_pg -->|"fuses sparse results"| node_bm25
node_pdf -->|"supplies document"| node_parser
node_parser -->|"provides chunks"| node_hybrid
node_parser -->|"provides chunks"| node_pg
node_settings -->|"configures backend"| node_api
node_settings -->|"configures runtime"| node_ui

click node_ui "https://github.com/shobhit-shukla/rag-legal-docs/blob/master/app.py"
click node_api "https://github.com/shobhit-shukla/rag-legal-docs/blob/master/src/api/main.py"
click node_chain "https://github.com/shobhit-shukla/rag-legal-docs/blob/master/src/generation/chain.py"
click node_agent "https://github.com/shobhit-shukla/rag-legal-docs/blob/master/src/generation/agent.py"
click node_reranker "https://github.com/shobhit-shukla/rag-legal-docs/blob/master/src/retrieval/reranker.py"
click node_hybrid "https://github.com/shobhit-shukla/rag-legal-docs/blob/master/src/retrieval/retriever.py"
click node_pg "https://github.com/shobhit-shukla/rag-legal-docs/blob/master/src/retrieval/pg_retriever.py"
click node_bm25 "https://github.com/shobhit-shukla/rag-legal-docs/blob/master/data/bm25.pkl"
click node_pdf "https://github.com/shobhit-shukla/rag-legal-docs/blob/master/data/raw/gdpr.pdf"
click node_parser "https://github.com/shobhit-shukla/rag-legal-docs/blob/master/src/ingestion/pdf_parser.py"
click node_settings "https://github.com/shobhit-shukla/rag-legal-docs/blob/master/src/config.py"

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_ui,node_api toneBlue
class node_chain,node_agent,node_llm,node_langfuse toneAmber
class node_reranker,node_hybrid,node_pg,node_chroma,node_postgres,node_bm25 toneMint
class node_pdf,node_parser toneRose
class node_settings,node_user toneIndigo
```

## Components

### User experience

- [Streamlit chat](https://github.com/shobhit-shukla/rag-legal-docs/blob/master/app.py): `app.py`
- [FastAPI endpoints](https://github.com/shobhit-shukla/rag-legal-docs/blob/master/src/api/main.py): `src/api/main.py`

### Query and generation

- [RAG chain](https://github.com/shobhit-shukla/rag-legal-docs/blob/master/src/generation/chain.py): `src/generation/chain.py`
- [Agentic router](https://github.com/shobhit-shukla/rag-legal-docs/blob/master/src/generation/agent.py): `src/generation/agent.py`
- **LLM provider**
- **Langfuse tracing**

### Retrieval and storage

- [Cross-encoder reranker](https://github.com/shobhit-shukla/rag-legal-docs/blob/master/src/retrieval/reranker.py): `src/retrieval/reranker.py`
- [Chroma hybrid search](https://github.com/shobhit-shukla/rag-legal-docs/blob/master/src/retrieval/retriever.py): `src/retrieval/retriever.py`
- [pgvector hybrid search](https://github.com/shobhit-shukla/rag-legal-docs/blob/master/src/retrieval/pg_retriever.py): `src/retrieval/pg_retriever.py`
- **ChromaDB index**
- **Postgres pgvector**
- [BM25 index](https://github.com/shobhit-shukla/rag-legal-docs/blob/master/data/bm25.pkl): `data/bm25.pkl`

### Legal document ingestion

- [Legal PDF](https://github.com/shobhit-shukla/rag-legal-docs/blob/master/data/raw/gdpr.pdf): `data/raw/gdpr.pdf`
- [Article-aware parser](https://github.com/shobhit-shukla/rag-legal-docs/blob/master/src/ingestion/pdf_parser.py): `src/ingestion/pdf_parser.py`

### Configuration

- [Runtime settings](https://github.com/shobhit-shukla/rag-legal-docs/blob/master/src/config.py): `src/config.py`

### Other components

- **Questioner**

## Connections

- Questioner → Streamlit chat: asks question (evidence: `README.md`)
- Streamlit chat → RAG chain: runs or streams (evidence: `app.py`)
- Streamlit chat → FastAPI endpoints: posts query (evidence: `app.py`)
- FastAPI endpoints → RAG chain: runs or streams (evidence: `src/api/main.py`)
- FastAPI endpoints → Article-aware parser: parses PDF (evidence: `src/api/main.py`)
- FastAPI endpoints → Chroma hybrid search: builds index (evidence: `src/api/main.py`)
- FastAPI endpoints → pgvector hybrid search: builds index (evidence: `src/api/main.py`)
- RAG chain → Chroma hybrid search: retrieves passages (evidence: `src/generation/chain.py`)
- RAG chain → Cross-encoder reranker: reranks shortlist (evidence: `src/generation/chain.py`)
- RAG chain → LLM provider: generates answer (evidence: `src/generation/chain.py`)
- RAG chain → Langfuse tracing: traces optionally (evidence: `src/generation/chain.py`)
- Agentic router → Chroma hybrid search: searches articles (evidence: `src/generation/agent.py`)
- Agentic router → Cross-encoder reranker: reranks shortlist (evidence: `src/generation/agent.py`)
- Agentic router → LLM provider: routes and answers (evidence: `src/generation/agent.py`)
- Chroma hybrid search → ChromaDB index: queries and stores (evidence: `README.md`)
- Chroma hybrid search → BM25 index: fuses sparse results (evidence: `README.md`)
- pgvector hybrid search → Postgres pgvector: queries and stores (evidence: `src/retrieval/pg_retriever.py`)
- pgvector hybrid search → BM25 index: fuses sparse results (evidence: `src/retrieval/pg_retriever.py`)
- Legal PDF → Article-aware parser: supplies document (evidence: `README.md`)
- Article-aware parser → Chroma hybrid search: provides chunks (evidence: `scripts/ingest.py`)
- Article-aware parser → pgvector hybrid search: provides chunks (evidence: `src/api/main.py`)
- Runtime settings → FastAPI endpoints: configures backend (evidence: `src/api/main.py`)
- Runtime settings → Streamlit chat: configures runtime (evidence: `app.py`)
