1
0

hello_agents_runner.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """HelloAgents 框架 Agent 封装:把框架原生 Agent 接入 PaperMind 执行链路
  2. 提供两类框架 Agent 封装:
  3. - build_reviewer_agent: 基于框架 SimpleAgent 的质量审核 Agent(Reflexion 自校验使用)
  4. - run_react_task: 基于框架 ReActAgent 的单步任务执行(桥接项目工具,框架原生工具调度)
  5. """
  6. from __future__ import annotations
  7. import logging
  8. from typing import Any, Optional
  9. from hello_agents import HelloAgentsLLM, ReActAgent, SimpleAgent
  10. from backend.tools.base import ToolRegistry as PaperMindToolRegistry
  11. from backend.tools.hello_agents_bridge import build_hello_agents_registry
  12. logger = logging.getLogger(__name__)
  13. def build_reviewer_agent(
  14. llm: HelloAgentsLLM,
  15. name: str = "QualityReviewer",
  16. system_prompt: str = "你是质量审核员,负责对照上下文检查 Agent 输出质量。只输出 JSON。",
  17. ) -> SimpleAgent:
  18. """构建框架 SimpleAgent 形态的质量审核 Agent(无工具,纯推理)"""
  19. return SimpleAgent(name=name, llm=llm, system_prompt=system_prompt)
  20. async def run_reviewer_check(agent: SimpleAgent, prompt: str) -> str:
  21. """通过框架 SimpleAgent 异步执行审核,返回文本结果"""
  22. return await agent.arun(prompt)
  23. def build_react_agent(
  24. name: str,
  25. llm: HelloAgentsLLM,
  26. registry: PaperMindToolRegistry,
  27. system_prompt: str,
  28. tool_names: Optional[list[str]] = None,
  29. max_steps: int = 8,
  30. ) -> ReActAgent:
  31. """构建框架 ReActAgent:项目工具经桥接进入框架工具注册表"""
  32. ha_registry = build_hello_agents_registry(registry, tool_names)
  33. return ReActAgent(
  34. name=name,
  35. llm=llm,
  36. tool_registry=ha_registry,
  37. system_prompt=system_prompt,
  38. max_steps=max_steps,
  39. )
  40. async def run_react_task(
  41. name: str,
  42. llm: HelloAgentsLLM,
  43. registry: PaperMindToolRegistry,
  44. system_prompt: str,
  45. task: str,
  46. tool_names: Optional[list[str]] = None,
  47. max_steps: int = 8,
  48. ) -> dict[str, Any]:
  49. """用框架 ReActAgent 执行单步任务,返回项目统一的结果结构
  50. Returns:
  51. {"agent": name, "result": str, "tool_calls": []}
  52. (框架 ReActAgent 内部完成工具调度,工具明细由框架 TraceLogger 记录)
  53. """
  54. agent = build_react_agent(name, llm, registry, system_prompt, tool_names, max_steps)
  55. result = await agent.arun(task)
  56. return {"agent": name, "result": result, "tool_calls": []}