第 1 章 · 02 swarms 框架背景 本节摘要:AutoHedge 不是从零造轮子,它建在作者自家的 swarms 框架之上——一个用于多智能体编排的 Python 库(定位类似 CrewAI、AutoGen)。要读懂 AutoHedge 的 Agent 怎么协作,必须先理解 swarms 提供的几个核心抽象: (一个有角色、有工具、能调用 LLM 的单元)、 (跨 Agent 的消息历史)、以及关键的 handoffs(一个 Agent 把任务转给另一个的机制)。本节不深入 swarms 内部实现(那不在本仓库),而是讲清 AutoHedge 用到的那些抽象的概念与用法,为第 3 章读 workers.py 打底。读完本节,你理解了 AutoHedge 「编排能力」的来源。
本节摘要:AutoHedge 不是从零造轮子,它建在作者自家的 swarms 框架之上——一个用于多智能体编排的 Python 库(定位类似 CrewAI、AutoGen)。要读懂 AutoHedge 的 Agent 怎么协作,必须先理解 swarms 提供的几个核心抽象:
Agent(一个有角色、有工具、能调用 LLM 的单元)、Conversation(跨 Agent 的消息历史)、以及关键的 handoffs(一个 Agent 把任务转给另一个的机制)。本节不深入 swarms 内部实现(那不在本仓库),而是讲清 AutoHedge 用到的那些抽象的概念与用法,为第 3 章读 workers.py 打底。读完本节,你理解了 AutoHedge 「编排能力」的来源。
内容来源:基于
autohedge/workers.py中 swarms 的用法反推,结合多 Agent 框架通用概念。
阅读完本节,你应当能够:
swarms 是 The Swarm Corporation(也就是 AutoHedge 作者 Kye Gomez)开发的多智能体编排框架。它的目标:让你能方便地定义多个 Agent,并让它们协作完成复杂任务。
在多 Agent 框架的谱系里,swarms 与这些同类定位相近:
| 框架 | 风格 | 特点 |
|---|---|---|
| CrewAI | 角色 + 任务 | 给每个 Agent 角色,分配 Task,按流程跑 |
| AutoGen(微软) | 对话驱动 | 多 Agent 互相对话完成任务 |
| LangGraph | 图/状态机 | 把 Agent 编排画成图,显式控制流转 |
| swarms | handoffs 编排 | Director 通过 handoffs 动态调度专家 Agent |
AutoHedge 用的就是 swarms 的 「Director + 专家 + handoffs」 模式。
💡 核心心法:不同框架只是「多 Agent 协作」的不同表达方式,核心问题都一样——如何把一个复杂任务拆给多个 Agent,并控制它们之间的流转。理解了 swarms 的 handoffs,再看其他框架会很快上手。
swarms 的 Agent 是最核心的类。从 workers.py 看 AutoHedge 怎么用它:
from swarms import Agent director_agent = Agent( agent_name="Trading-Director", system_prompt=DIRECTOR_PROMPT + _SYSTEM_SUFFIX, model_name="gpt-4.1", max_loops=1, handoffs=ALL_AGENTS, )
关键构造参数:
| 参数 | 含义 | AutoHedge 的用法 |
|---|---|---|
agent_name |
Agent 名字,用于日志与交接 | 如 "Trading-Director" |
system_prompt |
系统提示词,定义角色/目标/输出 | 来自 prompts.py,加时间后缀 |
model_name |
用哪个 LLM | Director/专家用 gpt-4.1,情绪用 gpt-4o-mini |
tools |
Agent 可调用的工具函数列表 | 情绪 Agent 带 [exa_search] |
max_loops |
最大推理循环数 | 都设 1(单轮,不自我迭代) |
handoffs |
可交接的目标 Agent 列表 | Director 设 ALL_AGENTS |
output_type |
输出类型 | 部分专家设 "str" |
context_length |
上下文长度 | 专家设 16000 |
一个 Agent 实例 = 一个有角色(system_prompt)、有大脑(model_name)、有手(tools)、有协作对象(handoffs)的 LLM 单元。
💡 max_loops=1 的含义:AutoHedge 所有 Agent 都设单轮,意味着每个 Agent 收到任务后只推理一次就产出结果,不做「自我反思-再推理」的多轮迭代。这是简化,也降低了不确定性——适合教学,但生产中可能需要多轮。
swarms 的 Conversation 管理跨 Agent 的消息历史。从 main.py 看:
from swarms import Conversation self.conversation = Conversation(time_enabled=True) # ... self.conversation.add(role="user", content=f"Task: {task}") output = director_agent.run(task=task) self.conversation.add(role="director", content=output)
Conversation 支持三种序列化方式(对应 AutoHedge 主类的三种输出格式):
| 方法 | 输出 | 用途 |
|---|---|---|
return_messages_as_list() |
消息列表 | 程序化处理 |
return_messages_as_dictionary() |
字典 | 结构化存取 |
return_history_as_string() |
字符串 | 人类阅读 / 存档 |
time_enabled=True 表示每条消息带时间戳,便于审计「谁在什么时候说了什么」——这在交易场景里很重要(可追溯决策链)。
handoffs 是 swarms 编排的核心机制,也是 AutoHedge 架构的关键:
ALL_AGENTS = [sentiment_agent, risk_agent, execution_agent, quant_agent] director_agent = Agent( agent_name="Trading-Director", # ... handoffs=ALL_AGENTS, # Director 可以把任务交给这四个专家中的任何一个 )
语义:
handoffs=ALL_AGENTS 告诉框架:Director 在推理过程中,可以自主决定把当前任务「移交」给列表里的某个专家 Agent。与函数调用(Tool Use)的区别:
| 维度 | 工具调用(Tool Use) | handoffs |
|---|---|---|
| 转交对象 | 一个函数(无状态) | 另一个 Agent(有角色、有大脑) |
| 处理能力 | 取数据/执行单步动作 | 完整的多步推理 |
| 控制流 | 调完函数回到原 Agent | 转给专家,专家自主推理 |
💡 核心心法:工具调用是「让 Agent 调函数拿信息」,handoffs 是「让 Agent 把活儿交给另一个 Agent」。前者是手脚,后者是分权。AutoHedge 里 Director 用 handoffs 分权给专家,专家再用 tools 调函数——两层叠加。
直接用 OpenAI API 也能做 Agent,为什么用框架?
代价是:真正的调度细节藏在 swarms 内部,本仓库看不到 handoffs 具体怎么实现、Director 的「交接决策」prompt 长什么样。这是 AutoHedge 「看不透」的根源之一——你看到的是配置,不是机制。第 10 章会再谈这个局限。
理解了 swarms 的 Agent / Conversation / handoffs 三个抽象,你就掌握了读 workers.py 的全部钥匙。第 3 章会逐行拆解那 5 个 Agent 的实例化;第 4 章会拆 system_prompt 的写法;第 6 章会拆 tools 怎么被调用。
agent_name(名)+ system_prompt(角色)+ model_name(大脑)+ tools(手)+ max_loops(循环)+ handoffs(协作对象)。time_enabled 带时间戳便于审计。handoffs=ALL_AGENTS 即可自主分权给专家。下一节,我们把所有 Agent 串起来,画出 AutoHedge 的整体架构与两类执行链路。