用研究智能体与写作智能体串行协作完成博客生成
研究 Agent 负责搜集和总结,写作 Agent 接收前者输出并生成博客,SequentialWorkflow 负责按顺序传递结果。
方案简介
这是一个基于 Swarms 的串行多智能体协作方案,用两个 Agent 将“研究”和“写作”拆成连续阶段。第一个 Agent 负责研究给定主题并输出详细摘要,第二个 Agent 接收研究摘要并写成完整博客文章;SequentialWorkflow 负责把前一阶段的输出传递给后一阶段。该方案适合需要先整理资料、再生成内容的任务,例如 README 示例中的“人工智能的历史与未来”主题。它展示了 Swarms 如何把多个自主 Agent 组织成可运行的工作流,而不是让单个 Agent 独立承担全部任务。
亮点与能力
- 由多个 Agent 共同完成一个任务。
- 研究和写作分成两个职责明确的阶段。
- 研究 Agent 输出详细主题摘要。
- 写作 Agent 根据研究摘要生成博客文章。
- 使用
SequentialWorkflow串行编排 Agent。 - 研究者输出自动作为写作者输入。
- 通过
workflow.run对完整流程提交一个任务。
组成与分工
- Swarms:提供 Agent 与 SequentialWorkflow,实现多智能体编排。
- Agent:分别承担研究和写作任务,并由模型驱动。
- Researcher:按照系统提示研究主题并提供详细摘要。
- Writer:接收研究摘要,撰写具有吸引力的博客文章。
- SequentialWorkflow:按顺序运行 Agent,并把研究输出馈送到写作输入。
- GPT-5.4:示例中为研究者和写作者指定的模型。
前置要求
需要准备 Python 环境、Swarms 包,以及模型调用所需的 API 配置。README 推荐使用 uv 安装:
$ uv pip install swarms
也可以使用 pip:
$ pip3 install -U swarms
如果从源码安装,可执行:
# Clone the repository
$ git clone https://github.com/kyegomez/swarms.git
$ cd swarms
$ pip install -r requirements.txt
README 的环境配置示例包括模型 API Key 和工作目录:
OPENAI_API_KEY=""
WORKSPACE_DIR="agent_workspace"
ANTHROPIC_API_KEY=""
GROQ_API_KEY=""
实施步骤
1. 安装框架
使用 uv、pip 或源码方式安装 Swarms。推荐命令如下:
$ uv pip install swarms
2. 配置模型访问环境
根据实际使用的模型配置 API Key。仓库提供了 OPENAI_API_KEY、ANTHROPIC_API_KEY 和 GROQ_API_KEY 等变量示例,并提供 WORKSPACE_DIR 作为工作目录配置。
3. 创建研究 Agent
导入 Agent,设置名称为 Researcher,并通过 system_prompt 明确其职责是研究主题、提供详细摘要;模型设置为 gpt-5.4。
4. 创建写作 Agent
创建第二个 Agent,命名为 Writer,将系统提示设为根据研究摘要撰写博客,同样使用 gpt-5.4。
5. 组装并运行串行流程
将两个 Agent 按研究者、写作者的顺序传入 SequentialWorkflow,调用 workflow.run 提交主题,并打印返回的最终文章。
使用与配置要点
完整使用方式如下:
from swarms import Agent, SequentialWorkflow
# Agent 1: The Researcher
researcher = Agent(
agent_name="Researcher",
system_prompt="Your job is to research the provided topic and provide a detailed summary.",
model_name="gpt-5.4",
)
# Agent 2: The Writer
writer = Agent(
agent_name="Writer",
system_prompt="Your job is to take the research summary and write a beautiful, engaging blog post about it.",
model_name="gpt-5.4",
)
# Create a sequential workflow where the researcher's output feeds into the writer's input
workflow = SequentialWorkflow(agents=[researcher, writer])
# Run the workflow on a task
final_post = workflow.run("The history and future of artificial intelligence")
print(final_post)
日常使用时,主要修改 workflow.run 的主题文本,以及两个 Agent 的 system_prompt。验证成功的标志是 final_post 返回并被打印为最终博客内容;流程顺序应保持研究者在前、写作者在后。
注意事项与常见问题
- 该示例是串行流程,写作阶段依赖研究阶段的输出。
- 两个 Agent 的职责通过
system_prompt明确区分,修改提示词时应保持前后阶段的输入输出关系。 - README 同时提供了
max_loops="auto"的自主 Agent 模式,并指出开放式、多步骤任务适合自动循环;本串行示例没有配置该参数。 - 对延迟敏感或成本敏感的生产流程,README 建议使用固定的
max_loops;对步骤数量取决于中间结果的工作流,才适合自动循环。
优缺点
- ✓ 研究与写作职责分离
- ✓ 输出按顺序自动传递
出处
本方案挖掘自开源项目 kyegomez/swarms,方案内容与实施命令均来自其 README 原文。
本方案由真实开源项目挖掘整理,实施命令均来自其 README 原文,安装使用请遵循项目开源协议。