logo

实战四:多 Agent 调研团队

本实验将演示如何使用 CrewAI 组建一个包含“研究员”和“撰稿人”的虚拟团队,自动完成一份行业报告。

[LIVE_CHAT_BANNER]


1. 为什么用 CrewAI?

CrewAI 是目前最流行的 Multi-Agent 编排框架之一。它采用了基于角色 (Role-based) 的设计,非常符合人类团队的直觉。

pip install crewai crewai-tools

2. 定义 Agents

我们需要两个角色:

  1. Researcher: 负责搜集信息,要求事实准确。
  2. Writer: 负责基于信息写文章,要求文笔流畅。
from crewai import Agent, Task, Crew, Process
from crewai_tools import SerperDevTool

search_tool = SerperDevTool() # Google Search 工具

# 定义研究员
researcher = Agent(
  role='Senior Research Analyst',
  goal='Uncover cutting-edge developments in AI Agents',
  backstory="""You work at a leading tech think tank.
  Your expertise lies in identifying emerging trends.
  You have a knack for dissecting complex data and presenting actionable insights.""",
  verbose=True,
  allow_delegation=False,
  tools=[search_tool] # 只有研究员能用搜索工具
)

# 定义撰稿人
writer = Agent(
  role='Tech Content Strategist',
  goal='Craft compelling content on tech advancements',
  backstory="""You are a renowned Content Strategist, known for your insightful and engaging articles.
  You transform complex concepts into compelling narratives.""",
  verbose=True,
  allow_delegation=True
)

3. 定义 Tasks

任务必须具体,并指派给特定的 Agent。

# 任务 1:调研
task1 = Task(
  description="""Conduct a comprehensive analysis of the latest advancements in AI Agents in 2024.
  Identify key trends, breakthrough technologies, and potential industry impacts.""",
  expected_output="Full analysis report in bullet points",
  agent=researcher
)

# 任务 2:写作
task2 = Task(
  description="""Using the insights provided, develop an engaging blog post that highlights the most significant AI Agent trends.
  Your post should be informative yet accessible to a general tech-savvy audience.
  Avoid complex jargon.""",
  expected_output="Full blog post of at least 4 paragraphs",
  agent=writer
)

4. 组建 Crew 并起飞

crew = Crew(
  agents=[researcher, writer],
  tasks=[task1, task2],
  verbose=2, # 打印详细日志
  process=Process.sequential # 顺序执行:先调研,后写作
)

result = crew.kickoff()

print("######################")
print(result)

5. 观察运行过程

当你运行这段代码时,你会看到:

  1. Researcher 开始调用 Google Search,搜索 "AI Agent trends 2024"。
  2. 它会阅读搜索结果,提取关键点(如 AutoGen, BabyAGI)。
  3. 任务完成后,它把调研报告传递给下一棒。
  4. Writer 接收到报告,开始撰写文章。
  5. 最终输出一篇完整的博客。

小结

Multi-Agent 的魔力在于分工

  • 如果你让一个 Agent 既搜索又写文章,它容易分心或产生幻觉。
  • 拆分后,Researcher 专注于准确性,Writer 专注于可读性,效果往往更好。
AI Agent 开发实战手册
AI Engineer

AI Agent 开发实战手册

从 0 到 1 掌握 AI Agent 开发:涵盖自主计划、工具调用、MCP 协议与多智能体编排实战。

AI Agent 开发实战手册实战四:多 Agent 调研团队

实战四:多 Agent 调研团队

本实验将演示如何使用 CrewAI 组建一个包含“研究员”和“撰稿人”的虚拟团队,自动完成一份行业报告。

咨询顾问
Live Chat · 在线
想了解课程与方案?直接和顾问聊聊
真人顾问在线,获取学习路径与课程建议。

#1. 为什么用 CrewAI?

CrewAI 是目前最流行的 Multi-Agent 编排框架之一。它采用了基于角色 (Role-based) 的设计,非常符合人类团队的直觉。

bash
pip install crewai crewai-tools

#2. 定义 Agents

我们需要两个角色:

  1. Researcher: 负责搜集信息,要求事实准确。
  2. Writer: 负责基于信息写文章,要求文笔流畅。
python
from crewai import Agent, Task, Crew, Process from crewai_tools import SerperDevTool search_tool = SerperDevTool() # Google Search 工具 # 定义研究员 researcher = Agent( role='Senior Research Analyst', goal='Uncover cutting-edge developments in AI Agents', backstory="""You work at a leading tech think tank. Your expertise lies in identifying emerging trends. You have a knack for dissecting complex data and presenting actionable insights.""", verbose=True, allow_delegation=False, tools=[search_tool] # 只有研究员能用搜索工具 ) # 定义撰稿人 writer = Agent( role='Tech Content Strategist', goal='Craft compelling content on tech advancements', backstory="""You are a renowned Content Strategist, known for your insightful and engaging articles. You transform complex concepts into compelling narratives.""", verbose=True, allow_delegation=True )

#3. 定义 Tasks

任务必须具体,并指派给特定的 Agent。

python
# 任务 1:调研 task1 = Task( description="""Conduct a comprehensive analysis of the latest advancements in AI Agents in 2024. Identify key trends, breakthrough technologies, and potential industry impacts.""", expected_output="Full analysis report in bullet points", agent=researcher ) # 任务 2:写作 task2 = Task( description="""Using the insights provided, develop an engaging blog post that highlights the most significant AI Agent trends. Your post should be informative yet accessible to a general tech-savvy audience. Avoid complex jargon.""", expected_output="Full blog post of at least 4 paragraphs", agent=writer )

#4. 组建 Crew 并起飞

python
crew = Crew( agents=[researcher, writer], tasks=[task1, task2], verbose=2, # 打印详细日志 process=Process.sequential # 顺序执行:先调研,后写作 ) result = crew.kickoff() print("######################") print(result)

#5. 观察运行过程

当你运行这段代码时,你会看到:

  1. Researcher 开始调用 Google Search,搜索 "AI Agent trends 2024"。
  2. 它会阅读搜索结果,提取关键点(如 AutoGen, BabyAGI)。
  3. 任务完成后,它把调研报告传递给下一棒。
  4. Writer 接收到报告,开始撰写文章。
  5. 最终输出一篇完整的博客。

#小结

Multi-Agent 的魔力在于分工

  • 如果你让一个 Agent 既搜索又写文章,它容易分心或产生幻觉。
  • 拆分后,Researcher 专注于准确性,Writer 专注于可读性,效果往往更好。

常见问题

开发 AI Agent 需要掌握哪些编程语言?
首选 Python 或 TypeScript。Python 是 AI 生态的基石,而 TypeScript 在开发 MCP Server 和网页端交互时效率极高。借助 Cursor 等 AI 原生编辑器,编程门槛已大幅降低。
MCP 协议目前支持哪些模型?
MCP 是开放协议,目前对 Claude 3.5 系列支持最完美。通过 MCP Proxy,GPT-4o 和 Gemini 也可以间接访问 MCP Server 数据源。
AI Agent 会导致程序员失业吗?
不会,但会改变程序员的工作内容。未来的开发者将从“写代码”转向“管理 Agent 团队”,重点在于系统架构设计、复杂逻辑校验和 Agent 的提示词优化。