# 24p61a6662-stack/legal_document architecture

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

- Interactive diagram: https://gitdiagram.com/24p61a6662-stack/legal_document
- Repository: https://github.com/24p61a6662-stack/legal_document

## Overview

Legal Document Assistant provides contract upload and analysis, legal-document Q&A, risk and compliance review, contract comparison, and account access. The principal evidenced workflow is a user chat or upload in the React app, a request to the FastAPI backend, then RAG retrieval and answer generation for chat; uploads are parsed and sent for analysis. The README documents a modular ingestion and retrieval stack, while sampled source also shows a separate direct-upload and streaming-chat implementation. Several frontend/backend endpoint contracts differ, so those links are not treated as verified. Unsampled service connections are shown only where documented, and then as unverified.

Main entry points: [`backend/app/main.py`](https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/main.py) (Chat and upload API), [`backend/app/rag/ingest.py`](https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/rag/ingest.py) (Document ingestion), [`backend/app/rag/rag_pipeline.py`](https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/rag/rag_pipeline.py) (RAG response logic), [`frontend/src/App.js`](https://github.com/24p61a6662-stack/legal_document/blob/main/frontend/src/App.js) (Chat app)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["User interface"]
  node_app["Chat app<br/>[App.js]"]
  node_chatwindow["Chat window<br/>[ChatWindow.js]"]
  node_messagebubble["Message display<br/>[MessageBubble.js]"]
  node_sidebar["Chat history UI<br/>[Sidebar.js]"]
  node_featureui["Analysis panels"]
  node_uploadui["Contract upload UI<br/>[UploadContract.js]"]
  node_apiclient["API client<br/>[api.js]"]
end

subgraph group_api["API and identity"]
  node_fastapi["Chat and upload API<br/>[main.py]"]
  node_extra_routes["Additional API routes<br/>[routes.py]"]
  node_authroutes["Account routes<br/>[auth_routes.py]"]
  node_authhandler["Token and passwords<br/>[auth_handler.py]"]
end

subgraph group_rag["Document Q&amp;A"]
  node_ragentry["RAG response logic<br/>[rag_pipeline.py]"]
  node_ingest["Document ingestion<br/>[ingest.py]"]
  node_chunker["Text chunking<br/>[chunker.py]"]
  node_embedder["Embedding generation"]
  node_vectorstore[("Vector store<br/>[vector_store.py]")]
  node_retriever["Semantic retrieval<br/>[retriever.py]"]
  node_llm["LLM generation<br/>[llm_service.py]"]
end

subgraph group_analysis["Legal analysis"]
  node_loader["File parsing<br/>[document_loader.py]"]
  node_risk["Risk review<br/>[risk_analyzer.py]"]
  node_compliance["Compliance review"]
  node_comparison["Contract comparison"]
end

subgraph group_state["Conversation state"]
  node_historydb[("Chat history database<br/>[main.py]")]
end

node_user(("Legal assistant user"))
node_ollama{{"Ollama model service"}}

node_user -->|"asks or uploads"| node_app
node_app -->|"streams chat"| node_fastapi
node_app -->|"posts upload"| node_fastapi
node_user -->|"enters query"| node_chatwindow
node_chatwindow -->|"renders messages"| node_messagebubble
node_user -.->|"selects document"| node_uploadui
node_uploadui -.->|"requests upload"| node_apiclient
node_featureui -->|"requests analysis"| node_apiclient
node_apiclient -.->|"calls API"| node_fastapi
node_fastapi -->|"gets RAG response"| node_ragentry
node_extra_routes -->|"gets RAG response"| node_ragentry
node_fastapi -.->|"parses upload"| node_loader
node_fastapi -->|"requests analysis"| node_ollama
node_fastapi -->|"reads and writes"| node_historydb
node_authroutes -->|"uses credentials"| node_authhandler
node_ragentry -.->|"retrieves context"| node_retriever
node_retriever -->|"queries vectors"| node_vectorstore
node_ingest -.->|"loads documents"| node_loader
node_ingest -.->|"chunks text"| node_chunker
node_ingest -.->|"embeds text"| node_embedder
node_ingest -.->|"stores vectors"| node_vectorstore
node_ragentry -->|"generates answer"| node_ollama
node_llm -->|"generates response"| node_ollama
node_fastapi -.->|"requests review"| node_risk
node_fastapi -.->|"requests check"| node_compliance
node_fastapi -.->|"requests comparison"| node_comparison

click node_app "https://github.com/24p61a6662-stack/legal_document/blob/main/frontend/src/App.js"
click node_chatwindow "https://github.com/24p61a6662-stack/legal_document/blob/main/frontend/src/components/ChatWindow.js"
click node_messagebubble "https://github.com/24p61a6662-stack/legal_document/blob/main/frontend/src/components/MessageBubble.js"
click node_sidebar "https://github.com/24p61a6662-stack/legal_document/blob/main/frontend/src/components/Sidebar.js"
click node_featureui "https://github.com/24p61a6662-stack/legal_document/blob/main/frontend/src/components/CompareContracts.js"
click node_uploadui "https://github.com/24p61a6662-stack/legal_document/blob/main/frontend/src/components/UploadContract.js"
click node_apiclient "https://github.com/24p61a6662-stack/legal_document/blob/main/frontend/src/api/api.js"
click node_fastapi "https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/main.py"
click node_extra_routes "https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/api/routes.py"
click node_authroutes "https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/auth/auth_routes.py"
click node_authhandler "https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/auth/auth_handler.py"
click node_loader "https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/services/document_loader.py"
click node_ragentry "https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/rag/rag_pipeline.py"
click node_ingest "https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/rag/ingest.py"
click node_chunker "https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/services/chunker.py"
click node_embedder "https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/services/embedder_service.py"
click node_vectorstore "https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/services/vector_store.py"
click node_retriever "https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/services/retriever.py"
click node_llm "https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/services/llm_service.py"
click node_risk "https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/services/risk_analyzer.py"
click node_compliance "https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/services/compliance_checker.py"
click node_comparison "https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/services/contract_comparator.py"
click node_historydb "https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/main.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_app,node_chatwindow,node_messagebubble,node_sidebar,node_featureui,node_uploadui,node_apiclient,node_user toneBlue
class node_fastapi,node_extra_routes,node_authroutes,node_authhandler toneAmber
class node_ragentry,node_ingest,node_chunker,node_embedder,node_vectorstore,node_retriever,node_llm toneMint
class node_loader,node_risk,node_compliance,node_comparison toneRose
class node_historydb,node_ollama toneIndigo
```

## Components

### User interface

- [Chat app](https://github.com/24p61a6662-stack/legal_document/blob/main/frontend/src/App.js): `frontend/src/App.js`
- [Chat window](https://github.com/24p61a6662-stack/legal_document/blob/main/frontend/src/components/ChatWindow.js): `frontend/src/components/ChatWindow.js`
- [Message display](https://github.com/24p61a6662-stack/legal_document/blob/main/frontend/src/components/MessageBubble.js): `frontend/src/components/MessageBubble.js`
- [Chat history UI](https://github.com/24p61a6662-stack/legal_document/blob/main/frontend/src/components/Sidebar.js): `frontend/src/components/Sidebar.js`
- [Analysis panels](https://github.com/24p61a6662-stack/legal_document/blob/main/frontend/src/components/CompareContracts.js): `frontend/src/components/CompareContracts.js`
- [Contract upload UI](https://github.com/24p61a6662-stack/legal_document/blob/main/frontend/src/components/UploadContract.js): `frontend/src/components/UploadContract.js`
- [API client](https://github.com/24p61a6662-stack/legal_document/blob/main/frontend/src/api/api.js): `frontend/src/api/api.js`

### API and identity

- [Chat and upload API](https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/main.py): `backend/app/main.py`
- [Additional API routes](https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/api/routes.py): `backend/app/api/routes.py`
- [Account routes](https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/auth/auth_routes.py): `backend/app/auth/auth_routes.py`
- [Token and passwords](https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/auth/auth_handler.py): `backend/app/auth/auth_handler.py`

### Document Q&A

- [RAG response logic](https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/rag/rag_pipeline.py): `backend/app/rag/rag_pipeline.py`
- [Document ingestion](https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/rag/ingest.py): `backend/app/rag/ingest.py`
- [Text chunking](https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/services/chunker.py): `backend/app/services/chunker.py`
- [Embedding generation](https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/services/embedder_service.py): `backend/app/services/embedder_service.py`
- [Vector store](https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/services/vector_store.py): `backend/app/services/vector_store.py`
- [Semantic retrieval](https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/services/retriever.py): `backend/app/services/retriever.py`
- [LLM generation](https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/services/llm_service.py): `backend/app/services/llm_service.py`

### Legal analysis

- [File parsing](https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/services/document_loader.py): `backend/app/services/document_loader.py`
- [Risk review](https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/services/risk_analyzer.py): `backend/app/services/risk_analyzer.py`
- [Compliance review](https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/services/compliance_checker.py): `backend/app/services/compliance_checker.py`
- [Contract comparison](https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/services/contract_comparator.py): `backend/app/services/contract_comparator.py`

### Conversation state

- [Chat history database](https://github.com/24p61a6662-stack/legal_document/blob/main/backend/app/main.py): `backend/app/main.py`

### Other components

- **Legal assistant user**
- **Ollama model service**

## Connections

- Legal assistant user → Chat app: asks or uploads (evidence: `frontend/src/App.js`)
- Chat app → Chat and upload API: streams chat (evidence: `frontend/src/App.js`)
- Chat app → Chat and upload API: posts upload (evidence: `frontend/src/App.js`)
- Legal assistant user → Chat window: enters query (evidence: `frontend/src/components/ChatWindow.js`)
- Chat window → Message display: renders messages (evidence: `frontend/src/components/ChatWindow.js`)
- Legal assistant user → Contract upload UI: selects document
- Contract upload UI → API client: requests upload (evidence: `frontend/src/api/api.js`)
- Analysis panels → API client: requests analysis (evidence: `frontend/src/components/CompareContracts.js`)
- API client → Chat and upload API: calls API
- Chat and upload API → RAG response logic: gets RAG response (evidence: `backend/app/main.py`)
- Additional API routes → RAG response logic: gets RAG response (evidence: `backend/app/api/routes.py`)
- Chat and upload API → File parsing: parses upload
- Chat and upload API → Ollama model service: requests analysis (evidence: `backend/app/main.py`)
- Chat and upload API → Chat history database: reads and writes (evidence: `backend/app/main.py`)
- Account routes → Token and passwords: uses credentials (evidence: `backend/app/auth/auth_routes.py`)
- RAG response logic → Semantic retrieval: retrieves context
- Semantic retrieval → Vector store: queries vectors (evidence: `backend/app/services/retriever.py`)
- Document ingestion → File parsing: loads documents
- Document ingestion → Text chunking: chunks text
- Document ingestion → Embedding generation: embeds text
- Document ingestion → Vector store: stores vectors
- RAG response logic → Ollama model service: generates answer (evidence: `backend/app/rag/rag_pipeline.py`)
- LLM generation → Ollama model service: generates response (evidence: `backend/app/services/llm_service.py`)
- Chat and upload API → Risk review: requests review
- Chat and upload API → Compliance review: requests check
- Chat and upload API → Contract comparison: requests comparison
