How haolpku/k12-kgraph works
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: run_pipeline.py KG orchestrator · extract_kg_from_textbook.py Extract graph · merge_kg.py Curriculum graph · sft_qa SFT QA generation
Components 27
Graph construction
- KG orchestrator
src/kg/run_pipeline.py - Prepare textbooks
src/kg/prepare_textbooks.py - Segment sections
src/kg/segment_textbooks.py - Extract graph
src/kg/extract_kg_from_textbook.py - Merge graphs
src/kg/merge_kg.py - Enrich exercises
src/kg/build_afterclass_exercises.py - Check cycles
src/kg/check_cycles.py - Curriculum graph
src/kg/merge_kg.py
Dataset generation
- Benchmark generation
src/benchmark - SFT QA generation
src/sft_qa - Exercise QA
src/sft_qa/exercise_to_qa.py - Node-edge QA
src/sft_qa/generate_qa.py - Training data assembly
src/sft_qa/build_train_jsonl.py - K12-Bench
src/benchmark - K12-Train
src/sft_qa
Model evaluation
- Multiselect evaluation
eval/eval_multiselect.py
Shared runtime
- Pipeline config
src/utils/config.py - Environment loader
src/utils/envfile.py - JSON IO
src/utils/io.py - LLM client
src/utils/llm_client.py - Graph identifiers
src/utils/k12_ids.py - SFT shared helpers
src/sft_qa/common.py
Other components
- Pipeline user
- Textbooks
- MinerU parser
- Model evaluator
- Model API
Connections 35
- Pipeline user → KG orchestrator: runs
README.md - Textbooks → MinerU parser: parsed by
README.md - MinerU parser → Prepare textbooks: provides input
README.md - KG orchestrator → Prepare textbooks: runs
src/kg/run_pipeline.py - KG orchestrator → Segment sections: runs
src/kg/run_pipeline.py - KG orchestrator → Extract graph: runs
src/kg/run_pipeline.py - KG orchestrator → Merge graphs: runs
src/kg/run_pipeline.py - KG orchestrator → Enrich exercises: runs
src/kg/run_pipeline.py - KG orchestrator → Check cycles: runs
src/kg/run_pipeline.py - Prepare textbooks → Segment sections: prepares sections
README.md - Segment sections → Extract graph: segments for
README.md - Extract graph → Merge graphs: extracts for
README.md - Merge graphs → Curriculum graph: produces
README.md - Enrich exercises → Curriculum graph: enriches
src/kg/build_afterclass_exercises.py - Extract graph → LLM client: requests generation
src/kg/extract_kg_from_textbook.py - Enrich exercises → LLM client: requests enrichment
src/kg/build_afterclass_exercises.py - KG orchestrator → Benchmark generation: derives benchmark
README.md - KG orchestrator → SFT QA generation: derives training data
README.md - Curriculum graph → Benchmark generation: queries graph
README.md - Curriculum graph → Node-edge QA: grounds QA
src/sft_qa/generate_qa.py - Curriculum graph → Exercise QA: supplies exercises
- SFT QA generation → Node-edge QA: generates shards
src/sft_qa/generate_qa.py - SFT QA generation → Exercise QA: generates exercise QA
src/sft_qa/exercise_to_qa.py - SFT QA generation → Training data assembly: assembles pairs
- Benchmark generation → K12-Bench: produces
README.md - Training data assembly → K12-Train: writes pairs
- Model evaluator → Multiselect evaluation: runs evaluation
README.md - K12-Bench → Multiselect evaluation: evaluates on
README.md - Multiselect evaluation → Model API: queries
README.md - Multiselect evaluation → Environment loader: loads environment
eval/eval_multiselect.py - Extract graph → Pipeline config: uses settings
src/kg/extract_kg_from_textbook.py - Extract graph → JSON IO: uses serialization
src/kg/extract_kg_from_textbook.py - Merge graphs → Graph identifiers: uses identifiers
src/kg/merge_kg.py - Node-edge QA → SFT shared helpers: uses helpers
src/sft_qa/generate_qa.py - SFT shared helpers → Pipeline config: loads config
src/sft_qa/common.py