介绍

CrewAI中的分层过程引入了一种结构化的任务管理方法,模拟传统的组织层次结构,以实现高效的任务委派和执行。 这种系统化的工作流程通过确保任务以最佳效率和准确性处理,从而提高了项目成果。

分层过程旨在利用像GPT-4这样的高级模型,优化令牌使用,同时更高效地处理复杂任务。

分层流程概览

默认情况下,CrewAI中的任务是通过顺序流程进行管理的。然而,采用分层方法可以在任务管理中建立清晰的层次结构,其中“经理”代理协调工作流程、委派任务并验证结果,以实现简化和有效的执行。现在,这个经理代理可以由CrewAI自动创建,也可以由用户明确设置。

主要特点

  • 任务委派:经理代理根据团队成员的职责和能力分配任务。
  • 结果验证: 经理评估结果以确保它们符合所需标准。
  • 高效的工作流程:模拟公司结构,提供有组织的任务管理方法。
  • 系统提示处理: 可选择指定系统是否应使用预定义的提示。
  • 停用词控制: 可选择指定是否使用停用词,支持包括o1模型在内的多种模型。
  • 上下文窗口尊重:通过启用上下文窗口的尊重来优先处理重要上下文,这是现在的默认行为。
  • 委托控制: 默认情况下,委托现在被禁用,以便用户能够明确控制。
  • 每分钟最大请求数: 可配置选项,用于设置每分钟的最大请求数。
  • 最大迭代次数: 限制获取最终答案的最大迭代次数。

实施分层过程

要利用分层流程,必须明确将流程属性设置为Process.hierarchical,因为默认行为是Process.sequential。 定义一个具有指定管理员的团队,并建立明确的指挥链。

在代理级别分配工具,以便在经理的指导下由指定的代理进行任务委派和执行。 也可以在任务级别指定工具,以便在任务执行期间精确控制工具的可用性。

配置manager_llm参数对于分层过程至关重要。 系统需要设置一个管理LLM以确保功能的正常运行,从而实现定制的决策。

Code
from langchain_openai import ChatOpenAI
from crewai import Crew, Process, Agent

# Agents are defined with attributes for backstory, cache, and verbose mode
researcher = Agent(
    role='Researcher',
    goal='Conduct in-depth analysis',
    backstory='Experienced data analyst with a knack for uncovering hidden trends.',
    cache=True,
    verbose=False,
    # tools=[]  # This can be optionally specified; defaults to an empty list
    use_system_prompt=True,  # Enable or disable system prompts for this agent
    max_rpm=30,  # Limit on the number of requests per minute
    max_iter=5  # Maximum number of iterations for a final answer
)
writer = Agent(
    role='Writer',
    goal='Create engaging content',
    backstory='Creative writer passionate about storytelling in technical domains.',
    cache=True,
    verbose=False,
    # tools=[]  # Optionally specify tools; defaults to an empty list
    use_system_prompt=True,  # Enable or disable system prompts for this agent
    max_rpm=30,  # Limit on the number of requests per minute
    max_iter=5  # Maximum number of iterations for a final answer
)

# Establishing the crew with a hierarchical process and additional configurations
project_crew = Crew(
    tasks=[...],  # Tasks to be delegated and executed under the manager's supervision
    agents=[researcher, writer],
    manager_llm=ChatOpenAI(temperature=0, model="gpt-4"),  # Mandatory if manager_agent is not set
    process=Process.hierarchical,  # Specifies the hierarchical management approach
    respect_context_window=True,  # Enable respect of the context window for tasks
    memory=True,  # Enable memory usage for enhanced task execution
    manager_agent=None,  # Optional: explicitly set a specific agent as manager instead of the manager_llm
    planning=True,  # Enable planning feature for pre-execution strategy
)

工作流程在行动

  1. 任务分配: 经理根据每个代理的能力和可用工具,策略性地分配任务。
  2. 执行和审查: 代理完成任务时可以选择异步执行和回调函数,以实现简化的工作流程。
  3. 顺序任务进展:尽管是一个分层过程,任务遵循逻辑顺序以确保顺利进展,由经理的监督促进。

结论

在CrewAI中采用分层流程,通过正确的配置和对系统功能的理解,有助于实现有序且高效的项目管理方法。 利用高级功能和自定义选项,根据您的特定需求定制工作流程,确保任务执行和项目成功的最佳效果。