build_graph.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. """CLI:默认构建生产图谱;显式 JSON 配置保留历史模板构建。
  2. 用法: uv run python scripts/build_graph.py [config.json] [--clear]
  3. """
  4. from __future__ import annotations
  5. import argparse
  6. import json
  7. import sys
  8. from pathlib import Path
  9. def main() -> None:
  10. sys.stdout.reconfigure(encoding="utf-8")
  11. if len(sys.argv) == 1 or sys.argv[1].startswith("--"):
  12. from build_production_graph import main as production_main
  13. raise SystemExit(production_main())
  14. ap = argparse.ArgumentParser()
  15. ap.add_argument("config", nargs="?", default="data/config_example.json")
  16. ap.add_argument("--clear", action="store_true", help="先清空图再构建")
  17. args = ap.parse_args()
  18. config_path = Path(args.config)
  19. if not config_path.exists():
  20. print(f"配置文件不存在: {config_path}")
  21. sys.exit(1)
  22. config = json.loads(config_path.read_text(encoding="utf-8"))
  23. from step2_graph_building.graph import build_knowledge_graph
  24. result = build_knowledge_graph(config, clear_first=args.clear)
  25. print("ok:", result["ok"])
  26. print("读取记录数:", result.get("records"))
  27. print("构建记录数:", result.get("built"))
  28. print("图谱统计:", result.get("graph"))
  29. if result.get("issues"):
  30. print("\n校验问题:")
  31. for i in result["issues"]:
  32. print(" -", i)
  33. if result.get("warnings"):
  34. print("\n构建警告:")
  35. for w in result["warnings"]:
  36. print(" -", w)
  37. if not result["ok"]:
  38. sys.exit(1)
  39. if __name__ == "__main__":
  40. main()