跳转到主要内容
本指南解释如何使用注释来正确引用 智能体任务 以及 crew.py 文件中的其他组件。

Introduction

CrewAI框架中的注解用于装饰类和方法,为您的智能体团队中的各个组件提供元数据和功能。这些注解有助于组织和结构化您的代码,使其更具可读性和可维护性。

可用注解

CrewAI框架提供以下注解:
  • @CrewBase: 用于装饰主智能体群组类。
  • @agent: 装饰那些定义并返回智能体对象的方法。
  • @task: 装饰那些定义并返回Task对象的方法。
  • @crew: 装饰用于创建并返回Crew对象的方法。
  • @llm: 装饰那些初始化和返回语言模型对象的方法。
  • @tool: 装饰那些初始化并返回 Tool 对象的方法。
  • @callback: 用于定义回调方法。
  • @output_json: 用于输出JSON数据的方法。
  • @output_pydantic: 用于输出Pydantic模型的方法。
  • @cache_handler: 用于定义缓存处理方法。

使用示例

让我们通过示例来了解如何使用这些注解:

1. 智能体群组基类

@CrewBase
class LinkedinProfileCrew():
    """LinkedinProfile crew"""
    agents_config = 'config/agents.yaml'
    tasks_config = 'config/tasks.yaml'
@CrewBase 注解用于装饰主 crew 类。该类通常包含用于创建智能体、任务以及 crew 本身的配置和方法。
@CrewBase 不仅仅注册类:
  • 配置引导: 在类文件旁查找 agents_configtasks_config(默认为 config/agents.yamlconfig/tasks.yaml),在实例化时加载它们,如果文件缺失则安全地回退到空字典。
  • 装饰器编排: 保留对每个标记有 @agent@task@before_kickoff@after_kickoff 的方法的备忘录引用,以便每个 crew 只实例化一次,并按声明顺序执行。
  • 钩子连接: 自动将保留的启动钩子附加到由 @crew 方法返回的 Crew 对象上,使它们在 .kickoff() 之前和之后运行。
  • MCP 集成:当类定义了 mcp_server_params 时,get_mcp_tools() 会惰性地启动一个 MCP 服务器适配器,填充已声明的工具,并且一个内部的后启动钩子会停止该适配器。有关适配器配置的详细信息,请参阅 MCP 概述

2. 工具定义

@tool
def myLinkedInProfileTool(self):
    return LinkedInProfileTool()
@tool 注解用于装饰返回工具对象的方法。这些工具可供智能体用来执行特定任务。

3. 大语言模型定义

@llm
def groq_llm(self):
    api_key = os.getenv('api_key')
    return ChatGroq(api_key=api_key, temperature=0, model_name="mixtral-8x7b-32768")
@llm 注解用于修饰那些初始化并返回语言模型对象的方法。这些语言模型由智能体用于自然语言处理任务。

4. 智能体定义

@agent
def researcher(self) -> Agent:
    return Agent(
        config=self.agents_config['researcher']
    )
@agent 注解用于装饰定义并返回智能体对象的方法。

5. 任务定义

@task
def research_task(self) -> Task:
    return Task(
        config=self.tasks_config['research_linkedin_task'],
        agent=self.researcher()
    )
@task 注解用于装饰那些定义并返回 Task 对象的方法。这些方法指定了任务配置以及负责该任务的智能体。

6. 团队创建

@crew
def crew(self) -> Crew:
    """Creates the LinkedinProfile crew"""
    return Crew(
        agents=self.agents,
        tasks=self.tasks,
        process=Process.sequential,
        verbose=True
    )
@crew 注解用于装饰创建并返回 Crew 对象的方法。该方法将所有组件(智能体和任务)组装成一个功能完整的 crew。

YAML 配置

智能体配置通常存储在YAML文件中。以下是研究员智能体的agents.yaml文件示例:
researcher:
    role: >
        LinkedIn Profile Senior Data Researcher
    goal: >
        Uncover detailed LinkedIn profiles based on provided name {name} and domain {domain}
        Generate a Dall-E image based on domain {domain}
    backstory: >
        You're a seasoned researcher with a knack for uncovering the most relevant LinkedIn profiles.
        Known for your ability to navigate LinkedIn efficiently, you excel at gathering and presenting
        professional information clearly and concisely.
    allow_delegation: False
    verbose: True
    llm: groq_llm
    tools:
        - myLinkedInProfileTool
        - mySerperDevTool
        - myDallETool
此YAML配置对应于在LinkedinProfileCrew类中定义的研究员智能体。该配置指定了智能体的角色、目标、背景故事以及其他属性,例如其使用的LLM和工具。 注意YAML文件中的llmtools如何对应Python类中带有@llm@tool装饰器的方法。

Best Practices

  • 命名一致性: 为你的方法使用清晰且一致的命名规范。例如,智能体方法可以按其角色命名(例如:研究员、报告分析师)。
  • 环境变量: 使用环境变量来存储敏感信息,例如API密钥。
  • 灵活性: 通过允许轻松添加或移除智能体和任务,设计您的crewai团队以保持灵活性。
  • YAML-代码对应关系: 确保您的YAML文件中的名称和结构正确对应于Python代码中的装饰方法。
遵循这些指南并正确使用注释,您可以使用CrewAI框架创建结构良好且易于维护的智能体团队。