构建你的第一个 Crew
构建研究 Crew
Section titled “构建研究 Crew”在本指南中,你将创建一个由两个智能体组成的研究 crew,它会收集某个主题的信息并写出一份 markdown 报告。新的 crew 项目采用 JSON-first:智能体定义在 agents/*.jsonc 中,任务和 crew 设置定义在 crew.jsonc 中,crewai run 会直接加载该 JSON 定义。
开始之前,请确保你已经:
- 按照安装指南安装了 CrewAI
- 按照LLM 设置指南设置了 LLM API key
- 如果希望 researcher 使用网页搜索,还需要一个 Serper.dev API key
第 1 步:创建一个新的 Crew
Section titled “第 1 步:创建一个新的 Crew”crewai create crew research_crewcd research_crewCLI 会创建一个 JSON-first 项目:
research_crew/├── .gitignore├── .env├── agents/│ └── researcher.jsonc├── crew.jsonc├── knowledge/├── pyproject.toml├── README.md├── skills/└── tools/第 2 步:定义智能体
Section titled “第 2 步:定义智能体”替换生成的 agents/researcher.jsonc 文件,并添加 agents/analyst.jsonc。文件名就是你在 crew.jsonc 中引用的名称。
{ "role": "Senior Research Specialist for {topic}", "goal": "Find comprehensive and accurate information about {topic}, with a focus on recent developments and key insights.", "backstory": "You are an experienced research specialist who organizes complex information into clear, useful notes.", // Replace with your model, for example "openai/gpt-4o". "llm": "provider/model-id", "tools": ["SerperDevTool"], "settings": { "verbose": true, "allow_delegation": false }}{ "role": "Report Analyst for {topic}", "goal": "Turn research findings into a clear, well-structured report.", "backstory": "You are a careful analyst with strong technical writing skills and a talent for extracting useful insights.", // Replace with your model, for example "openai/gpt-4o". "llm": "provider/model-id", "settings": { "verbose": true, "allow_delegation": false }}将 provider/model-id 替换为你使用的模型,例如 openai/gpt-4o、anthropic/claude-sonnet-4-6 或 gemini/gemini-2.0-flash-001。
第 3 步:定义任务和 crew 设置
Section titled “第 3 步:定义任务和 crew 设置”用下面内容替换 crew.jsonc:
{ "name": "Research Crew", "agents": ["researcher", "analyst"], "tasks": [ { "name": "research_task", "description": "Conduct thorough research on {topic}. Focus on key concepts, recent developments, major challenges, notable applications, and future outlook.", "expected_output": "A comprehensive research document with organized sections, specific facts, and useful examples about {topic}.", "agent": "researcher" }, { "name": "analysis_task", "description": "Analyze the research findings and create a polished report on {topic}. Include an executive summary, key insights, trend analysis, and recommendations.", "expected_output": "A professional markdown report with clear headings, a concise summary, main findings, and recommendations.", "agent": "analyst", "context": ["research_task"], "output_file": "output/report.md", "markdown": true } ], "process": "sequential", "verbose": true, "memory": true, "inputs": { "topic": "Artificial Intelligence in Healthcare" }}context 指向前一个任务的名称,因此 analyst 会接收到 research 任务的输出。inputs 对象为 {topic} 提供默认值。如果你移除了默认值,crewai run 会提示你输入。
第 4 步:设置环境变量
Section titled “第 4 步:设置环境变量”打开 .env,添加你的模型和工具所需的 key:
SERPER_API_KEY=your_serper_api_key# 也在这里添加你的模型提供商 API key。请参阅 LLM 设置指南 获取不同提供商所需的 key。
第 5 步:安装并运行
Section titled “第 5 步:安装并运行”crewai installcrewai runcrewai run 会检测 crew.jsonc,从 agents/ 加载智能体,提示缺失的占位符,并运行 crew。运行结束后,打开 output/report.md。
crew.jsonc定义 crew、任务顺序、流程、记忆和运行时输入。agents/researcher.jsonc和agents/analyst.jsonc定义智能体。- researcher 先运行。
- analyst 随后运行,并带有
context: ["research_task"]。 - 最终任务会写出
output/report.md。
扩展你的 Crew
Section titled “扩展你的 Crew”你可以添加:
- 通过创建新的
agents/<name>.jsonc文件并在crew.jsonc中列出它们,来添加更多智能体 - 通过向
tasks数组追加对象,来添加更多任务 - 通过添加诸如
"FileReadTool"或"SerperDevTool"之类的工具类名,来使用内置工具 - 通过使用
"custom:<name>"来添加自定义工具,它会加载tools/<name>.py - 通过设置
"process": "hierarchical"并提供manager_llm或manager_agent,来启用层级执行