run.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env python3
  2. """单需求视频点位拓展判断 — 由任务层调用。"""
  3. from __future__ import annotations
  4. from dataclasses import dataclass
  5. from supply_agent.types import AgentResult, Role
  6. _POINT_TYPE_LABEL = {
  7. "inspiration": "灵感点",
  8. "purpose": "目的点",
  9. "key": "关键点",
  10. }
  11. @dataclass
  12. class VideoPoint:
  13. video_id: str
  14. point_type: str
  15. point_data: str | None
  16. point_desc: str | None
  17. @dataclass
  18. class DemandExpandContext:
  19. biz_dt: str
  20. run_id: str
  21. demand_grade_id: int
  22. demand_name: str
  23. grade: str
  24. score: float | None
  25. video_ids: list[str]
  26. points: list[VideoPoint]
  27. def build_expand_user_input(ctx: DemandExpandContext) -> str:
  28. """构建传给 Agent 的用户消息。"""
  29. score_text = f"{ctx.score:.2f}" if ctx.score is not None else "—"
  30. lines = [
  31. f"biz_dt={ctx.biz_dt}",
  32. f"run_id={ctx.run_id}",
  33. f"demand_grade_id={ctx.demand_grade_id}",
  34. f"demand_name={ctx.demand_name}",
  35. f"grade={ctx.grade}",
  36. f"score={score_text}",
  37. f"video_count={len(ctx.video_ids)}",
  38. "",
  39. "以下是从关联视频中提取的点位,请判断哪些可作为拓展需求:",
  40. ]
  41. for index, point in enumerate(ctx.points, start=1):
  42. type_label = _POINT_TYPE_LABEL.get(point.point_type, point.point_type)
  43. data_text = point.point_data or "—"
  44. desc_text = point.point_desc or "—"
  45. lines.append(
  46. f"[{index}|{type_label}|video={point.video_id}] "
  47. f"{data_text} | 描述: {desc_text}"
  48. )
  49. lines.extend(
  50. [
  51. "",
  52. "判断完成后调用 batch_save_demand_expansions 落库;",
  53. "将上述 biz_dt、demand_grade_id、demand_name、grade、run_id 原样传入工具。",
  54. "若无合适拓展,传 items=[]。",
  55. ]
  56. )
  57. return "\n".join(lines)
  58. def judge_demand_expansion(ctx: DemandExpandContext) -> AgentResult:
  59. """对单个需求执行拓展判断。"""
  60. from agents.demand_video_expand_agent.agent import create_demand_video_expand_agent
  61. agent = create_demand_video_expand_agent()
  62. user_input = build_expand_user_input(ctx)
  63. return agent.run(user_input)
  64. def extract_saved_count(result: AgentResult) -> int:
  65. """从 Agent 工具返回消息中解析保存条数。"""
  66. for msg in reversed(result.messages):
  67. if msg.role != Role.TOOL or not msg.content:
  68. continue
  69. text = str(msg.content)
  70. if "成功保存" not in text:
  71. continue
  72. try:
  73. part = text.split("成功保存", 1)[1].strip()
  74. return int(part.split("条", 1)[0].strip())
  75. except (IndexError, ValueError):
  76. continue
  77. return 0