跳转到主要内容

InvokeCrewAIAutomationTool

InvokeCrewAIAutomationTool提供CrewAI平台API与外部团队服务的集成。此工具允许您从CrewAI代理内部调用和与CrewAI平台自动化进行交互,从而实现不同团队工作流之间的无缝集成。

安装

uv pip install 'crewai[tools]'

要求

  • CrewAI平台API访问
  • 用于身份验证的有效Bearer令牌
  • 对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令牌
crew_namestr团队自动化名称
crew_descriptionstr团队自动化功能描述
max_polling_timeint600等待任务完成的最大时间(秒)
crew_inputs字典定义自定义输入模式字段的字典

环境变量

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()

用例

分布式团队编排

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

跨平台集成

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

企业自动化管道

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

动态工作流组合

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

专业领域处理

  • 从通用代理访问领域特定自动化(金融分析、法律研究、技术文档)
  • 利用预构建的专业团队自动化,而无需重新构建复杂的领域逻辑
  • 使代理能够通过有针对性的自动化服务访问专家级功能

自定义输入模式

定义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令牌
  • 超时错误:任务超出最大轮询时间
  • 任务失败:Crew自动化在执行过程中失败
  • 输入验证错误:传递给自动化端点的参数无效

API端点

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

注意

  • 该工具会自动每秒轮询状态端点,直到完成或超时
  • 成功任务直接返回结果,失败任务返回错误信息
  • Bearer令牌应妥善保管,不应在生产环境中硬编码
  • 考虑使用环境变量来存储Bearer令牌等敏感配置
  • 自定义输入模式必须与目标团队自动化所需的参数兼容