handle_library.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """
  2. 关键点检索工具 - 根据输入的点在图数据库中查找所有关联的点
  3. 用于 Agent 执行时自主调取关联关键点数据。
  4. """
  5. import json
  6. import os
  7. from pathlib import Path
  8. from typing import Any, Dict, Optional
  9. # 完整图数据库文件路径(包含 edges)
  10. GRAPH_FULL_DATA_PATH = os.getenv(
  11. "GRAPH_FULL_DATA_PATH",
  12. # str(Path(__file__).parent.parent / "data/library/item_graph/item_graph_full_all_levels.json")
  13. str(Path(__file__).parent.parent / "data/library/item_graph/item_graph_full_max.json")
  14. )
  15. # 缓存图数据,避免重复加载
  16. _graph_full_cache: Optional[Dict[str, Any]] = None
  17. def _load_graph_full() -> Dict[str, Any]:
  18. """加载完整图数据(带缓存,包含 edges)"""
  19. global _graph_full_cache
  20. if _graph_full_cache is None:
  21. with open(GRAPH_FULL_DATA_PATH, 'r', encoding='utf-8') as f:
  22. _graph_full_cache = json.load(f)
  23. return _graph_full_cache
  24. if __name__ == "__main__":
  25. graph = _load_graph_full()
  26. data = {
  27. "灵感点" :{
  28. "实质" : {},
  29. "形式" : {},
  30. "意图" : {}
  31. },
  32. "目的点" :{
  33. "实质" : {},
  34. "形式" : {},
  35. "意图" : {}
  36. },
  37. "关键点" :{
  38. "实质" : {},
  39. "形式" : {},
  40. "意图" : {}
  41. }
  42. }
  43. for class_path, value in graph.items():
  44. if class_path == '_post_ids_index':
  45. continue
  46. meta = value.get("meta", {})
  47. point_type = meta.get("point_type")
  48. dimension = meta.get("dimension")
  49. elements = meta.get("elements", {})
  50. if not point_type or not dimension:
  51. continue
  52. for element in elements.keys():
  53. data[point_type][dimension][element] = class_path
  54. # 输出到文件
  55. output_path = Path(__file__).parent.parent / "data/library/item_graph/item_graph_full_max_map.json"
  56. with open(output_path, 'w', encoding='utf-8') as f:
  57. json.dump(data, f, ensure_ascii=False, indent=2)
  58. print(f"数据已输出到: {output_path}")