CrewAI 上手攻略:多 Agent 自动化处理复杂任务,让 AI 像员工一样分工协作

简介: CrewAI 是一个基于 Python 的自主 AI 智能体编排框架,可构建“虚拟团队”协同完成复杂任务。通过定义角色明确的 Agents、任务流 Tasks、协作流程 Processes 及可用工具 Tools,实现研究、写作、开发等多环节自动化。适用于长链条工作流,如研报生成、竞品分析、软件开发等,支持异步执行、人工介入与结构化输出,集成主流大模型与工具生态,是处理复杂知识型任务的高效选择。(238 字)

CrewAI是一个可以专门用来编排自主 AI 智能体(Autonomous AI Agents) 的Python 框架,你可以把它理解为在代码层面组建一个“虚拟团队”,给每个 Agent 分配特定的角色、目标,让它们协同处理那些单个 LLM 搞不定的复杂任务。

CrewAI 介绍

CrewAI 包含以下组件:

Agents 是具体的执行实体,有角色设定和能力边界;Tasks 是具体的任务指令;Crews 是把“人”和事儿撮合到一起的团队容器;Tools 则是 Agent 手里的工具(比如搜索、读文件、调 API等等);Processes 决定了活儿怎么干,比如说是大家排队干(顺序)还是层级汇报(层级)。

CrewA最适合的是那种链条长、环节多的工作流。比如你要搞个深度研报,需要先全网搜集信息,然后整理分析,写初稿,最后润色发布,这种“研究-写作-编辑”的流水线就非常契合。同理商业竞品分析、代码开发流程(设计-编码-测试)或者分工明确的客户支持系统,都是它的强项。

但有几种情况别用:

如果你的任务简单到一次 LLM 调用就能解决,用 CrewAI 就没有必要了而且还会增加复杂度和成本。对实时性要求极高的场景(比如毫秒级响应)也不合适,因为多 Agent 交互本来就慢。还有那种每一步都得让人盯着确认的流程,这种流程自动化程度太低也没必要上 Agent 编排。

安装与配置

环境准备很简单,基础包装上就行,如果需要额外的工具集,就把 tools 加上。

 # Install CrewAI  
 pip install crewai crewai-tools

 # For additional tools  
 pip install 'crewai[tools]'

基础示例:搭建内容创作团队

下面这段代码展示了如何把 Research Analyst(研究员)、Content Writer(撰稿人)和 Editor(编辑)这三个角色串起来。代码逻辑很简单:定义 Agent,定义 Task,最后塞进 Crew 里跑起来。

*注意观察

context

参数,它实现了任务间的数据流转。*

 from crewai import Agent, Task, Crew, Process  
from crewai_tools import SerperDevTool, WebsiteSearchTool

# Initialize tools  
search_tool = SerperDevTool()  
web_tool = WebsiteSearchTool()

# Create Agents  
researcher = Agent(  
    role='Research Analyst',  
    goal='Gather comprehensive information on {topic}',  
    backstory='You are an expert researcher with a keen eye for detail and accuracy.',  
    tools=[search_tool, web_tool],  
    verbose=True,  
    allow_delegation=False  
)

writer = Agent(  
    role='Content Writer',  
    goal='Create engaging, well-structured content about {topic}',  
    backstory='You are a skilled writer who transforms research into compelling narratives.',  
    verbose=True,  
    allow_delegation=False  
)

editor = Agent(  
    role='Editor',  
    goal='Refine and polish content to ensure quality and clarity',  
    backstory='You are a meticulous editor with an eye for grammar, style, and flow.',  
    verbose=True,  
    allow_delegation=False  
)

# Define Tasks  
research_task = Task(  
    description='Research {topic} and gather key facts, statistics, and insights.',  
    expected_output='A comprehensive research report with sources',  
    agent=researcher  
)

writing_task = Task(  
    description='Using the research, write a 500-word blog post about {topic}',  
    expected_output='A well-written blog post in markdown format',  
    agent=writer,  
    context=[research_task]  # Depends on research task  
)

editing_task = Task(  
    description='Edit the blog post for grammar, clarity, and engagement',  
    expected_output='A polished, publication-ready blog post',  
    agent=editor,  
    context=[writing_task]  
)

# Create Crew  
crew = Crew(  
    agents=[researcher, writer, editor],  
    tasks=[research_task, writing_task, editing_task],  
    process=Process.sequential,  # Tasks run in order  
    verbose=True  
)

# Execute  
result = crew.kickoff(inputs={'topic': 'Artificial Intelligence in Healthcare'})  
 print(result)

进阶示例:软件开发

对于更复杂的场景,比如软件开发,可能需要引入层级流程(Hierarchical Process)。这时候会有一个隐藏的 Manager Agent(通常用更强的模型如 GPT-5)来统筹分配任务,而不是简单的线性执行。

 from crewai import Agent, Task, Crew  
from crewai_tools import FileReadTool, CodeInterpreterTool

# Tools  
file_tool = FileReadTool()  
code_tool = CodeInterpreterTool()

# Agents with specific expertise  
architect = Agent(  
    role='Software Architect',  
    goal='Design scalable software architecture for {project}',  
    backstory='Senior architect with 15 years of experience in system design.',  
    verbose=True  
)

developer = Agent(  
    role='Python Developer',  
    goal='Write clean, efficient Python code',  
    backstory='Expert Python developer focused on best practices.',  
    tools=[code_tool],  
    verbose=True  
)

qa_engineer = Agent(  
    role='QA Engineer',  
    goal='Ensure code quality through comprehensive testing',  
    backstory='Detail-oriented QA engineer specializing in test automation.',  
    tools=[code_tool],  
    verbose=True  
)

# Tasks  
design_task = Task(  
    description='Design architecture for a {project} including component breakdown',  
    expected_output='Detailed architecture document with diagrams',  
    agent=architect  
)

development_task = Task(  
    description='Implement the core functionality based on the architecture',  
    expected_output='Working Python code with documentation',  
    agent=developer,  
    context=[design_task]  
)

testing_task = Task(  
    description='Write and execute unit tests for the developed code',  
    expected_output='Test suite with coverage report',  
    agent=qa_engineer,  
    context=[development_task]  
)

# Hierarchical process with manager agent  
dev_crew = Crew(  
    agents=[architect, developer, qa_engineer],  
    tasks=[design_task, development_task, testing_task],  
    process=Process.hierarchical,  # Manager coordinates tasks  
    manager_llm='gpt-4',  # Manager uses GPT-4  
    verbose=True  
)

 result = dev_crew.kickoff(inputs={'project': 'RESTful API for task management'})

进阶功能:异步、人工介入与结构化输出

如果你追求性能,异步执行(Asynchronous Execution) 是一个可选项,特别是 IO 密集型任务。

 # Run crew asynchronously for better performance  
 result = await crew.kickoff_async(inputs={'topic': 'AI trends'})

 # Run specific tasks in parallel  
 from crewai import Task  
 task1 = Task(description='Research topic A', agent=researcher, async_execution=True)  
 task2 = Task(description='Research topic B', agent=researcher, async_execution=True)

有些关键节点不能完全信赖 AI,这时候开启 Human-in-the-Loop,Agent 执行到一半会停下来问你要反馈。

 agent = Agent(  
     role='Decision Maker',  
     goal='Make strategic decisions',  
     human_input=True  # Will prompt for human feedback  
 )

工程化最头疼的是输出格式不可控,CrewAI 支持 Pydantic 模型,强制 Agent 输出结构化数据,这对后续的数据清洗非常有帮助。

 from crewai import Task  
from pydantic import BaseModel

class BlogPost(BaseModel):  
    title: str  
    content: str  
    tags: list[str]

task = Task(  
    description='Write a blog post',  
    expected_output='Blog post with title and tags',  
    agent=writer,  
    output_json=BlogPost,  # Structured output  
    output_file='output.json'  # Save to file  
 )

生态与集成

官方内置了一堆工具库,覆盖了搜索(Google/Serper)、文件操作(File/Directory Read)、代码执行(CodeInterpreter)以及各种数据源(PDF, CSV, JSON, GitHub, YouTube)的读取。

模型支持方面利用了 LangChain 的生态,OpenAI, Anthropic, Google Gemini 都能切。想省钱或者数据敏感,用 Ollama 跑本地模型(Llama 3, Mistral)也没问题。

CrewAI vs 其他

经常有人问它和 AutoGen 的区别。简单说CrewAI 像是管理严密的正规军,强调角色(Role)和流程(Process);AutoGen 更像是一个聊天室,Agent 之间通过对话来解决问题,更灵活但也更难控制。至于 LangGraph,那是更底层的图编排工具,控制粒度极细,但上手门槛高。你可以理解为CrewAI 是在 LangChain 之上做了很好的封装,用起来简单。

补充规划、记忆与安全

新版本(0.30+)加入了 Planning Mode,Agent 开干前会先生成个计划书(现在Agent基本上都会有计划了)。记忆系统也升级了:支持短期记忆(本次执行内)、长期记忆(跨执行持久化)甚至实体记忆(记住具体的人和事)。

如果你需要监控整个 Crew 的运行状态,可以开启 Telemetry,导出 JSON 格式的日志做分析。

总结

CrewAI 在处理角色分工明确、流程复杂的知识型工作时表现非常出色。如果你是初学者:先别整太复杂的流程,2-3 个 Agent 起步,把目标定死,用 Pydantic 锁死输出格式,把缓存开起来。等熟悉了 Agent 的操作,再上复杂的层级结构和记忆系统。

https://avoid.overfit.cn/post/03c0bdbc21254d52b80f170a0fa2c567

目录
相关文章
|
3月前
|
人工智能 自然语言处理 数据可视化
AI Agent框架
AI Agent框架选型需权衡灵活性与易用性,根据团队能力、项目复杂度及合规需求选择。开发者可选LangGraph、AutoGen等高定制框架,业务侧推荐Dify、Coze等低代码平台,核心是匹配场景,而非追逐热门技术。
|
3月前
|
人工智能 JSON 机器人
从零开始:用Python和Gemini 3四步搭建你自己的AI Agent
AI Agent并非玄学,核心仅为“循环 + 大模型 + 工具函数”。本文教你用Gemini 3从零搭建能读写文件、执行指令的命令行助手,拆解其“观察-思考-行动”循环机制,揭示智能体背后的简洁本质。
651 17
从零开始:用Python和Gemini 3四步搭建你自己的AI Agent
|
3月前
|
人工智能 运维 监控
从代码到生产推理服务:DevPod 全流程部署 DeepSeek-OCR 模型实战指南
DevPod 重塑 AI 工程化流程,实现从开发、调试到生产部署的全流程闭环。依托云端 GPU 环境与一键镜像构建,打通代码到服务的“最后一公里”,让模型真正高效落地。
|
3月前
|
存储 人工智能 搜索推荐
LEANN:一个极简的本地向量数据库
LEANN是面向RAG的轻量级嵌入式向量数据库,如SQLite般无需服务依赖,可在本地运行。它通过图结构与选择性重计算,节省97%存储且不损精度,让笔记本也能高效检索百万文档,兼顾隐私与性能,堪称个人AI搜索引擎。
357 4
LEANN:一个极简的本地向量数据库
|
4月前
|
数据采集 自然语言处理 供应链
LLM安全新威胁:为什么几百个毒样本就能破坏整个模型
数据投毒通过在训练数据中植入恶意样本,将后门永久嵌入大模型,仅需数百份毒样本即可触发数据泄露、越狱等行为,防御需结合溯源、聚类分析与自动化检测。
407 2
LLM安全新威胁:为什么几百个毒样本就能破坏整个模型
|
4月前
|
存储 人工智能 数据库
构建有记忆的 AI Agent:SQLite 存储 + 向量检索完整方案示例
本文介绍如何为AI Agent构建记忆系统,通过SQLite存储交互历史、向量数据库实现语义检索,结合LLM反思与总结,赋予Agent跨会话记忆、自我反思和目标追踪能力,使其从被动应答工具进化为可长期协作的智能伙伴。
459 2
|
3月前
|
缓存 Linux 开发者
Windows 下手动下载安装配置 uv
UV 是专为 Windows 打造的轻量命令行包管理器,仅需 uv.exe 和 uvx.exe 两个文件,无需 WSL 或管理员权限。支持一键安装、卸载、多版本切换 Python 等工具,内置依赖解析与缓存管理,搭配国内镜像更高效。绿色无残留,开发者友好,真正实现 Linux 般丝滑体验。
2587 2
|
存储 人工智能 监控
从代码生成到自主决策:打造一个Coding驱动的“自我编程”Agent
本文介绍了一种基于LLM的“自我编程”Agent系统,通过代码驱动实现复杂逻辑。该Agent以Python为执行引擎,结合Py4j实现Java与Python交互,支持多工具调用、记忆分层与上下文工程,具备感知、认知、表达、自我评估等能力模块,目标是打造可进化的“1.5线”智能助手。
1671 62
|
5月前
|
人工智能 编解码 运维
2025年智能体平台排名:第一梯队企业盘点与选型指南
AI智能体正从“被动响应”走向“主动决策”,成为企业数字化转型的核心驱动力。本文基于Gartner、IDC报告及全球500强实践,解析智能体市场趋势、第一梯队企业优势,并提供选型框架,助力企业避开“概念陷阱”,选出真正可落地的智能体平台,推动业务增长与智能化升级。
3459 8