跳转到主要内容

InvokeCrewAIAutomationTool

InvokeCrewAIAutomationTool 提供了 CrewAI 平台 API 与外部 Crew 服务的集成。此工具允许您从 CrewAI 智能体内部调用 CrewAI 平台的自动化并与之交互,从而实现不同 Crew 工作流之间的无缝集成。

安装

uv pip install 'crewai[tools]'

要求

  • CrewAI 平台 API 访问权限
  • 用于身份验证的有效不记名令牌(Bearer Token)
  • 对 CrewAI 平台自动化端点的网络访问权限

用法

以下是如何在 CrewAI 智能体中使用此工具
from crewai import Agent, Task, Crew
from crewai_tools import InvokeCrewAIAutomationTool

# Initialize the tool
automation_tool = InvokeCrewAIAutomationTool(
    crew_api_url="https://data-analysis-crew-[...].crewai.com",
    crew_bearer_token="your_bearer_token_here",
    crew_name="Data Analysis Crew",
    crew_description="Analyzes data and generates insights"
)

# Create a CrewAI agent that uses the tool
automation_coordinator = Agent(
    role='Automation Coordinator',
    goal='Coordinate and execute automated crew tasks',
    backstory='I am an expert at leveraging automation tools to execute complex workflows.',
    tools=[automation_tool],
    verbose=True
)

# Create a task for the agent
analysis_task = Task(
    description="Execute data analysis automation and provide insights",
    agent=automation_coordinator,
    expected_output="Comprehensive data analysis report"
)

# Create a crew with the agent
crew = Crew(
    agents=[automation_coordinator],
    tasks=[analysis_task],
    verbose=2
)

# Run the crew
result = crew.kickoff()
print(result)

工具参数

参数类型必填默认值描述
crew_api_urlstrCrewAI 平台自动化 API 的基础 URL
crew_bearer_tokenstr用于 API 身份验证的不记名令牌(Bearer Token)
crew_namestrCrew 自动化的名称
crew_descriptionstr描述 Crew 自动化的功能
max_polling_timeint600等待任务完成的最长时间(秒)
crew_inputsdict定义自定义输入模式字段的字典

环境变量

CREWAI_API_URL=https://your-crew-automation.crewai.com  # Alternative to passing crew_api_url
CREWAI_BEARER_TOKEN=your_bearer_token_here              # Alternative to passing crew_bearer_token

高级用法

带动态参数的自定义输入模式

from crewai import Agent, Task, Crew
from crewai_tools import InvokeCrewAIAutomationTool
from pydantic import Field

# Define custom input schema
custom_inputs = {
    "year": Field(..., description="Year to retrieve the report for (integer)"),
    "region": Field(default="global", description="Geographic region for analysis"),
    "format": Field(default="summary", description="Report format (summary, detailed, raw)")
}

# Create tool with custom inputs
market_research_tool = InvokeCrewAIAutomationTool(
    crew_api_url="https://state-of-ai-report-crew-[...].crewai.com",
    crew_bearer_token="your_bearer_token_here",
    crew_name="State of AI Report",
    crew_description="Retrieves a comprehensive report on state of AI for a given year and region",
    crew_inputs=custom_inputs,
    max_polling_time=15 * 60  # 15 minutes timeout
)

# Create an agent with the tool
research_agent = Agent(
    role="Research Coordinator",
    goal="Coordinate and execute market research tasks",
    backstory="You are an expert at coordinating research tasks and leveraging automation tools.",
    tools=[market_research_tool],
    verbose=True
)

# Create and execute a task with custom parameters
research_task = Task(
    description="Conduct market research on AI tools market for 2024 in North America with detailed format",
    agent=research_agent,
    expected_output="Comprehensive market research report"
)

crew = Crew(
    agents=[research_agent],
    tasks=[research_task]
)

result = crew.kickoff()

多阶段自动化工作流

from crewai import Agent, Task, Crew, Process
from crewai_tools import InvokeCrewAIAutomationTool

# Initialize different automation tools
data_collection_tool = InvokeCrewAIAutomationTool(
    crew_api_url="https://data-collection-crew-[...].crewai.com",
    crew_bearer_token="your_bearer_token_here",
    crew_name="Data Collection Automation",
    crew_description="Collects and preprocesses raw data"
)

analysis_tool = InvokeCrewAIAutomationTool(
    crew_api_url="https://analysis-crew-[...].crewai.com",
    crew_bearer_token="your_bearer_token_here",
    crew_name="Analysis Automation",
    crew_description="Performs advanced data analysis and modeling"
)

reporting_tool = InvokeCrewAIAutomationTool(
    crew_api_url="https://reporting-crew-[...].crewai.com",
    crew_bearer_token="your_bearer_token_here",
    crew_name="Reporting Automation",
    crew_description="Generates comprehensive reports and visualizations"
)

# Create specialized agents
data_collector = Agent(
    role='Data Collection Specialist',
    goal='Gather and preprocess data from various sources',
    backstory='I specialize in collecting and cleaning data from multiple sources.',
    tools=[data_collection_tool]
)

data_analyst = Agent(
    role='Data Analysis Expert',
    goal='Perform advanced analysis on collected data',
    backstory='I am an expert in statistical analysis and machine learning.',
    tools=[analysis_tool]
)

report_generator = Agent(
    role='Report Generation Specialist',
    goal='Create comprehensive reports and visualizations',
    backstory='I excel at creating clear, actionable reports from complex data.',
    tools=[reporting_tool]
)

# Create sequential tasks
collection_task = Task(
    description="Collect market data for Q4 2024 analysis",
    agent=data_collector
)

analysis_task = Task(
    description="Analyze collected data to identify trends and patterns",
    agent=data_analyst
)

reporting_task = Task(
    description="Generate executive summary report with key insights and recommendations",
    agent=report_generator
)

# Create a crew with sequential processing
crew = Crew(
    agents=[data_collector, data_analyst, report_generator],
    tasks=[collection_task, analysis_task, reporting_task],
    process=Process.sequential,
    verbose=2
)

result = crew.kickoff()

用例

分布式 Crew 编排

  • 协调多个专门的 Crew 自动化,以处理复杂的多阶段工作流
  • 实现不同自动化服务之间的无缝交接,以完成全面的任务执行
  • 通过将工作负载分布到多个 CrewAI 平台自动化中来扩展处理能力

跨平台集成

  • 将 CrewAI 智能体与 CrewAI 平台自动化连接起来,实现混合的本地-云工作流
  • 利用专门的自动化功能,同时保持本地控制和编排
  • 实现本地智能体与基于云的自动化服务之间的安全协作

企业自动化管道

  • 创建企业级自动化管道,将本地智能与云处理能力相结合
  • 实现跨多个自动化服务的复杂业务工作流
  • 为数据分析、报告和决策制定实现可扩展、可重复的流程

动态工作流组合

  • 通过根据任务需求链接不同的自动化服务来动态组合工作流
  • 实现自适应处理,其中自动化的选择取决于数据特征或业务规则
  • 创建可以以多种方式组合的灵活、可重用的自动化组件

专业领域处理

  • 从通用智能体访问特定领域的自动化(金融分析、法律研究、技术文档)
  • 利用预构建的、专门的 Crew 自动化,无需重建复杂的领域逻辑
  • 使智能体能够通过有针对性的自动化服务获得专家级的能力

自定义输入模式

在定义 crew_inputs 时,使用 Pydantic Field 对象来指定输入参数
from pydantic import Field

crew_inputs = {
    "required_param": Field(..., description="This parameter is required"),
    "optional_param": Field(default="default_value", description="This parameter is optional"),
    "typed_param": Field(..., description="Integer parameter", ge=1, le=100)  # With validation
}

错误处理

该工具为常见场景提供了全面的错误处理
  • API 连接错误:与 CrewAI 平台的网络连接问题
  • 身份验证错误:无效或过期的不记名令牌(Bearer Token)
  • 超时错误:任务超出最大轮询时间
  • 任务失败:在执行过程中失败的 Crew 自动化
  • 输入验证错误:传递给自动化端点的参数无效

API 端点

该工具与两个主要的 API 端点进行交互
  • POST {crew_api_url}/kickoff:启动一个新的 Crew 自动化任务
  • GET {crew_api_url}/status/{crew_id}:检查正在运行的任务的状态

注意

  • 该工具会每秒自动轮询状态端点,直到任务完成或超时
  • 成功的任务直接返回结果,而失败的任务则返回错误信息
  • 不记名令牌(Bearer Token)应保持安全,不要在生产环境中硬编码
  • 考虑使用环境变量来存储像不记名令牌这样的敏感配置
  • 自定义输入模式必须与目标 Crew 自动化的预期参数兼容