# framech01/a-law-ai architecture

> Architecture diagram of the GitHub repository framech01/a-law-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/framech01/a-law-ai
- Repository: https://github.com/framech01/a-law-ai

## Overview

A-Law AI is a FastAPI service for lease-contract OCR and analysis, plus a Korean legal RAG chatbot. Spring Boot initiates synchronous REST requests or asynchronous RabbitMQ analysis; contract analysis combines summarization, clause risk checks, and missing-clause detection, while OCR can fetch images from shared S3. The chatbot classifies questions, retrieves and reranks legal sources, generates an answer, and checks citations. Paths and principal calls follow the README and SOURCE INDEX; unsampled subsystem links are shown only where the README documents them.

Main entry points: [`app/api/routers.py`](https://github.com/framech01/a-law-ai/blob/main/app/api/routers.py) (REST API), [`app/rag/graph.py`](https://github.com/framech01/a-law-ai/blob/main/app/rag/graph.py) (Chat workflow), [`app/services/analyzer.py`](https://github.com/framech01/a-law-ai/blob/main/app/services/analyzer.py) (Contract analysis), [`app/rag/pipeline.py`](https://github.com/framech01/a-law-ai/blob/main/app/rag/pipeline.py) (Legal RAG pipeline)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API entrypoints"]
  node_http_api["REST API<br/>[routers.py]"]
  node_contract_api["Contract endpoints<br/>[contract.py]"]
  node_ocr_api["OCR endpoints<br/>[ocr.py]"]
  node_chat_api["Chat endpoints<br/>[chat.py]"]
  node_rag_api["RAG admin endpoints<br/>[rag.py]"]
end

subgraph group_contracts["Contract analysis"]
  node_analysis["Contract analysis<br/>[analyzer.py]"]
  node_risk["Clause risk analysis<br/>[analyzer.py]"]
  node_splitter["Clause splitting<br/>[clause_splitter.py]"]
  node_prefilter["Rule prefilter<br/>[prefilter.py]"]
  node_missing["Missing clauses<br/>[missing.py]"]
  node_summary["Contract summary<br/>[summarizer.py]"]
  node_ocr_service["OCR service<br/>[ocr_service.py]"]
  node_upstage["Upstage OCR pipeline<br/>[upstage_ocr.py]"]
end

subgraph group_legal["Legal assistant"]
  node_chatbot["Chatbot service<br/>[chatbot.py]"]
  node_session[("Chat sessions<br/>[chat_session.py]")]
  node_chat_graph["Chat workflow<br/>[graph.py]"]
  node_domain["Domain classifier<br/>[domain.py]"]
  node_rag_pipeline["Legal RAG pipeline<br/>[pipeline.py]"]
  node_citations["Citation verifier<br/>[citations.py]"]
end

subgraph group_integration["External integrations"]
  node_consumer["RabbitMQ consumer"]
  node_s3_client["S3 client<br/>[s3_client.py]"]
  node_s3[("Shared S3")]
  node_rabbitmq["RabbitMQ"]
  node_pinecone[("Pinecone")]
end

subgraph group_runtime["Shared runtime"]
  node_llm["LLM runtime<br/>[llm.py]"]
  node_config["Configuration<br/>[config.py]"]
end

node_spring(("Spring Boot"))

node_spring -->|"calls REST"| node_http_api
node_http_api -->|"routes requests"| node_contract_api
node_http_api -->|"routes requests"| node_ocr_api
node_http_api -->|"routes requests"| node_chat_api
node_http_api -->|"routes requests"| node_rag_api
node_contract_api -->|"invokes analysis"| node_analysis
node_analysis -->|"generates summary"| node_summary
node_analysis -->|"runs risk checks"| node_risk
node_risk -->|"splits clauses"| node_splitter
node_risk -->|"screens clauses"| node_prefilter
node_risk -->|"checks omissions"| node_missing
node_ocr_api -->|"requests OCR"| node_ocr_service
node_ocr_service -->|"processes images"| node_upstage
node_chat_api -->|"handles chat"| node_chatbot
node_chatbot -->|"loads session"| node_session
node_chatbot -->|"runs workflow"| node_chat_graph
node_chat_graph -->|"classifies query"| node_domain
node_chat_graph -->|"retrieves evidence"| node_rag_pipeline
node_chat_graph -->|"verifies answer"| node_citations
node_rag_api -->|"indexes and searches"| node_rag_pipeline
node_consumer -->|"consumes requests"| node_rabbitmq
node_spring -->|"publishes analysis"| node_rabbitmq
node_consumer -->|"runs analysis"| node_analysis
node_consumer -->|"fetches contract image"| node_s3_client
node_s3_client -->|"reads image"| node_s3
node_consumer -->|"publishes result"| node_rabbitmq
node_rag_pipeline -->|"searches vectors"| node_pinecone
node_rag_pipeline -.->|"uses embeddings"| node_llm
node_analysis -.->|"requests generation"| node_llm
node_chat_graph -->|"generates answer"| node_llm
node_ocr_service -.->|"structures OCR"| node_llm
node_llm -->|"reads settings"| node_config
node_rag_pipeline -->|"reads settings"| node_config

click node_http_api "https://github.com/framech01/a-law-ai/blob/main/app/api/routers.py"
click node_contract_api "https://github.com/framech01/a-law-ai/blob/main/app/api/endpoints/contract.py"
click node_ocr_api "https://github.com/framech01/a-law-ai/blob/main/app/api/endpoints/ocr.py"
click node_chat_api "https://github.com/framech01/a-law-ai/blob/main/app/api/endpoints/chat.py"
click node_rag_api "https://github.com/framech01/a-law-ai/blob/main/app/api/endpoints/rag.py"
click node_analysis "https://github.com/framech01/a-law-ai/blob/main/app/services/analyzer.py"
click node_risk "https://github.com/framech01/a-law-ai/blob/main/app/services/risk/analyzer.py"
click node_splitter "https://github.com/framech01/a-law-ai/blob/main/app/services/risk/clause_splitter.py"
click node_prefilter "https://github.com/framech01/a-law-ai/blob/main/app/services/risk/prefilter.py"
click node_missing "https://github.com/framech01/a-law-ai/blob/main/app/services/risk/missing.py"
click node_summary "https://github.com/framech01/a-law-ai/blob/main/app/services/summarizer.py"
click node_ocr_service "https://github.com/framech01/a-law-ai/blob/main/app/services/ocr/ocr_service.py"
click node_upstage "https://github.com/framech01/a-law-ai/blob/main/app/ocr/upstage_ocr.py"
click node_chatbot "https://github.com/framech01/a-law-ai/blob/main/app/services/chatbot.py"
click node_session "https://github.com/framech01/a-law-ai/blob/main/app/services/chat_session.py"
click node_chat_graph "https://github.com/framech01/a-law-ai/blob/main/app/rag/graph.py"
click node_domain "https://github.com/framech01/a-law-ai/blob/main/app/rag/domain.py"
click node_rag_pipeline "https://github.com/framech01/a-law-ai/blob/main/app/rag/pipeline.py"
click node_citations "https://github.com/framech01/a-law-ai/blob/main/app/rag/citations.py"
click node_consumer "https://github.com/framech01/a-law-ai/blob/main/app/services/rabbitmq_consumer.py"
click node_s3_client "https://github.com/framech01/a-law-ai/blob/main/app/util/s3_client.py"
click node_llm "https://github.com/framech01/a-law-ai/blob/main/app/core/llm.py"
click node_config "https://github.com/framech01/a-law-ai/blob/main/app/core/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_http_api,node_contract_api,node_ocr_api,node_chat_api,node_rag_api toneBlue
class node_analysis,node_risk,node_splitter,node_prefilter,node_missing,node_summary,node_ocr_service,node_upstage toneAmber
class node_chatbot,node_session,node_chat_graph,node_domain,node_rag_pipeline,node_citations toneMint
class node_consumer,node_s3_client,node_s3,node_rabbitmq,node_pinecone toneRose
class node_llm,node_config,node_spring toneIndigo
```

## Components

### API entrypoints

- [REST API](https://github.com/framech01/a-law-ai/blob/main/app/api/routers.py): `app/api/routers.py`
- [Contract endpoints](https://github.com/framech01/a-law-ai/blob/main/app/api/endpoints/contract.py): `app/api/endpoints/contract.py`
- [OCR endpoints](https://github.com/framech01/a-law-ai/blob/main/app/api/endpoints/ocr.py): `app/api/endpoints/ocr.py`
- [Chat endpoints](https://github.com/framech01/a-law-ai/blob/main/app/api/endpoints/chat.py): `app/api/endpoints/chat.py`
- [RAG admin endpoints](https://github.com/framech01/a-law-ai/blob/main/app/api/endpoints/rag.py): `app/api/endpoints/rag.py`

### Contract analysis

- [Contract analysis](https://github.com/framech01/a-law-ai/blob/main/app/services/analyzer.py): `app/services/analyzer.py`
- [Clause risk analysis](https://github.com/framech01/a-law-ai/blob/main/app/services/risk/analyzer.py): `app/services/risk/analyzer.py`
- [Clause splitting](https://github.com/framech01/a-law-ai/blob/main/app/services/risk/clause_splitter.py): `app/services/risk/clause_splitter.py`
- [Rule prefilter](https://github.com/framech01/a-law-ai/blob/main/app/services/risk/prefilter.py): `app/services/risk/prefilter.py`
- [Missing clauses](https://github.com/framech01/a-law-ai/blob/main/app/services/risk/missing.py): `app/services/risk/missing.py`
- [Contract summary](https://github.com/framech01/a-law-ai/blob/main/app/services/summarizer.py): `app/services/summarizer.py`
- [OCR service](https://github.com/framech01/a-law-ai/blob/main/app/services/ocr/ocr_service.py): `app/services/ocr/ocr_service.py`
- [Upstage OCR pipeline](https://github.com/framech01/a-law-ai/blob/main/app/ocr/upstage_ocr.py): `app/ocr/upstage_ocr.py`

### Legal assistant

- [Chatbot service](https://github.com/framech01/a-law-ai/blob/main/app/services/chatbot.py): `app/services/chatbot.py`
- [Chat sessions](https://github.com/framech01/a-law-ai/blob/main/app/services/chat_session.py): `app/services/chat_session.py`
- [Chat workflow](https://github.com/framech01/a-law-ai/blob/main/app/rag/graph.py): `app/rag/graph.py`
- [Domain classifier](https://github.com/framech01/a-law-ai/blob/main/app/rag/domain.py): `app/rag/domain.py`
- [Legal RAG pipeline](https://github.com/framech01/a-law-ai/blob/main/app/rag/pipeline.py): `app/rag/pipeline.py`
- [Citation verifier](https://github.com/framech01/a-law-ai/blob/main/app/rag/citations.py): `app/rag/citations.py`

### External integrations

- [RabbitMQ consumer](https://github.com/framech01/a-law-ai/blob/main/app/services/rabbitmq_consumer.py): `app/services/rabbitmq_consumer.py`
- [S3 client](https://github.com/framech01/a-law-ai/blob/main/app/util/s3_client.py): `app/util/s3_client.py`
- **Shared S3**
- **RabbitMQ**
- **Pinecone**

### Shared runtime

- [LLM runtime](https://github.com/framech01/a-law-ai/blob/main/app/core/llm.py): `app/core/llm.py`
- [Configuration](https://github.com/framech01/a-law-ai/blob/main/app/core/config.py): `app/core/config.py`

### Other components

- **Spring Boot**

## Connections

- Spring Boot → REST API: calls REST (evidence: `README.md`)
- REST API → Contract endpoints: routes requests (evidence: `app/api/routers.py`)
- REST API → OCR endpoints: routes requests (evidence: `app/api/routers.py`)
- REST API → Chat endpoints: routes requests (evidence: `app/api/routers.py`)
- REST API → RAG admin endpoints: routes requests (evidence: `app/api/routers.py`)
- Contract endpoints → Contract analysis: invokes analysis (evidence: `app/api/endpoints/contract.py`)
- Contract analysis → Contract summary: generates summary (evidence: `app/services/analyzer.py`)
- Contract analysis → Clause risk analysis: runs risk checks (evidence: `app/services/analyzer.py`)
- Clause risk analysis → Clause splitting: splits clauses (evidence: `app/services/risk/analyzer.py`)
- Clause risk analysis → Rule prefilter: screens clauses (evidence: `app/services/risk/analyzer.py`)
- Clause risk analysis → Missing clauses: checks omissions (evidence: `app/services/risk/analyzer.py`)
- OCR endpoints → OCR service: requests OCR (evidence: `app/api/endpoints/ocr.py`)
- OCR service → Upstage OCR pipeline: processes images (evidence: `app/services/ocr/ocr_service.py`)
- Chat endpoints → Chatbot service: handles chat (evidence: `app/api/endpoints/chat.py`)
- Chatbot service → Chat sessions: loads session (evidence: `app/services/chatbot.py`)
- Chatbot service → Chat workflow: runs workflow (evidence: `app/services/chatbot.py`)
- Chat workflow → Domain classifier: classifies query (evidence: `app/rag/graph.py`)
- Chat workflow → Legal RAG pipeline: retrieves evidence (evidence: `app/rag/graph.py`)
- Chat workflow → Citation verifier: verifies answer (evidence: `app/rag/graph.py`)
- RAG admin endpoints → Legal RAG pipeline: indexes and searches (evidence: `app/api/endpoints/rag.py`)
- RabbitMQ consumer → RabbitMQ: consumes requests (evidence: `README.md`)
- Spring Boot → RabbitMQ: publishes analysis (evidence: `README.md`)
- RabbitMQ consumer → Contract analysis: runs analysis (evidence: `app/services/rabbitmq_consumer.py`)
- RabbitMQ consumer → S3 client: fetches contract image (evidence: `app/services/rabbitmq_consumer.py`)
- S3 client → Shared S3: reads image (evidence: `app/util/s3_client.py`)
- RabbitMQ consumer → RabbitMQ: publishes result (evidence: `README.md`)
- Legal RAG pipeline → Pinecone: searches vectors (evidence: `app/rag/pipeline.py`)
- Legal RAG pipeline → LLM runtime: uses embeddings
- Contract analysis → LLM runtime: requests generation (evidence: `app/services/analyzer.py`)
- Chat workflow → LLM runtime: generates answer (evidence: `app/rag/graph.py`)
- OCR service → LLM runtime: structures OCR
- LLM runtime → Configuration: reads settings (evidence: `app/core/llm.py`)
- Legal RAG pipeline → Configuration: reads settings (evidence: `app/rag/pipeline.py`)
