跳转到内容

构建你的第一个 Crew

在本指南中,你将创建一个由两个智能体组成的研究 crew,它会收集某个主题的信息并写出一份 markdown 报告。新的 crew 项目采用 JSON-first:智能体定义在 agents/*.jsonc 中,任务和 crew 设置定义在 crew.jsonc 中,crewai run 会直接加载该 JSON 定义。

开始之前,请确保你已经:

  1. 按照安装指南安装了 CrewAI
  2. 按照LLM 设置指南设置了 LLM API key
  3. 如果希望 researcher 使用网页搜索,还需要一个 Serper.dev API key
Terminal window
crewai create crew research_crew
cd research_crew

CLI 会创建一个 JSON-first 项目:

research_crew/
├── .gitignore
├── .env
├── agents/
│ └── researcher.jsonc
├── crew.jsonc
├── knowledge/
├── pyproject.toml
├── README.md
├── skills/
└── tools/

替换生成的 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-4oanthropic/claude-sonnet-4-6gemini/gemini-2.0-flash-001

用下面内容替换 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 会提示你输入。

打开 .env,添加你的模型和工具所需的 key:

Terminal window
SERPER_API_KEY=your_serper_api_key
# 也在这里添加你的模型提供商 API key。

请参阅 LLM 设置指南 获取不同提供商所需的 key。

Terminal window
crewai install
crewai run

crewai run 会检测 crew.jsonc,从 agents/ 加载智能体,提示缺失的占位符,并运行 crew。运行结束后,打开 output/report.md

  1. crew.jsonc 定义 crew、任务顺序、流程、记忆和运行时输入。
  2. agents/researcher.jsoncagents/analyst.jsonc 定义智能体。
  3. researcher 先运行。
  4. analyst 随后运行,并带有 context: ["research_task"]
  5. 最终任务会写出 output/report.md

你可以添加:

  • 通过创建新的 agents/<name>.jsonc 文件并在 crew.jsonc 中列出它们,来添加更多智能体
  • 通过向 tasks 数组追加对象,来添加更多任务
  • 通过添加诸如 "FileReadTool""SerperDevTool" 之类的工具类名,来使用内置工具
  • 通过使用 "custom:<name>" 来添加自定义工具,它会加载 tools/<name>.py
  • 通过设置 "process": "hierarchical" 并提供 manager_llmmanager_agent,来启用层级执行