主题
构建你的第一个团队
分步教程:使用 JSON 优先的团队配置创建一个协作式 AI 团队。
构建一个研究团队
在本指南中,你将创建一个由两个智能体组成的研究团队,它负责收集某个主题的信息并撰写 Markdown 报告。新的团队项目采用 JSON 优先方式:智能体定义在 agents/*.jsonc 中,任务和团队设置在 crew.jsonc 中定义,crewai run 会直接加载 JSON 定义。
前提条件
开始之前,请确保你已经:
- 已按照安装指南安装 CrewAI
- 已按照 LLM 设置指南设置好 LLM API 密钥
- 如果你希望研究员使用网络搜索,还需要一个 Serper.dev API 密钥
步骤 1:创建一个新团队
bash
crewai create crew research_crew
cd research_crewCLI 会创建一个 JSON 优先的项目:
text
research_crew/
├── .gitignore
├── .env
├── agents/
│ └── researcher.jsonc
├── crew.jsonc
├── knowledge/
├── pyproject.toml
├── README.md
├── skills/
└── tools/TIP
需要旧的 crew.py、config/agents.yaml 和 config/tasks.yaml 布局?使用 crewai create crew research_crew --classic 创建。
步骤 2:定义你的智能体
替换生成的 agents/researcher.jsonc 文件,并添加 agents/analyst.jsonc。文件名就是你在 crew.jsonc 中引用的名称。
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
}
}jsonc
{
"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-3.7-flash。
步骤 3:定义任务和团队设置
将 crew.jsonc 替换为:
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 指向先前的任务名称,因此分析师会收到研究任务的输出。inputs 对象为 {topic} 提供默认值。如果你移除某个默认值,crewai run 会提示你输入。
步骤 4:设置环境变量
打开 .env,添加你的模型和工具所需的密钥:
sh
SERPER_API_KEY=your_serper_api_key
# Add your model provider API key here too.各服务商的专属密钥请参阅 LLM 设置指南。
步骤 5:安装并运行
bash
crewai install
crewai runcrewai run 会检测 crew.jsonc,从 agents/ 加载智能体,提示输入缺失的占位符,然后运行团队。运行结束后,打开 output/report.md。
工作原理
crew.jsonc定义团队、任务顺序、流程、记忆和运行时输入。agents/researcher.jsonc和agents/analyst.jsonc定义智能体。- 研究员先运行。
- 分析师通过
context: ["research_task"]第二个运行。 - 最终任务写出
output/report.md。
扩展你的团队
你可以添加:
- 更多智能体:创建新的
agents/<name>.jsonc文件并在crew.jsonc中列出 - 更多任务:向
tasks数组追加对象 - 内置工具:添加工具类名,如
"FileReadTool"或"SerperDevTool" - 自定义工具:使用
"custom:<name>",它会加载tools/<name>.py - 层级执行:使用
"process": "hierarchical"并配合manager_llm或manager_agent
WARNING
只从你信任的来源运行 JSON 团队项目。custom:<name> 工具和 {"python": "module.attribute"} 引用会在团队加载时执行本地 Python 代码。
TIP
你现在有了一个可运行的 JSON 优先团队,它能研究某个主题并撰写报告。