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 的操作,再上复杂的层级结构和记忆系统。
如何学习大模型 AI ?
由于新岗位的生产效率,要优于被取代岗位的生产效率,所以实际上整个社会的生产效率是提升的。
但是具体到个人,只能说是:
“最先掌握AI的人,将会比较晚掌握AI的人有竞争优势”。
这句话,放在计算机、互联网、移动互联网的开局时期,都是一样的道理。
我在一线互联网企业工作十余年里,指导过不少同行后辈。帮助很多人得到了学习和成长。
我意识到有很多经验和知识值得分享给大家,也可以通过我们的能力和经验解答大家在人工智能学习中的很多困惑,所以在工作繁忙的情况下还是坚持各种整理和分享。但苦于知识传播途径有限,很多互联网行业朋友无法获得正确的资料得到学习提升,故此将并将重要的AI大模型资料包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。
第一阶段(10天):初阶应用
该阶段让大家对大模型 AI有一个最前沿的认识,对大模型 AI 的理解超过 95% 的人,可以在相关讨论时发表高级、不跟风、又接地气的见解,别人只会和 AI 聊天,而你能调教 AI,并能用代码将大模型和业务衔接。
- 大模型 AI 能干什么?
- 大模型是怎样获得「智能」的?
- 用好 AI 的核心心法
- 大模型应用业务架构
- 大模型应用技术架构
- 代码示例:向 GPT-3.5 灌入新知识
- 提示工程的意义和核心思想
- Prompt 典型构成
- 指令调优方法论
- 思维链和思维树
- Prompt 攻击和防范
- …
第二阶段(30天):高阶应用
该阶段我们正式进入大模型 AI 进阶实战学习,学会构造私有知识库,扩展 AI 的能力。快速开发一个完整的基于 agent 对话机器人。掌握功能最强的大模型开发框架,抓住最新的技术进展,适合 Python 和 JavaScript 程序员。
- 为什么要做 RAG
- 搭建一个简单的 ChatPDF
- 检索的基础概念
- 什么是向量表示(Embeddings)
- 向量数据库与向量检索
- 基于向量检索的 RAG
- 搭建 RAG 系统的扩展知识
- 混合检索与 RAG-Fusion 简介
- 向量模型本地部署
- …
第三阶段(30天):模型训练
恭喜你,如果学到这里,你基本可以找到一份大模型 AI相关的工作,自己也能训练 GPT 了!通过微调,训练自己的垂直大模型,能独立训练开源多模态大模型,掌握更多技术方案。
到此为止,大概2个月的时间。你已经成为了一名“AI小子”。那么你还想往下探索吗?
- 为什么要做 RAG
- 什么是模型
- 什么是模型训练
- 求解器 & 损失函数简介
- 小实验2:手写一个简单的神经网络并训练它
- 什么是训练/预训练/微调/轻量化微调
- Transformer结构简介
- 轻量化微调
- 实验数据集的构建
- …
第四阶段(20天):商业闭环
对全球大模型从性能、吞吐量、成本等方面有一定的认知,可以在云端和本地等多种环境下部署大模型,找到适合自己的项目/创业方向,做一名被 AI 武装的产品经理。
- 硬件选型
- 带你了解全球大模型
- 使用国产大模型服务
- 搭建 OpenAI 代理
- 热身:基于阿里云 PAI 部署 Stable Diffusion
- 在本地计算机运行大模型
- 大模型的私有化部署
- 基于 vLLM 部署大模型
- 案例:如何优雅地在阿里云私有部署开源大模型
- 部署一套开源 LLM 项目
- 内容安全
- 互联网信息服务算法备案
- …
学习是一个过程,只要学习就会有挑战。天道酬勤,你越努力,就会成为越优秀的自己。
如果你能在15天内完成所有的任务,那你堪称天才。然而,如果你能完成 60-70% 的内容,你就已经开始具备成为一名大模型 AI 的正确特征了。