Banner

CrewAI 上手教程

1天前

关联项目:CrewAI

前置要求:Python 3.10+(以官方文档为准);了解 Agent / Task 的基本概念;具备 OpenAI 或兼容模型的 API Key。

预计耗时:环境安装约 10 分钟;跑通第一个 Agent/Task/Crew 示例约 20 分钟。

官方仓库:crewAIInc/crewAI

本教程带你完成从环境准备到跑通核心工作流的完整过程。命令与示例针对主流环境给出,实际版本参数请以官方文档为准。

一、环境准备

python -m venv .venv
source .venv/bin/activate
# Windows 下激活脚本为 .venv/Scripts/activate
pip install crewai
export OPENAI_API_KEY="sk-..."

要点: - 默认使用 OpenAI 兼容模型;切换到其他模型供应商的配置方式以官方文档为准。 - 需要工具或记忆能力时,可能要安装额外依赖,以官方文档为准。

二、安装部署

验证安装:

python -c "import crewai; print(crewai.__version__)"

预期输出:版本号字符串(例如 0.x.x),具体版本以官方文档为准;若报 ModuleNotFoundError,请确认虚拟环境已激活。

三、快速上手:跑通第一个示例

定义一个研究员和一个撰稿人,由 Crew 顺序完成任务:

from crewai import Agent, Task, Crew, Process

researcher = Agent(
    role="Researcher",
    goal="Collect key facts about {topic}",
    backstory="You are a meticulous researcher who cites concrete points.",
    verbose=True,
    allow_delegation=False,
)
writer = Agent(
    role="Writer",
    goal="Write a short brief about {topic}",
    backstory="You turn research notes into clear prose.",
    verbose=True,
    allow_delegation=False,
)

research_task = Task(
    description="Research the topic {topic} and list 3 key facts.",
    agent=researcher,
    expected_output="A bullet list of 3 facts.",
)
write_task = Task(
    description="Write a 100-word brief about {topic} using the facts.",
    agent=writer,
    expected_output="A 100-word brief in English.",
)

crew = Crew(
    agents=[researcher, writer],
    tasks=[research_task, write_task],
    process=Process.sequential,
    verbose=True,
)

result = crew.kickoff(inputs={"topic": "multi-agent frameworks"})
print(result)

关键参数: - role / goal / backstory 决定 Agent 的行为边界;写具体一些可显著提升稳定性。 - expected_output 约束每个 Task 的产出格式。 - {topic} 是模板变量,由 kickoff(inputs=...) 注入。 - Process.sequential 表示按任务列表顺序执行;层级模式参数以官方文档为准。

四、核心工作流

任务:生成一份关于某主题的研究简报

  1. 设置密钥并确认依赖可用:
export OPENAI_API_KEY="sk-..."
python -c "import crewai; print(crewai.__version__)"
  1. 定义两个角色互补的 Agent(代码见「快速上手」中的 researcher 与 writer 定义)。预期结果:两个具有不同 role 与 goal 的 Agent 对象。

  2. 为每一步定义 Task,并写明 expected_output:

research_task = Task(
    description="Find 3 concrete facts about {topic}.",
    agent=researcher,
    expected_output="3 bullet points.",
)
write_task = Task(
    description="Summarize those facts into a short brief about {topic}.",
    agent=writer,
    expected_output="A short paragraph in English.",
)

预期结果:任务按依赖顺序排列,每个任务都绑定了执行者。

  1. 组装 Crew 并运行:
crew = Crew(agents=[researcher, writer], tasks=[research_task, write_task],
            process=Process.sequential, verbose=True)
result = crew.kickoff(inputs={"topic": "open-source agent frameworks"})
print(result)

预期结果:终端逐步打印两个 Agent 的执行日志,最后输出一段英文简报。

  1. 迭代优化:若输出偏离要求,优先修改 goal、backstory 与 expected_output,再考虑调整 process;更细的 Flow 写法以官方文档为准。

五、进阶实践

  • 层级流程:使用 Process.hierarchical 引入管理 Agent 协调任务分配,管理模型参数以官方文档为准。
  • 自定义工具:通过官方工具装饰器或继承 BaseTool 接入业务 API,写法以官方文档为准。
  • 开启记忆:在 Crew 上启用记忆与知识检索以跨任务保留上下文,参数以官方文档为准。
  • CrewAI Flows:用 @start、@listen 装饰器编排多步骤、带状态的长流程(以官方文档为准)。
  • CLI 脚手架:用官方 CLI 创建新项目与检查版本,命令与参数以官方文档为准。

六、常见问题

Q:必须先配置 OpenAI API Key 吗? A:默认示例使用 OpenAI 兼容模型,因此需要;改用其他供应商时按官方文档配置模型与密钥,不要硬编码到代码中。

Q:kickoff() 里的 {topic} 为什么没有被替换? A:检查 Task 的 description 是否使用了相同占位符,并确认通过 kickoff(inputs={"topic": ...}) 传入;参数细节以官方文档为准。

Q:Agent 之间如何分工与委派? A:由 role、goal、backstory 定义职责,allow_delegation 控制是否把任务转交其他成员;层级流程下还涉及管理角色(以官方文档为准)。

Q:输出格式不稳定怎么办? A:把 expected_output 写得尽可能具体,必要时把大任务拆成多个 Task;仍不稳定时考虑换用支持结构化输出的模型或显式校验。

Q:与 LangGraph 如何取舍? A:CrewAI 胜在角色模板与快速编排;LangGraph 胜在显式状态机、持久化与可控分支。业务流程原型选前者,生产级复杂工作流选后者。

七、学习资源

3