test_planner_graph.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. from __future__ import annotations
  2. """测试行程规划阶段子图:验证ItineraryPlannerAgent生成多日行程的流程。"""
  3. import asyncio
  4. from rich.console import Console
  5. from rich.table import Table
  6. from app.graph.planner_builder import (
  7. build_planner_graph,
  8. )
  9. console = Console()
  10. async def main() -> None:
  11. graph = await build_planner_graph()
  12. result = await graph.ainvoke(
  13. {
  14. "user_query": (
  15. "我和妻子计划2026年8月10日"
  16. "从上海去成都,8月14日返回,"
  17. "总预算5000元。"
  18. "喜欢熊猫、自然景观和川菜,"
  19. "希望行程轻松一些。"
  20. "酒店每晚不超过300元,"
  21. "评分不低于3,"
  22. "并且希望靠近地铁。"
  23. "机票价格和便利性综合考虑。"
  24. ),
  25. "errors": [],
  26. "missing_fields": [],
  27. "planning_attempts": 0,
  28. "revision_feedback": [],
  29. }
  30. )
  31. console.rule("[bold]Planner工作流结果")
  32. console.print(result["final_answer"])
  33. plan = result.get("itinerary_plan")
  34. if plan is None:
  35. console.print(
  36. {
  37. "错误": result.get("errors"),
  38. }
  39. )
  40. return
  41. console.rule("[bold]方案摘要")
  42. console.print(
  43. {
  44. "标题": plan.title,
  45. "概述": plan.overview,
  46. "规划次数": result.get(
  47. "planning_attempts"
  48. ),
  49. "航班组合": (
  50. plan.selected_flight
  51. .combination_id
  52. ),
  53. "航班报价": (
  54. plan.selected_flight
  55. .quoted_price
  56. ),
  57. "酒店": plan.selected_hotel.name,
  58. "酒店ID": (
  59. plan.selected_hotel.hotel_id
  60. ),
  61. "住宿晚数": (
  62. plan.selected_hotel.nights
  63. ),
  64. }
  65. )
  66. for day in plan.days:
  67. table = Table(
  68. title=(
  69. f"第{day.day_index}天 "
  70. f"{day.date}|{day.theme}"
  71. )
  72. )
  73. table.add_column("时间")
  74. table.add_column("类型")
  75. table.add_column("活动")
  76. table.add_column("地址")
  77. table.add_column("交通")
  78. table.add_column("备注")
  79. for activity in day.activities:
  80. table.add_row(
  81. (
  82. f"{activity.start_time}"
  83. f"—{activity.end_time}"
  84. ),
  85. activity.activity_type,
  86. activity.name,
  87. activity.address or "",
  88. activity.transport_mode or "",
  89. activity.notes or "",
  90. )
  91. console.print(table)
  92. if plan.assumptions:
  93. console.rule("[bold yellow]假设")
  94. for item in plan.assumptions:
  95. console.print(f"- {item}")
  96. if plan.warnings:
  97. console.rule("[bold yellow]警告")
  98. for item in plan.warnings:
  99. console.print(f"- {item}")
  100. if __name__ == "__main__":
  101. asyncio.run(main())