agents.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """使用官方 HelloAgents SimpleAgent 构建协作团队。"""
  2. from __future__ import annotations
  3. from dataclasses import dataclass
  4. from typing import Protocol
  5. from hello_agents import Config, HelloAgentsLLM, SimpleAgent
  6. from hello_agents.tools import ToolRegistry
  7. from .config import LLMSettings
  8. from .prompts import (
  9. REPORT_SYNTHESIZER_PROMPT,
  10. REQUIREMENT_ANALYST_PROMPT,
  11. RISK_REVIEWER_PROMPT,
  12. SOLUTION_ARCHITECT_PROMPT,
  13. )
  14. class AgentLike(Protocol):
  15. """便于离线测试注入替身,同时生产环境始终使用 SimpleAgent。"""
  16. def run(self, input_text: str, **kwargs: object) -> str:
  17. """处理一个阶段的输入并返回文本。"""
  18. ...
  19. @dataclass(frozen=True)
  20. class AgentTeam:
  21. """顺序协作的四个角色。"""
  22. analyst: AgentLike
  23. architect: AgentLike
  24. reviewer: AgentLike
  25. synthesizer: AgentLike
  26. def build_agent_team(settings: LLMSettings, tool_registry: ToolRegistry) -> AgentTeam:
  27. """用同一个 HelloAgentsLLM 实例创建四个官方 SimpleAgent。"""
  28. settings.validate()
  29. llm = HelloAgentsLLM(
  30. model=settings.model,
  31. api_key=settings.api_key,
  32. base_url=settings.base_url,
  33. temperature=settings.temperature,
  34. timeout=settings.timeout,
  35. )
  36. config = Config(debug=False, max_history_length=20)
  37. return AgentTeam(
  38. analyst=SimpleAgent(
  39. name="需求分析师",
  40. llm=llm,
  41. system_prompt=REQUIREMENT_ANALYST_PROMPT,
  42. config=config,
  43. tool_registry=tool_registry,
  44. ),
  45. architect=SimpleAgent(
  46. name="方案架构师",
  47. llm=llm,
  48. system_prompt=SOLUTION_ARCHITECT_PROMPT,
  49. config=config,
  50. ),
  51. reviewer=SimpleAgent(
  52. name="风险审查员",
  53. llm=llm,
  54. system_prompt=RISK_REVIEWER_PROMPT,
  55. config=config,
  56. ),
  57. synthesizer=SimpleAgent(
  58. name="报告整合员",
  59. llm=llm,
  60. system_prompt=REPORT_SYNTHESIZER_PROMPT,
  61. config=config,
  62. tool_registry=tool_registry,
  63. ),
  64. )