层级流程
CrewAI 中的层级流程为任务管理引入了一种结构化方法,模拟传统组织层级,以实现高效的任务委派和执行。 这一系统化工作流通过确保任务以最佳效率和准确性完成,从而提升项目成果。
层级流程概览
Section titled “层级流程概览”默认情况下,CrewAI 中的任务通过顺序流程进行管理。不过,采用层级方法可以在任务管理中建立清晰的层级, 由一个“经理”代理协调工作流、分派任务并验证结果,从而实现流畅而高效的执行。这个经理代理现在既可以由 CrewAI 自动创建,也可以由用户显式设置。
- 任务委派:经理代理会根据各成员的角色和能力在团队成员之间分配任务。
- 结果验证:经理会评估结果,确保它们符合要求的标准。
- 高效工作流:模拟企业组织结构,为任务管理提供有序的方法。
- 系统提示处理:可选地指定系统是否应使用预定义提示。
- 停止词控制:可选地指定是否使用停止词,支持包括 o1 系列在内的多种模型。
- 上下文窗口遵循:通过启用遵循上下文窗口来优先处理重要上下文,这现在是默认行为。
- 委派控制:委派现在默认禁用,以便给用户显式控制权。
- 每分钟最大请求数:可配置选项,用于设置每分钟的最大请求数量。
- 最大迭代次数:限制获取最终答案时的最大迭代次数。
实现层级流程
Section titled “实现层级流程”要使用层级流程,必须显式将 process 属性设置为 Process.hierarchical,因为默认行为是 Process.sequential。
定义一个带有指定经理的 crew,并建立清晰的指挥链。
from crewai import Crew, Process, Agent
# Agents are defined with attributes for backstory, cache, and verbose moderesearcher = Agent( role='Researcher', goal='Conduct in-depth analysis', backstory='Experienced data analyst with a knack for uncovering hidden trends.',)writer = Agent( role='Writer', goal='Create engaging content', backstory='Creative writer passionate about storytelling in technical domains.',)
# Establishing the crew with a hierarchical process and additional configurationsproject_crew = Crew( tasks=[...], # Tasks to be delegated and executed under the manager's supervision agents=[researcher, writer], manager_llm="gpt-4o", # Specify which LLM the manager should use process=Process.hierarchical, planning=True,)使用自定义经理代理
Section titled “使用自定义经理代理”或者,你也可以创建一个具有特定属性的自定义经理代理,以满足项目的管理需求。这样你就能更好地控制经理的行为和能力。
# Define a custom manager agentmanager = Agent( role="Project Manager", goal="Efficiently manage the crew and ensure high-quality task completion", backstory="You're an experienced project manager, skilled in overseeing complex projects and guiding teams to success.", allow_delegation=True,)
# Use the custom manager in your crewproject_crew = Crew( tasks=[...], agents=[researcher, writer], manager_agent=manager, # Use your custom manager agent process=Process.hierarchical, planning=True,)- 任务分配:经理会结合每个代理的能力和可用工具,策略性地分派任务。
- 执行与审阅:代理在完成任务时可选择异步执行和回调函数,以实现更顺畅的工作流。
- 顺序化任务推进:尽管这是一个层级流程,任务仍会遵循逻辑顺序平稳推进,由经理监督协调。
在 CrewAI 中采用层级流程,并配合正确的配置和对系统能力的理解,可以为项目管理提供有序且高效的方法。 利用这些高级特性和自定义项,将工作流调整为最适合你的需求,确保最佳任务执行和项目成功。