build_graph.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. """CLI:根据 JSON 配置(15 类文件数组)构建知识图谱。
  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. ap = argparse.ArgumentParser()
  12. ap.add_argument("config", nargs="?", default="data/config_example.json")
  13. ap.add_argument("--clear", action="store_true", help="先清空图再构建")
  14. args = ap.parse_args()
  15. config_path = Path(args.config)
  16. if not config_path.exists():
  17. print(f"配置文件不存在: {config_path}")
  18. sys.exit(1)
  19. config = json.loads(config_path.read_text(encoding="utf-8"))
  20. from knowledge_agent.graph import build_knowledge_graph
  21. result = build_knowledge_graph(config, clear_first=args.clear)
  22. print("ok:", result["ok"])
  23. print("读取记录数:", result.get("records"))
  24. print("构建记录数:", result.get("built"))
  25. print("图谱统计:", result.get("graph"))
  26. if result.get("issues"):
  27. print("\n校验问题:")
  28. for i in result["issues"]:
  29. print(" -", i)
  30. if result.get("warnings"):
  31. print("\n构建警告:")
  32. for w in result["warnings"]:
  33. print(" -", w)
  34. if not result["ok"]:
  35. sys.exit(1)
  36. if __name__ == "__main__":
  37. main()