How xmudeeplit/legalgraphrag works
Purpose and entry points
LegalGraphRAG is a Python evaluation framework for criminal legal judgment prediction. It builds a feature/case graph, retrieves graph-grounded legal context, and uses LLM agents to predict charges, applicable law articles, and imprisonment. The operational entry point is run.py; offline scoring is evaluation/evaluate_results.py.
Orchestration and execution
run.py loads dotenv configuration, selects a model/runtime, optionally builds or loads a serialized graph, distributes cases across requested GPU devices, and writes combined per-dataset predictions and run statistics. The central application coordinator is core/LegalGraphRAG.py, which connects preprocessing, retrieval, prompts, and judgment stages.
Grounding: run.py, core/LegalGraphRAG.py, configs/main.env.
Case preprocessing and feature extraction
Raw or evaluation cases are segmented, pre-judged, and converted into structured legal features used as graph nodes/retrieval keys. The preparation pipeline can generate sampled CAIL inputs, case features, law-dependency hints, and merged law-to-crime resources before inference.
Grounding: core/preprocess/case_seg.py, core/preprocess/get_features.py, core/preprocess/preJudge.py; orchestration scripts: scripts/prepare_data.py, scripts/prepare_case_features.py.
Graph construction and persistence
The graph layer converts feature-enriched historical cases and law resources into a retrievable graph database. It uses embeddings during construction/retrieval and persists the graph as a pickle artifact so parallel inference workers can reload a common read-only-ish graph rather than rebuild it. Rebuilds are required when graph-construction model/configuration changes.
Grounding: core/graph_construct/feature_graph.py, core/graph_construct/graph_db.py, datas/main_experiment/cases_with_feature_big.json.
Retrieval-to-judgment pipeline
For each target case, the system uses structured features to retrieve relevant precedent/law graph context, then routes that evidence through specialized legal decision agents. Crime and law judgments are separate boundaries, allowing charge reasoning and statutory-article reasoning to be independently produced before the final result schema is emitted.
Grounding: core/LegalGraphRAG.py, core/judge/judge_crime.py, core/judge/judge_law.py.
Model and prompt boundary
A common model abstraction supports local Hugging Face Transformers models and OpenAI-compatible API models. Model-specific implementations encapsulate loading/inference details; prompt packages separate graph, retrieval, preprocessing, and judge prompt families, with Chinese/English selection configured externally.
Grounding: core/models/base.py, core/models/transformers_base.py, core/models/openai_base.py; examples: core/models/transformers/qwen3_model.py, core/models/openai/gpt4o_mini.py.
Data assets, infrastructure, and evaluation
The main experiment consumes a 14,049-case graph corpus plus CAIL and CMDL evaluation inputs, criminal-law records, and crime-category metadata. It requires Python 3.10+, CUDA/PyTorch for local large models and multi-GPU execution; embeddings come from an externally running Ollama-compatible endpoint (default localhost:11434/api/embed, bge-m3). API-backed models additionally require configured credentials. Metrics evaluate charge/law exact match and Micro-F1, plus sentence exact match and month MAE.
Grounding: datas/main_experiment/crime_data_CAIL_small.json, datas/main_experiment/crime_data_CMDL_small.json, datas/main_experiment/criminal_law_processed.json, evaluation/evaluate_results.py.
Main entry points: LegalGraphRAG.py LegalGraphRAG coordinator · run.py Run entry point · feature_graph.py Feature graph builder · base.py Model abstraction
Components 19
Execution
- Run entry point · Selects runtime, distributes cases across GPUs, and writes predictions.
run.py
Knowledge Build
- Case segmentation
core/preprocess/case_seg.py - Legal feature extraction · Produces structured retrieval keys and graph-node features.
core/preprocess/get_features.py - Pre-judgment
core/preprocess/preJudge.py - Preparation scripts · Creates sampled inputs, features, and law-dependency resources.
scripts/prepare_data.py - Case feature preparation
scripts/prepare_case_features.py - Historical case corpus · Feature-enriched source corpus for the precedent graph.
datas/main_experiment/cases_with_feature_big.json - Feature graph builder
core/graph_construct/feature_graph.py - Persistent graph database · Serializes a shared graph artifact for inference workers.
core/graph_construct/graph_db.py
RAG Judgment
- LegalGraphRAG coordinator · Connects preprocessing, graph retrieval, prompts, and judgment.
core/LegalGraphRAG.py - Graph-grounded retrieval · Finds relevant precedent and law context from case features.
- Crime judgment agent · Predicts criminal charges from retrieved evidence.
core/judge/judge_crime.py - Law judgment agent · Predicts applicable statutory articles.
core/judge/judge_law.py
Model Boundary
- Model abstraction · Unifies local Transformers and OpenAI-compatible models.
core/models/base.py - Transformers runtime
core/models/transformers_base.py - OpenAI-compatible runtime
core/models/openai_base.py - Embedding endpoint · Ollama-compatible embedding service used for graph construction and retrieval.
Evaluation
- CAIL and CMDL inputs
datas/main_experiment/crime_data_CAIL_small.json - Offline evaluator · Scores charge/law matches, Micro-F1, sentence match, and month MAE.
evaluation/evaluate_results.py
Connections 23
- Run entry point → LegalGraphRAG coordinator: starts
- Run entry point → Persistent graph database: loads or rebuilds
- Run entry point → CAIL and CMDL inputs: distributes cases from
- Preparation scripts → Case feature preparation: feeds
- Case feature preparation → Case segmentation: uses
- Case segmentation → Legal feature extraction: segments for
- Pre-judgment → Legal feature extraction: enriches
- Historical case corpus → Feature graph builder: source cases
- Legal feature extraction → Feature graph builder: structured features
- Feature graph builder → Persistent graph database: builds
- Embedding endpoint → Feature graph builder: embeddings
- LegalGraphRAG coordinator → Graph-grounded retrieval: retrieves context
- Persistent graph database → Graph-grounded retrieval: searches
- Embedding endpoint → Graph-grounded retrieval: embeddings
- Graph-grounded retrieval → Crime judgment agent: evidence
- Graph-grounded retrieval → Law judgment agent: evidence
- Crime judgment agent → LegalGraphRAG coordinator: charge prediction
- Law judgment agent → LegalGraphRAG coordinator: law prediction
- Model abstraction → Transformers runtime: implements
- Model abstraction → OpenAI-compatible runtime: implements
- LegalGraphRAG coordinator → Model abstraction: invokes
- Run entry point → Offline evaluator: outputs scored by
- CAIL and CMDL inputs → Offline evaluator: ground truth