# yakkalakarthikeya/legalrag-ai architecture

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

- Interactive diagram: https://gitdiagram.com/yakkalakarthikeya/legalrag-ai
- Repository: https://github.com/yakkalakarthikeya/legalrag-ai

## Overview

LegalRAG-AI is a Streamlit application for analyzing uploaded legal PDFs and answering document-grounded questions. Its main workflow extracts and segments documents, creates embeddings and per-document FAISS indices, then retrieves, validates, and adaptively selects clauses before generating and evaluating an answer. The graph also includes cached artifacts and report/export behavior. Workflow links use the README where source excerpts do not show the full call sequence; unsampled implementation details are not inferred.

Main entry points: [`app.py`](https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/app.py) (Streamlit dashboard), [`vectorstore/faiss_manager.py`](https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/vectorstore/faiss_manager.py) (FAISS index manager), [`models/sentence_bert.py`](https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/models/sentence_bert.py) (Sentence embeddings), [`retrieval/retrieval.py`](https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/retrieval/retrieval.py) (Semantic retrieval)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interface["User interface"]
  node_dashboard["Streamlit dashboard<br/>[app.py]"]
  node_report["Results and text export<br/>[app.py]"]
end

subgraph group_ingestion["Document ingestion"]
  node_pdf_extract["PDF extraction<br/>[pdf_extractor.py]"]
  node_clause_split["Clause segmentation<br/>[clause_chunker.py]"]
  node_chunk_cache[("Clause cache")]
end

subgraph group_indexing["Semantic indexing"]
  node_embedding_model["Sentence embeddings<br/>[sentence_bert.py]"]
  node_embedding_cache[("Embedding cache")]
  node_faiss_index["FAISS index manager<br/>[faiss_manager.py]"]
  node_index_store[("FAISS indices")]
end

subgraph group_answering["Question answering"]
  node_retriever["Semantic retrieval<br/>[retrieval.py]"]
  node_relevance["Relevance validation"]
  node_dcbd["Adaptive clause selection<br/>[dcbd.py]"]
  node_llm["Grounded report generation<br/>[llama_manager.py]"]
  node_llm_services{{"Groq or local LLM"}}
end

subgraph group_evaluation["Answer evaluation"]
  node_faithfulness["Faithfulness evaluation<br/>[faithfulness.py]"]
  node_jaccard["Jaccard evaluation"]
  node_bert_eval["BERTScore evaluation"]
end

node_user(("Document user"))

node_user -->|"uploads and asks"| node_dashboard
node_dashboard -->|"extracts PDFs"| node_pdf_extract
node_pdf_extract -->|"passes text"| node_clause_split
node_clause_split -->|"saves clauses"| node_chunk_cache
node_dashboard -->|"encodes clauses"| node_embedding_model
node_embedding_model -->|"reads clauses"| node_chunk_cache
node_embedding_model -->|"caches vectors"| node_embedding_cache
node_dashboard -->|"builds index"| node_faiss_index
node_faiss_index -->|"loads vectors"| node_embedding_cache
node_faiss_index -->|"loads metadata"| node_chunk_cache
node_faiss_index -->|"persists index"| node_index_store
node_dashboard -->|"searches documents"| node_retriever
node_retriever -->|"encodes query"| node_embedding_model
node_retriever -->|"searches index"| node_index_store
node_dashboard -->|"checks results"| node_relevance
node_dashboard -->|"selects clauses"| node_dcbd
node_dashboard -->|"generates answer"| node_llm
node_llm -.->|"invokes backend"| node_llm_services
node_dashboard -->|"evaluates answer"| node_faithfulness
node_faithfulness -->|"computes lexical score"| node_jaccard
node_dashboard -.->|"compares BERTScore"| node_bert_eval
node_dashboard -->|"displays and exports"| node_report

click node_dashboard "https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/app.py"
click node_pdf_extract "https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/preprocessing/pdf_extractor.py"
click node_clause_split "https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/preprocessing/clause_chunker.py"
click node_chunk_cache "https://github.com/yakkalakarthikeya/legalrag-ai/tree/main/chunks"
click node_embedding_model "https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/models/sentence_bert.py"
click node_embedding_cache "https://github.com/yakkalakarthikeya/legalrag-ai/tree/main/embeddings"
click node_faiss_index "https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/vectorstore/faiss_manager.py"
click node_index_store "https://github.com/yakkalakarthikeya/legalrag-ai/tree/main/faiss_index"
click node_retriever "https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/retrieval/retrieval.py"
click node_relevance "https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/retrieval/relevance_validator.py"
click node_dcbd "https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/retrieval/dcbd.py"
click node_llm "https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/llm/llama_manager.py"
click node_faithfulness "https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/evaluation/faithfulness.py"
click node_jaccard "https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/evaluation/jaccard_evaluator.py"
click node_bert_eval "https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/evaluation/bertscore_evaluator.py"
click node_report "https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/app.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_dashboard,node_report,node_user toneBlue
class node_pdf_extract,node_clause_split,node_chunk_cache toneAmber
class node_embedding_model,node_embedding_cache,node_faiss_index,node_index_store toneMint
class node_retriever,node_relevance,node_dcbd,node_llm,node_llm_services toneRose
class node_faithfulness,node_jaccard,node_bert_eval toneIndigo
```

## Components

### User interface

- [Streamlit dashboard](https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/app.py): `app.py`
- [Results and text export](https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/app.py): `app.py`

### Document ingestion

- [PDF extraction](https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/preprocessing/pdf_extractor.py): `preprocessing/pdf_extractor.py`
- [Clause segmentation](https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/preprocessing/clause_chunker.py): `preprocessing/clause_chunker.py`
- [Clause cache](https://github.com/yakkalakarthikeya/legalrag-ai/tree/main/chunks): `chunks`

### Semantic indexing

- [Sentence embeddings](https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/models/sentence_bert.py): `models/sentence_bert.py`
- [Embedding cache](https://github.com/yakkalakarthikeya/legalrag-ai/tree/main/embeddings): `embeddings`
- [FAISS index manager](https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/vectorstore/faiss_manager.py): `vectorstore/faiss_manager.py`
- [FAISS indices](https://github.com/yakkalakarthikeya/legalrag-ai/tree/main/faiss_index): `faiss_index`

### Question answering

- [Semantic retrieval](https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/retrieval/retrieval.py): `retrieval/retrieval.py`
- [Relevance validation](https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/retrieval/relevance_validator.py): `retrieval/relevance_validator.py`
- [Adaptive clause selection](https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/retrieval/dcbd.py): `retrieval/dcbd.py`
- [Grounded report generation](https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/llm/llama_manager.py): `llm/llama_manager.py`
- **Groq or local LLM**

### Answer evaluation

- [Faithfulness evaluation](https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/evaluation/faithfulness.py): `evaluation/faithfulness.py`
- [Jaccard evaluation](https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/evaluation/jaccard_evaluator.py): `evaluation/jaccard_evaluator.py`
- [BERTScore evaluation](https://github.com/yakkalakarthikeya/legalrag-ai/blob/main/evaluation/bertscore_evaluator.py): `evaluation/bertscore_evaluator.py`

### Other components

- **Document user**

## Connections

- Document user → Streamlit dashboard: uploads and asks (evidence: `README.md`)
- Streamlit dashboard → PDF extraction: extracts PDFs (evidence: `README.md`)
- PDF extraction → Clause segmentation: passes text (evidence: `README.md`)
- Clause segmentation → Clause cache: saves clauses (evidence: `preprocessing/clause_chunker.py`)
- Streamlit dashboard → Sentence embeddings: encodes clauses (evidence: `README.md`)
- Sentence embeddings → Clause cache: reads clauses (evidence: `models/sentence_bert.py`)
- Sentence embeddings → Embedding cache: caches vectors (evidence: `models/sentence_bert.py`)
- Streamlit dashboard → FAISS index manager: builds index (evidence: `app.py`)
- FAISS index manager → Embedding cache: loads vectors (evidence: `vectorstore/faiss_manager.py`)
- FAISS index manager → Clause cache: loads metadata (evidence: `vectorstore/faiss_manager.py`)
- FAISS index manager → FAISS indices: persists index (evidence: `vectorstore/faiss_manager.py`)
- Streamlit dashboard → Semantic retrieval: searches documents (evidence: `app.py`)
- Semantic retrieval → Sentence embeddings: encodes query (evidence: `retrieval/retrieval.py`)
- Semantic retrieval → FAISS indices: searches index (evidence: `README.md`)
- Streamlit dashboard → Relevance validation: checks results (evidence: `app.py`)
- Streamlit dashboard → Adaptive clause selection: selects clauses (evidence: `app.py`)
- Streamlit dashboard → Grounded report generation: generates answer (evidence: `README.md`)
- Grounded report generation → Groq or local LLM: invokes backend
- Streamlit dashboard → Faithfulness evaluation: evaluates answer (evidence: `app.py`)
- Faithfulness evaluation → Jaccard evaluation: computes lexical score (evidence: `evaluation/faithfulness.py`)
- Streamlit dashboard → BERTScore evaluation: compares BERTScore
- Streamlit dashboard → Results and text export: displays and exports (evidence: `README.md`)
