# haolpku/k12-kgraph architecture

> Architecture diagram of the GitHub repository haolpku/k12-kgraph, 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/haolpku/k12-kgraph
- Repository: https://github.com/haolpku/k12-kgraph

## Overview

K12-KGraph builds a curriculum-aligned knowledge graph from textbooks, then derives benchmark questions and instruction-training data; its evaluation runner assesses models on the benchmark. The main documented route is textbook preparation, sectioning, extraction, merging, exercise enrichment, and graph checks. The README also documents the benchmark, SFT-data, and evaluation capabilities. The KG orchestrator and selected stage/helper calls are supported by excerpts and the SOURCE INDEX; several downstream components are unsampled, so their internal wiring is not expanded.

Main entry points: [`src/kg/run_pipeline.py`](https://github.com/haolpku/k12-kgraph/blob/main/src/kg/run_pipeline.py) (KG orchestrator), [`src/kg/extract_kg_from_textbook.py`](https://github.com/haolpku/k12-kgraph/blob/main/src/kg/extract_kg_from_textbook.py) (Extract graph), [`src/kg/merge_kg.py`](https://github.com/haolpku/k12-kgraph/blob/main/src/kg/merge_kg.py) (Curriculum graph), [`src/sft_qa`](https://github.com/haolpku/k12-kgraph/tree/main/src/sft_qa) (SFT QA generation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_kg["Graph construction"]
  node_kg_runner["KG orchestrator<br/>[run_pipeline.py]"]
  node_prepare["Prepare textbooks"]
  node_segment["Segment sections"]
  node_extract["Extract graph"]
  node_merge["Merge graphs<br/>[merge_kg.py]"]
  node_exercises["Enrich exercises"]
  node_cycles["Check cycles<br/>[check_cycles.py]"]
  node_kgdata[("Curriculum graph<br/>[merge_kg.py]")]
end

subgraph group_data["Dataset generation"]
  node_bench["Benchmark generation"]
  node_sft["SFT QA generation"]
  node_exercise_qa["Exercise QA<br/>[exercise_to_qa.py]"]
  node_qa_generation["Node-edge QA<br/>[generate_qa.py]"]
  node_qa_merge["Training data assembly"]
  node_benchdata[("K12-Bench")]
  node_traindata[("K12-Train")]
end

subgraph group_eval["Model evaluation"]
  node_evaluation["Multiselect evaluation"]
end

subgraph group_shared["Shared runtime"]
  node_config["Pipeline config<br/>[config.py]"]
  node_env["Environment loader<br/>[envfile.py]"]
  node_io["JSON IO<br/>[io.py]"]
  node_llm["LLM client<br/>[llm_client.py]"]
  node_ids["Graph identifiers<br/>[k12_ids.py]"]
  node_common["SFT shared helpers<br/>[common.py]"]
end

node_user(("Pipeline user"))
node_textbooks["Textbooks"]
node_mineru["MinerU parser"]
node_model_user(("Model evaluator"))
node_models["Model API"]

node_user -->|"runs"| node_kg_runner
node_textbooks -->|"parsed by"| node_mineru
node_mineru -->|"provides input"| node_prepare
node_kg_runner -->|"runs"| node_prepare
node_kg_runner -->|"runs"| node_segment
node_kg_runner -->|"runs"| node_extract
node_kg_runner -->|"runs"| node_merge
node_kg_runner -->|"runs"| node_exercises
node_kg_runner -->|"runs"| node_cycles
node_prepare -->|"prepares sections"| node_segment
node_segment -->|"segments for"| node_extract
node_extract -->|"extracts for"| node_merge
node_merge -->|"produces"| node_kgdata
node_exercises -->|"enriches"| node_kgdata
node_extract -->|"requests generation"| node_llm
node_exercises -->|"requests enrichment"| node_llm
node_kg_runner -->|"derives benchmark"| node_bench
node_kg_runner -->|"derives training data"| node_sft
node_kgdata -->|"queries graph"| node_bench
node_kgdata -->|"grounds QA"| node_qa_generation
node_kgdata -.->|"supplies exercises"| node_exercise_qa
node_sft -.->|"generates shards"| node_qa_generation
node_sft -.->|"generates exercise QA"| node_exercise_qa
node_sft -.->|"assembles pairs"| node_qa_merge
node_bench -->|"produces"| node_benchdata
node_qa_merge -.->|"writes pairs"| node_traindata
node_model_user -->|"runs evaluation"| node_evaluation
node_benchdata -->|"evaluates on"| node_evaluation
node_evaluation -->|"queries"| node_models
node_evaluation -->|"loads environment"| node_env
node_extract -->|"uses settings"| node_config
node_extract -->|"uses serialization"| node_io
node_merge -->|"uses identifiers"| node_ids
node_qa_generation -->|"uses helpers"| node_common
node_common -->|"loads config"| node_config

click node_kg_runner "https://github.com/haolpku/k12-kgraph/blob/main/src/kg/run_pipeline.py"
click node_prepare "https://github.com/haolpku/k12-kgraph/blob/main/src/kg/prepare_textbooks.py"
click node_segment "https://github.com/haolpku/k12-kgraph/blob/main/src/kg/segment_textbooks.py"
click node_extract "https://github.com/haolpku/k12-kgraph/blob/main/src/kg/extract_kg_from_textbook.py"
click node_merge "https://github.com/haolpku/k12-kgraph/blob/main/src/kg/merge_kg.py"
click node_exercises "https://github.com/haolpku/k12-kgraph/blob/main/src/kg/build_afterclass_exercises.py"
click node_cycles "https://github.com/haolpku/k12-kgraph/blob/main/src/kg/check_cycles.py"
click node_kgdata "https://github.com/haolpku/k12-kgraph/blob/main/src/kg/merge_kg.py"
click node_bench "https://github.com/haolpku/k12-kgraph/tree/main/src/benchmark"
click node_sft "https://github.com/haolpku/k12-kgraph/tree/main/src/sft_qa"
click node_exercise_qa "https://github.com/haolpku/k12-kgraph/blob/main/src/sft_qa/exercise_to_qa.py"
click node_qa_generation "https://github.com/haolpku/k12-kgraph/blob/main/src/sft_qa/generate_qa.py"
click node_qa_merge "https://github.com/haolpku/k12-kgraph/blob/main/src/sft_qa/build_train_jsonl.py"
click node_benchdata "https://github.com/haolpku/k12-kgraph/tree/main/src/benchmark"
click node_traindata "https://github.com/haolpku/k12-kgraph/tree/main/src/sft_qa"
click node_evaluation "https://github.com/haolpku/k12-kgraph/blob/main/eval/eval_multiselect.py"
click node_config "https://github.com/haolpku/k12-kgraph/blob/main/src/utils/config.py"
click node_env "https://github.com/haolpku/k12-kgraph/blob/main/src/utils/envfile.py"
click node_io "https://github.com/haolpku/k12-kgraph/blob/main/src/utils/io.py"
click node_llm "https://github.com/haolpku/k12-kgraph/blob/main/src/utils/llm_client.py"
click node_ids "https://github.com/haolpku/k12-kgraph/blob/main/src/utils/k12_ids.py"
click node_common "https://github.com/haolpku/k12-kgraph/blob/main/src/sft_qa/common.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_kg_runner,node_prepare,node_segment,node_extract,node_merge,node_exercises,node_cycles,node_kgdata,node_user toneBlue
class node_bench,node_sft,node_exercise_qa,node_qa_generation,node_qa_merge,node_benchdata,node_traindata toneAmber
class node_evaluation,node_models toneMint
class node_config,node_env,node_io,node_llm,node_ids,node_common toneRose
class node_textbooks,node_mineru,node_model_user toneIndigo
```

## Components

### Graph construction

- [KG orchestrator](https://github.com/haolpku/k12-kgraph/blob/main/src/kg/run_pipeline.py): `src/kg/run_pipeline.py`
- [Prepare textbooks](https://github.com/haolpku/k12-kgraph/blob/main/src/kg/prepare_textbooks.py): `src/kg/prepare_textbooks.py`
- [Segment sections](https://github.com/haolpku/k12-kgraph/blob/main/src/kg/segment_textbooks.py): `src/kg/segment_textbooks.py`
- [Extract graph](https://github.com/haolpku/k12-kgraph/blob/main/src/kg/extract_kg_from_textbook.py): `src/kg/extract_kg_from_textbook.py`
- [Merge graphs](https://github.com/haolpku/k12-kgraph/blob/main/src/kg/merge_kg.py): `src/kg/merge_kg.py`
- [Enrich exercises](https://github.com/haolpku/k12-kgraph/blob/main/src/kg/build_afterclass_exercises.py): `src/kg/build_afterclass_exercises.py`
- [Check cycles](https://github.com/haolpku/k12-kgraph/blob/main/src/kg/check_cycles.py): `src/kg/check_cycles.py`
- [Curriculum graph](https://github.com/haolpku/k12-kgraph/blob/main/src/kg/merge_kg.py): `src/kg/merge_kg.py`

### Dataset generation

- [Benchmark generation](https://github.com/haolpku/k12-kgraph/tree/main/src/benchmark): `src/benchmark`
- [SFT QA generation](https://github.com/haolpku/k12-kgraph/tree/main/src/sft_qa): `src/sft_qa`
- [Exercise QA](https://github.com/haolpku/k12-kgraph/blob/main/src/sft_qa/exercise_to_qa.py): `src/sft_qa/exercise_to_qa.py`
- [Node-edge QA](https://github.com/haolpku/k12-kgraph/blob/main/src/sft_qa/generate_qa.py): `src/sft_qa/generate_qa.py`
- [Training data assembly](https://github.com/haolpku/k12-kgraph/blob/main/src/sft_qa/build_train_jsonl.py): `src/sft_qa/build_train_jsonl.py`
- [K12-Bench](https://github.com/haolpku/k12-kgraph/tree/main/src/benchmark): `src/benchmark`
- [K12-Train](https://github.com/haolpku/k12-kgraph/tree/main/src/sft_qa): `src/sft_qa`

### Model evaluation

- [Multiselect evaluation](https://github.com/haolpku/k12-kgraph/blob/main/eval/eval_multiselect.py): `eval/eval_multiselect.py`

### Shared runtime

- [Pipeline config](https://github.com/haolpku/k12-kgraph/blob/main/src/utils/config.py): `src/utils/config.py`
- [Environment loader](https://github.com/haolpku/k12-kgraph/blob/main/src/utils/envfile.py): `src/utils/envfile.py`
- [JSON IO](https://github.com/haolpku/k12-kgraph/blob/main/src/utils/io.py): `src/utils/io.py`
- [LLM client](https://github.com/haolpku/k12-kgraph/blob/main/src/utils/llm_client.py): `src/utils/llm_client.py`
- [Graph identifiers](https://github.com/haolpku/k12-kgraph/blob/main/src/utils/k12_ids.py): `src/utils/k12_ids.py`
- [SFT shared helpers](https://github.com/haolpku/k12-kgraph/blob/main/src/sft_qa/common.py): `src/sft_qa/common.py`

### Other components

- **Pipeline user**
- **Textbooks**
- **MinerU parser**
- **Model evaluator**
- **Model API**

## Connections

- Pipeline user → KG orchestrator: runs (evidence: `README.md`)
- Textbooks → MinerU parser: parsed by (evidence: `README.md`)
- MinerU parser → Prepare textbooks: provides input (evidence: `README.md`)
- KG orchestrator → Prepare textbooks: runs (evidence: `src/kg/run_pipeline.py`)
- KG orchestrator → Segment sections: runs (evidence: `src/kg/run_pipeline.py`)
- KG orchestrator → Extract graph: runs (evidence: `src/kg/run_pipeline.py`)
- KG orchestrator → Merge graphs: runs (evidence: `src/kg/run_pipeline.py`)
- KG orchestrator → Enrich exercises: runs (evidence: `src/kg/run_pipeline.py`)
- KG orchestrator → Check cycles: runs (evidence: `src/kg/run_pipeline.py`)
- Prepare textbooks → Segment sections: prepares sections (evidence: `README.md`)
- Segment sections → Extract graph: segments for (evidence: `README.md`)
- Extract graph → Merge graphs: extracts for (evidence: `README.md`)
- Merge graphs → Curriculum graph: produces (evidence: `README.md`)
- Enrich exercises → Curriculum graph: enriches (evidence: `src/kg/build_afterclass_exercises.py`)
- Extract graph → LLM client: requests generation (evidence: `src/kg/extract_kg_from_textbook.py`)
- Enrich exercises → LLM client: requests enrichment (evidence: `src/kg/build_afterclass_exercises.py`)
- KG orchestrator → Benchmark generation: derives benchmark (evidence: `README.md`)
- KG orchestrator → SFT QA generation: derives training data (evidence: `README.md`)
- Curriculum graph → Benchmark generation: queries graph (evidence: `README.md`)
- Curriculum graph → Node-edge QA: grounds QA (evidence: `src/sft_qa/generate_qa.py`)
- Curriculum graph → Exercise QA: supplies exercises
- SFT QA generation → Node-edge QA: generates shards (evidence: `src/sft_qa/generate_qa.py`)
- SFT QA generation → Exercise QA: generates exercise QA (evidence: `src/sft_qa/exercise_to_qa.py`)
- SFT QA generation → Training data assembly: assembles pairs
- Benchmark generation → K12-Bench: produces (evidence: `README.md`)
- Training data assembly → K12-Train: writes pairs
- Model evaluator → Multiselect evaluation: runs evaluation (evidence: `README.md`)
- K12-Bench → Multiselect evaluation: evaluates on (evidence: `README.md`)
- Multiselect evaluation → Model API: queries (evidence: `README.md`)
- Multiselect evaluation → Environment loader: loads environment (evidence: `eval/eval_multiselect.py`)
- Extract graph → Pipeline config: uses settings (evidence: `src/kg/extract_kg_from_textbook.py`)
- Extract graph → JSON IO: uses serialization (evidence: `src/kg/extract_kg_from_textbook.py`)
- Merge graphs → Graph identifiers: uses identifiers (evidence: `src/kg/merge_kg.py`)
- Node-edge QA → SFT shared helpers: uses helpers (evidence: `src/sft_qa/generate_qa.py`)
- SFT shared helpers → Pipeline config: loads config (evidence: `src/sft_qa/common.py`)
