test_research_graph.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from __future__ import annotations
  2. """测试地图研究阶段子图:验证高德地图POI搜索与天气查询的完整流程。"""
  3. import asyncio
  4. from rich.console import Console
  5. from rich.table import Table
  6. from app.graph.research_builder import (
  7. build_research_graph,
  8. )
  9. console = Console()
  10. async def main() -> None:
  11. graph = await build_research_graph()
  12. result = await graph.ainvoke(
  13. {
  14. "user_query": (
  15. "我和妻子计划2026年8月10日"
  16. "从上海去成都,8月13日返回,"
  17. "总预算8000元。"
  18. "喜欢熊猫、自然景观和川菜,"
  19. "不想安排太赶。"
  20. "酒店希望靠近地铁,"
  21. "每晚不超过600元。"
  22. "机票不限制是否中转。"
  23. ),
  24. "errors": [],
  25. "missing_fields": [],
  26. }
  27. )
  28. console.rule("[bold]工作流结果")
  29. console.print(result["final_answer"])
  30. map_result = result.get(
  31. "map_research_result"
  32. )
  33. if map_result is None:
  34. console.print(
  35. {
  36. "错误": result.get("errors"),
  37. }
  38. )
  39. return
  40. console.print(
  41. {
  42. "城市": map_result.city,
  43. "景点数量": len(
  44. map_result.attractions
  45. ),
  46. "餐饮数量": len(
  47. map_result.restaurants
  48. ),
  49. "天气数量": len(
  50. map_result.weather
  51. ),
  52. "备注": map_result.notes,
  53. }
  54. )
  55. attraction_table = Table(
  56. title="真实景点候选"
  57. )
  58. attraction_table.add_column("名称")
  59. attraction_table.add_column("地址")
  60. attraction_table.add_column("坐标")
  61. attraction_table.add_column("匹配原因")
  62. for place in map_result.attractions:
  63. attraction_table.add_row(
  64. place.name,
  65. place.address or "未知",
  66. place.location or "未知",
  67. place.match_reason or "未说明",
  68. )
  69. console.print(attraction_table)
  70. restaurant_table = Table(
  71. title="真实餐饮候选"
  72. )
  73. restaurant_table.add_column("名称")
  74. restaurant_table.add_column("地址")
  75. restaurant_table.add_column("坐标")
  76. for place in map_result.restaurants:
  77. restaurant_table.add_row(
  78. place.name,
  79. place.address or "未知",
  80. place.location or "未知",
  81. )
  82. console.print(restaurant_table)
  83. if __name__ == "__main__":
  84. asyncio.run(main())