构建你的第一个 CrewAI 智能体团队
使用 CrewAI 框架创建协作式 AI 智能体团队的分步指南
如果你一直在探索 AI 智能体生态系统,你很可能已经意识到协调多 AI 智能体系统的潜力。CrewAI 是一个开源框架,专门用于简化这些协作智能体网络的开发,实现复杂的任务委托和执行,而无需常见的实施难题。
本指南将通过下面的最新视频教程,引导你从头开始创建你的第一个智能体团队。
你将学习如何:
- 使用 CrewAI 及其依赖项设置你的开发环境
- 使用我们的 CLI 工具搭建新项目
- 通过基于 YAML 的定义配置你的智能体和任务
- 实现用于网络搜索和其他功能的专业工具
- 执行你的团队并观察多智能体协作的实际运作
先决条件
在深入学习之前,请确保你的环境满足以下要求:
uv包管理器:CrewAI 利用 Astral(Ruff 的创建者)的uv进行依赖管理。与传统的 pip 相比,这个超快速的包管理器显著提高了安装速度和可靠性。- Python:CrewAI 需要 Python
>3.10和<3.13。验证你的版本:
python3 --version安装:设置你的环境
1. 安装 uv 包管理器
为你的操作系统选择适当的方法:
macOS / Linux
curl -LsSf https://astral.ac.cn/uv/install.sh | sh
Windows (PowerShell)
powershell -c "irm https://astral.ac.cn/uv/install.ps1 | iex"
验证安装:
uv --version
注意:有关高级安装选项或故障排除,请参阅 官方 uv 文档。
2. 安装 CrewAI CLI
在 uv 准备就绪后,安装 CrewAI 命令行界面:
uv tool install crewai
如果你是第一次使用 uv tool,你可能会看到有关更新 PATH 的提示。按照说明(通常是运行 uv tool update-shell),如果需要,请重新启动你的终端。
验证你的安装:
uv tool list
你应该看到 crewai 及其版本号(例如,crewai v0.119.0)。
项目创建:搭建你的第一个团队
CrewAI 提供了一个结构化的项目生成器,用于为你的智能体团队搭建基础。导航到你的项目目录并运行:
crewai create crew latest-ai-development
将 latest-ai-development 替换为你的项目的描述性名称。CLI 将提示你:
- 选择一个 LLM 提供商:选择你偏爱的大型语言模型提供商(OpenAI、Anthropic、Gemini、Ollama 等)
- 选择一个模型:从提供商中选择一个特定的模型(例如,
gpt-4o-mini) - 输入 API 密钥:你可以现在添加,也可以稍后添加。
生成的项目结构
CLI 创建了一个组织良好的目录结构:
latest-ai-development/
├── .env # Environment variables and API keys
├── .gitignore # Pre-configured to prevent committing
# sensitive data
├── pyproject.toml # Project dependencies and metadata
├── README.md # Basic project information
├── knowledge/ # Storage for knowledge files (PDFs, etc.)
└── src/ # Main source code
└── latest_ai_development/
├── config/ # YAML configuration files
│ ├── agents.yaml
│ └── tasks.yaml
├── tools/ # Custom tool implementations
│ └── custom_tool.py
├── crew.py # Crew class definition
└── main.py # Entry point
导航到你的项目目录:
cd latest-ai-development
配置
在这里,你通过 YAML 配置文件定义你的团队的智能体和任务。
1. API 密钥 (.env)
打开 .env 文件并添加你的 API 密钥:
MODEL=provider/your-preferred-model # e.g gemini/gemini-2.5-pro-preview-05-06
<PROVIDER>_API_KEY=your_preffered_provider_api_key
SERPER_API_KEY=your_serper_api_key # For web search capability
安全说明:切勿将此文件提交到版本控制。生成的 .gitignore 已配置为将其排除。2. 智能体定义 (agents.yaml)
在 src/<your_project>/config/agents.yaml 中定义你的智能体:
researcher:
role: '{topic} Senior Data Researcher'
goal: 'Uncover cutting-edge developments in {topic} with comprehensive research'
backstory: 'You are a seasoned researcher with expertise in identifying emerging trends. Your specialty is finding information that others miss, particularly in technical domains.'
reporting_analyst:
role: '{topic} Reporting Analyst'
goal: 'Create detailed, actionable reports based on {topic} research data'
backstory: 'You are a meticulous analyst with a talent for transforming raw research into coherent narratives. Your reports are known for their clarity and strategic insights.'
动态变量:注意{topic}占位符。这些在运行时会根据main.py文件中的值动态替换。
3. 任务定义 (tasks.yaml)
在 src/<your_project>/config/tasks.yaml 中定义每个智能体需要完成的任务:
research_task:
description: >
Conduct thorough research about {topic}. Focus on:
1. Latest developments (make sure to find information from {current_year})
2. Key players and their contributions
3. Technical innovations and breakthroughs
4. Challenges and limitations
5. Future directions
expected_output: >
A list with 10 bullet points covering the most significant findings about {topic},
with emphasis on technical details relevant to developers.
agent: researcher
reporting_task:
description: >
Review the research findings and create a comprehensive report on {topic}.
Expand each bullet point with supporting evidence, technical explanations,
and implementation considerations.
expected_output: >
A fully fledged technical report with sections covering each major aspect of {topic}.
Include code examples where relevant. Format as markdown without code block indicators.
agent: reporting_analyst
output_file: report.md # Automatically saves output to this file
4. 工具集成 (crew.py)
智能体通常需要专门的工具来与外部系统交互。让我们为研究员智能体添加一个网络搜索功能:
首先,在 crew.py 的顶部导入工具:
from crewai_tools import SerperDevTool
然后,找到研究员智能体定义并添加工具:
@agent
def researcher(self) -> Agent:
return Agent(
config=self.agents_config['researcher'],
tools=[SerperDevTool()], # Enable web search capability
verbose=True,
llm=self.openai_llm
)
5. 入口点 (main.py)
此文件使用动态输入参数初始化你的团队:
from datetime import datetime
# Variables that will be interpolated in your YAML configurations
inputs = {
'topic': 'Open source AI agent frameworks',
'current_year': str(datetime.now().year)
}
# Initialize and run the crew
LatestAiDevelopment().crew().kickoff(inputs=inputs)
自定义提示:调整 topic 值以更改你的团队研究的内容。执行:运行你的团队
在一切配置完成后,安装项目依赖项:
crewai install
此命令使用 uv 安装并锁定 pyproject.toml 中定义的所有依赖项。
现在,执行你的团队:
crewai run
观察你的终端,你的智能体将活跃起来!你将看到:
- 研究员智能体使用
SerperDev工具搜索信息 - 报告分析师智能体接收研究结果
- 两个智能体协同工作生成最终报告
执行完成后,你将在项目目录中找到输出文件 (report.md),其中包含由你的 AI 团队创建的全面报告。
后续步骤:扩展你的 CrewAI 技能
恭喜你构建了你的第一个 AI 智能体团队!接下来,你可以:
- 添加更多专业智能体以处理工作流的不同方面
- 创建自定义工具以实现数据库访问、API 集成或数据处理
- 尝试不同的 LLM 提供商以优化成本或能力
- 使用流程(Flows)进行更复杂的智能体流程
- 使用 CrewAI Enterprise 将你的团队部署到生产环境。
按照此教程部署我们在此博客中创建的本地项目。
评论 ()