文档索引 获取完整文档索引: https://docs.crewai.com.cn/llms.txt
在深入了解之前,请使用此文件来浏览所有可用页面。
观看:使用编码智能体技能构建 CrewAI 智能体与流程
安装我们的编码智能体技能(Claude Code, Codex 等),以便快速让您的编码智能体在 CrewAI 中运行。 您可以使用 npx skills add crewaiinc/skills 进行安装。
在本指南中,您将创建一个 Flow ,设定研究主题,运行一个包含单个智能体 (使用网络搜索的研究员)的团队,并最终在磁盘上生成一份 Markdown 报告 。Flow 是构建生产级应用程序的推荐方式:它们负责管理状态 和执行顺序 ,而智能体 则在团队步骤中执行具体工作。 如果您尚未安装 CrewAI,请先按照安装指南 进行操作。 先决条件
构建您的第一个 Flow
创建 Flow 项目
在终端中,脚手架化一个 Flow 项目(文件夹名称使用下划线,例如 latest_ai_flow) crewai create flow latest-ai-flow
cd latest_ai_flow
这将会在 src/latest_ai_flow/ 下创建一个 Flow 应用,包括一个位于 crews/content_crew/ 的起始团队,您将在后续步骤中将其替换为一个最小化的单智能体 研究团队。
在 `agents.yaml` 中配置单个智能体
将 src/latest_ai_flow/crews/content_crew/config/agents.yaml 的内容替换为单一研究员配置。像 {topic} 这样的变量将由 crew.kickoff(inputs=...) 填充。 # src/latest_ai_flow/crews/content_crew/config/agents.yaml
researcher :
role : >
{topic} Senior Data Researcher
goal : >
Uncover cutting-edge developments in {topic}
backstory : >
You're a seasoned researcher with a knack for uncovering the latest
developments in {topic}. You find the most relevant information and
present it clearly.
在 `tasks.yaml` 中配置单个任务
# src/latest_ai_flow/crews/content_crew/config/tasks.yaml
research_task :
description : >
Conduct thorough research about {topic}. Use web search to find current,
credible information. The current year is 2026.
expected_output : >
A markdown report with clear sections: key trends, notable tools or companies,
and implications. Aim for 800–1200 words. No fenced code blocks around the whole document.
agent : researcher
output_file : output/report.md
连接团队类 (`content_crew.py`)
将生成的团队指向您的 YAML 文件,并将 SerperDevTool 附加给研究员。 # src/latest_ai_flow/crews/content_crew/content_crew.py
from typing import List
from crewai import Agent, Crew, Process, Task
from crewai.agents.agent_builder.base_agent import BaseAgent
from crewai.project import CrewBase, agent, crew, task
from crewai_tools import SerperDevTool
@CrewBase
class ResearchCrew :
"""Single-agent research crew used inside the Flow."""
agents: List[BaseAgent]
tasks: List[Task]
agents_config = "config/agents.yaml"
tasks_config = "config/tasks.yaml"
@agent
def researcher ( self ) -> Agent:
return Agent(
config = self .agents_config[ "researcher" ], # type: ignore[index]
verbose = True ,
tools = [SerperDevTool()],
)
@task
def research_task ( self ) -> Task:
return Task(
config = self .tasks_config[ "research_task" ], # type: ignore[index]
)
@crew
def crew ( self ) -> Crew:
return Crew(
agents = self .agents,
tasks = self .tasks,
process = Process.sequential,
verbose = True ,
)
在 `main.py` 中定义 Flow
将团队连接到 Flow:@start() 步骤在状态 中设置主题,@listen 步骤运行团队。任务的 output_file 依然会写入 output/report.md。 # src/latest_ai_flow/main.py
from pydantic import BaseModel
from crewai.flow import Flow, listen, start
from latest_ai_flow.crews.content_crew.content_crew import ResearchCrew
class ResearchFlowState ( BaseModel ):
topic: str = ""
report: str = ""
class LatestAiFlow (Flow[ResearchFlowState]):
@start ()
def prepare_topic ( self , crewai_trigger_payload : dict | None = None ):
if crewai_trigger_payload:
self .state.topic = crewai_trigger_payload.get( "topic" , "AI Agents" )
else :
self .state.topic = "AI Agents"
print ( f "Topic: { self .state.topic } " )
@listen (prepare_topic)
def run_research ( self ):
result = ResearchCrew().crew().kickoff( inputs = { "topic" : self .state.topic})
self .state.report = result.raw
print ( "Research crew finished." )
@listen (run_research)
def summarize ( self ):
print ( "Report path: output/report.md" )
def kickoff ():
LatestAiFlow().kickoff()
def plot ():
LatestAiFlow().plot()
if __name__ == "__main__" :
kickoff()
如果您的包名称与 latest_ai_flow 不同,请修改 ResearchCrew 的导入路径以匹配您的项目模块路径。
安装并运行
crewai install
crewai run
crewai run 会执行您项目中定义的 Flow 入口点(与运行团队的命令相同;在 pyproject.toml 中项目类型为 "flow")。
检查输出
您应该会看到来自 Flow 和团队的日志。打开 output/report.md 查看生成的报告(摘要)。 # AI Agents in 2026: Landscape and Trends
## Executive summary
…
## Key trends
- **Tool use and orchestration** — …
- **Enterprise adoption** — …
## Implications
…
您的实际文件会更长,并反映实时搜索结果。
本次运行的流程解析
Flow — LatestAiFlow 首先运行 prepare_topic,然后是 run_research,最后是 summarize。状态(topic, report)保存在 Flow 中。
Crew — ResearchCrew 执行一个带有一个智能体的任务:研究员使用 Serper 搜索网络,然后撰写结构化的报告。
Artifact — 任务的 output_file 将报告写入 output/report.md。
要深入了解 Flow 模式(路由、持久化、人机协作),请参阅构建您的第一个 Flow 和 Flows 。对于不使用 Flow 的团队,请参阅团队 (Crews) 。对于不带任务的单一 Agent 和 kickoff(),请参阅智能体 (Agents) 。
您现在拥有了一个包含智能体团队和已保存报告的端到端 Flow —— 这是添加更多步骤、团队或工具的坚实基础。
命名一致性
YAML 键(researcher, research_task)必须与您 @CrewBase 类上的方法名称相匹配。有关完整的装饰器模式,请参阅团队 (Crews) 。
一旦在本地运行成功且项目已托管在 GitHub 仓库中,即可将您的 Flow 推送到 CrewAI AMP 。在项目根目录下:
身份验证
创建部署
检查状态和日志
代码更新后进行推送部署
列出或删除部署
首次部署通常需要约 1 分钟 。完整的先决条件和 Web UI 流程请参阅部署到 AMP 。
加入社区 讨论想法、分享项目,并与其他 CrewAI 开发者联系。