# david8575/legal_graph_rag architecture

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

- Interactive diagram: https://gitdiagram.com/david8575/legal_graph_rag
- Repository: https://github.com/david8575/legal_graph_rag

## Overview

This is a FastAPI backend for a Korean legal graph-RAG service. Its main workflow fetches law records and article XML, normalizes articles, chunks them, and stores legal entities in Neo4j; separate indexing embeds chunks into Qdrant. Answer requests embed a query, retrieve similar chunks, enrich them with graph context, build a grounded prompt, and call an LLM. The graph follows calls visible in the sampled sources; broader capabilities such as case and issue schema are represented only where their implementation is evident.

Main entry points: [`backend/app/main.py`](https://github.com/david8575/legal_graph_rag/blob/main/backend/app/main.py) (FastAPI routes), [`backend/app/law_api.py`](https://github.com/david8575/legal_graph_rag/blob/main/backend/app/law_api.py) (Law API adapter), [`backend/app/models.py`](https://github.com/david8575/legal_graph_rag/blob/main/backend/app/models.py) (Legal data models), [`backend/app/config.py`](https://github.com/david8575/legal_graph_rag/blob/main/backend/app/config.py) (Runtime settings)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API orchestration"]
  node_api["FastAPI routes<br/>[main.py]"]
  node_models["Legal data models<br/>[models.py]"]
end

subgraph group_ingestion["Legal ingestion"]
  node_law_api["Law API adapter<br/>[law_api.py]"]
  node_chunking["Article chunking<br/>[chunking.py]"]
end

subgraph group_rag["Retrieval and answers"]
  node_embedding["Text embedding<br/>[embedding.py]"]
  node_prompt["Grounded prompt builder<br/>[main.py]"]
  node_llm["Answer generation<br/>[llm.py]"]
end

subgraph group_platform["Data and integrations"]
  node_config["Runtime settings<br/>[config.py]"]
  node_graph_repo["Graph repository"]
  node_neo4j[("Neo4j graph")]
  node_graph_schema["Graph schema setup<br/>[graph_schema.py]"]
  node_qdrant_repo["Vector repository"]
  node_qdrant[("Qdrant vectors")]
  node_context["Article context lookup"]
  node_health["Dependency checks<br/>[db.py]"]
end

node_admin(("Admin client"))
node_law_service["Korean law service"]
node_ollama_embed["Ollama embedding API"]
node_ollama_llm["Ollama generation API"]

node_admin -->|"calls"| node_api
node_api -->|"fetches laws"| node_law_api
node_law_api -->|"requests records"| node_law_service
node_api -->|"chunks articles"| node_chunking
node_api -->|"stores and lists"| node_graph_repo
node_graph_repo -->|"reads and writes"| node_neo4j
node_api -->|"applies schema"| node_graph_schema
node_graph_schema -->|"creates indexes"| node_neo4j
node_api -->|"embeds text"| node_embedding
node_embedding -->|"requests vectors"| node_ollama_embed
node_api -->|"indexes and searches"| node_qdrant_repo
node_qdrant_repo -->|"stores and queries"| node_qdrant
node_api -->|"loads article context"| node_context
node_context -->|"reads graph"| node_neo4j
node_api -->|"prepares evidence"| node_prompt
node_api -->|"generates answer"| node_llm
node_llm -->|"requests answer"| node_ollama_llm
node_api -->|"checks dependencies"| node_health
node_health -->|"checks connection"| node_neo4j
node_health -->|"checks collections"| node_qdrant
node_law_api -->|"creates legal records"| node_models
node_chunking -->|"creates chunks"| node_models
node_graph_repo -->|"persists legal entities"| node_models
node_qdrant_repo -->|"indexes chunks"| node_models
node_api -->|"reads settings"| node_config
node_law_api -->|"reads API key"| node_config
node_embedding -->|"reads model settings"| node_config
node_llm -->|"reads model settings"| node_config

click node_api "https://github.com/david8575/legal_graph_rag/blob/main/backend/app/main.py"
click node_models "https://github.com/david8575/legal_graph_rag/blob/main/backend/app/models.py"
click node_config "https://github.com/david8575/legal_graph_rag/blob/main/backend/app/config.py"
click node_law_api "https://github.com/david8575/legal_graph_rag/blob/main/backend/app/law_api.py"
click node_chunking "https://github.com/david8575/legal_graph_rag/blob/main/backend/app/chunking.py"
click node_graph_repo "https://github.com/david8575/legal_graph_rag/blob/main/backend/app/graph_repository.py"
click node_graph_schema "https://github.com/david8575/legal_graph_rag/blob/main/backend/app/graph_schema.py"
click node_qdrant_repo "https://github.com/david8575/legal_graph_rag/blob/main/backend/app/qdrant_repository.py"
click node_embedding "https://github.com/david8575/legal_graph_rag/blob/main/backend/app/embedding.py"
click node_context "https://github.com/david8575/legal_graph_rag/blob/main/backend/app/graph_repository.py"
click node_prompt "https://github.com/david8575/legal_graph_rag/blob/main/backend/app/main.py"
click node_llm "https://github.com/david8575/legal_graph_rag/blob/main/backend/app/llm.py"
click node_health "https://github.com/david8575/legal_graph_rag/blob/main/backend/app/db.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_api,node_models,node_admin toneBlue
class node_law_api,node_chunking toneAmber
class node_embedding,node_prompt,node_llm,node_ollama_embed,node_ollama_llm toneMint
class node_config,node_graph_repo,node_neo4j,node_graph_schema,node_qdrant_repo,node_qdrant,node_context,node_health toneRose
class node_law_service toneIndigo
```

## Components

### API orchestration

- [FastAPI routes](https://github.com/david8575/legal_graph_rag/blob/main/backend/app/main.py): `backend/app/main.py`
- [Legal data models](https://github.com/david8575/legal_graph_rag/blob/main/backend/app/models.py): `backend/app/models.py`

### Legal ingestion

- [Law API adapter](https://github.com/david8575/legal_graph_rag/blob/main/backend/app/law_api.py): `backend/app/law_api.py`
- [Article chunking](https://github.com/david8575/legal_graph_rag/blob/main/backend/app/chunking.py): `backend/app/chunking.py`

### Retrieval and answers

- [Text embedding](https://github.com/david8575/legal_graph_rag/blob/main/backend/app/embedding.py): `backend/app/embedding.py`
- [Grounded prompt builder](https://github.com/david8575/legal_graph_rag/blob/main/backend/app/main.py): `backend/app/main.py`
- [Answer generation](https://github.com/david8575/legal_graph_rag/blob/main/backend/app/llm.py): `backend/app/llm.py`

### Data and integrations

- [Runtime settings](https://github.com/david8575/legal_graph_rag/blob/main/backend/app/config.py): `backend/app/config.py`
- [Graph repository](https://github.com/david8575/legal_graph_rag/blob/main/backend/app/graph_repository.py): `backend/app/graph_repository.py`
- **Neo4j graph**
- [Graph schema setup](https://github.com/david8575/legal_graph_rag/blob/main/backend/app/graph_schema.py): `backend/app/graph_schema.py`
- [Vector repository](https://github.com/david8575/legal_graph_rag/blob/main/backend/app/qdrant_repository.py): `backend/app/qdrant_repository.py`
- **Qdrant vectors**
- [Article context lookup](https://github.com/david8575/legal_graph_rag/blob/main/backend/app/graph_repository.py): `backend/app/graph_repository.py`
- [Dependency checks](https://github.com/david8575/legal_graph_rag/blob/main/backend/app/db.py): `backend/app/db.py`

### Other components

- **Admin client**
- **Korean law service**
- **Ollama embedding API**
- **Ollama generation API**

## Connections

- Admin client → FastAPI routes: calls (evidence: `backend/app/main.py`)
- FastAPI routes → Law API adapter: fetches laws (evidence: `backend/app/main.py`)
- Law API adapter → Korean law service: requests records (evidence: `backend/app/law_api.py`)
- FastAPI routes → Article chunking: chunks articles (evidence: `backend/app/main.py`)
- FastAPI routes → Graph repository: stores and lists (evidence: `backend/app/main.py`)
- Graph repository → Neo4j graph: reads and writes (evidence: `backend/app/graph_repository.py`)
- FastAPI routes → Graph schema setup: applies schema (evidence: `backend/app/main.py`)
- Graph schema setup → Neo4j graph: creates indexes (evidence: `backend/app/graph_schema.py`)
- FastAPI routes → Text embedding: embeds text (evidence: `backend/app/main.py`)
- Text embedding → Ollama embedding API: requests vectors (evidence: `backend/app/embedding.py`)
- FastAPI routes → Vector repository: indexes and searches (evidence: `backend/app/main.py`)
- Vector repository → Qdrant vectors: stores and queries (evidence: `backend/app/qdrant_repository.py`)
- FastAPI routes → Article context lookup: loads article context (evidence: `backend/app/main.py`)
- Article context lookup → Neo4j graph: reads graph (evidence: `backend/app/graph_repository.py`)
- FastAPI routes → Grounded prompt builder: prepares evidence (evidence: `backend/app/main.py`)
- FastAPI routes → Answer generation: generates answer (evidence: `backend/app/main.py`)
- Answer generation → Ollama generation API: requests answer (evidence: `backend/app/llm.py`)
- FastAPI routes → Dependency checks: checks dependencies (evidence: `backend/app/main.py`)
- Dependency checks → Neo4j graph: checks connection (evidence: `backend/app/db.py`)
- Dependency checks → Qdrant vectors: checks collections (evidence: `backend/app/db.py`)
- Law API adapter → Legal data models: creates legal records (evidence: `backend/app/law_api.py`)
- Article chunking → Legal data models: creates chunks (evidence: `backend/app/chunking.py`)
- Graph repository → Legal data models: persists legal entities (evidence: `backend/app/graph_repository.py`)
- Vector repository → Legal data models: indexes chunks (evidence: `backend/app/qdrant_repository.py`)
- FastAPI routes → Runtime settings: reads settings (evidence: `backend/app/main.py`)
- Law API adapter → Runtime settings: reads API key (evidence: `backend/app/law_api.py`)
- Text embedding → Runtime settings: reads model settings (evidence: `backend/app/embedding.py`)
- Answer generation → Runtime settings: reads model settings (evidence: `backend/app/llm.py`)
