介绍

CrewAI 提供了异步启动 crew 的能力,允许您以非阻塞的方式启动 crew 的执行。 当您希望同时运行多个 crew 或在 crew 执行时执行其他任务时,此功能特别有用。

异步团队执行

要异步启动一个团队,请使用kickoff_async()方法。此方法在一个单独的线程中启动团队执行,允许主线程继续执行其他任务。

方法签名

Code
def kickoff_async(self, inputs: dict) -> CrewOutput:

参数

  • inputs (dict): 一个包含任务所需输入数据的字典。

返回

  • CrewOutput: 表示团队执行结果的对象。

潜在用例

  • 并行内容生成:异步启动多个独立的团队,每个团队负责生成不同主题的内容。例如,一个团队可能研究和起草一篇关于人工智能趋势的文章,而另一个团队则生成关于新产品发布的社交媒体帖子。每个团队独立运作,使得内容生产能够高效扩展。

  • 并发市场研究任务:异步启动多个团队以并行进行市场研究。一个团队可能分析行业趋势,另一个团队研究竞争对手策略,还有一个团队评估消费者情绪。每个团队独立完成任务,从而实现更快、更全面的洞察。

  • 独立旅行规划模块:执行独立的团队来分别规划旅行的不同方面。一个团队可能负责航班选择,另一个负责住宿,第三个则规划活动。每个团队异步工作,使得旅行的各个组成部分能够同时且独立地进行规划,从而更快地得到结果。

示例:单次异步任务执行

以下是一个如何使用 asyncio 异步启动一个任务并等待结果的示例:

Code
import asyncio
from crewai import Crew, Agent, Task

# Create an agent with code execution enabled
coding_agent = Agent(
    role="Python Data Analyst",
    goal="Analyze data and provide insights using Python",
    backstory="You are an experienced data analyst with strong Python skills.",
    allow_code_execution=True
)

# Create a task that requires code execution
data_analysis_task = Task(
    description="Analyze the given dataset and calculate the average age of participants. Ages: {ages}",
    agent=coding_agent
)

# Create a crew and add the task
analysis_crew = Crew(
    agents=[coding_agent],
    tasks=[data_analysis_task]
)

# Async function to kickoff the crew asynchronously
async def async_crew_execution():
    result = await analysis_crew.kickoff_async(inputs={"ages": [25, 30, 35, 40, 45]})
    print("Crew Result:", result)

# Run the async function
asyncio.run(async_crew_execution())

示例:多个异步Crew执行

在这个例子中,我们将展示如何异步启动多个任务,并使用asyncio.gather()等待它们全部完成:

Code
import asyncio
from crewai import Crew, Agent, Task

# Create an agent with code execution enabled
coding_agent = Agent(
    role="Python Data Analyst",
    goal="Analyze data and provide insights using Python",
    backstory="You are an experienced data analyst with strong Python skills.",
    allow_code_execution=True
)

# Create tasks that require code execution
task_1 = Task(
    description="Analyze the first dataset and calculate the average age of participants. Ages: {ages}",
    agent=coding_agent
)

task_2 = Task(
    description="Analyze the second dataset and calculate the average age of participants. Ages: {ages}",
    agent=coding_agent
)

# Create two crews and add tasks
crew_1 = Crew(agents=[coding_agent], tasks=[task_1])
crew_2 = Crew(agents=[coding_agent], tasks=[task_2])

# Async function to kickoff multiple crews asynchronously and wait for all to finish
async def async_multiple_crews():
    result_1 = crew_1.kickoff_async(inputs={"ages": [25, 30, 35, 40, 45]})
    result_2 = crew_2.kickoff_async(inputs={"ages": [20, 22, 24, 28, 30]})

    # Wait for both crews to finish
    results = await asyncio.gather(result_1, result_2)

    for i, result in enumerate(results, 1):
        print(f"Crew {i} Result:", result)

# Run the async function
asyncio.run(async_multiple_crews())

这个页面有帮助吗?