异步启动 Crew
CrewAI 支持异步启动 crew,使你能够以非阻塞方式开始 crew 执行。这个功能在你想同时运行多个 crew,或者在 crew 执行期间还需要做其他事情时尤其有用。
CrewAI 提供两种异步执行方式:
| 方法 | 类型 | 说明 |
|---|---|---|
akickoff() | 原生 async | 在整个执行链中使用真正的 async/await |
kickoff_async() | 基于线程 | 使用 asyncio.to_thread 包装同步执行 |
使用 akickoff() 的原生异步执行
Section titled “使用 akickoff() 的原生异步执行”akickoff() 方法提供真正的原生异步执行,在整个执行链中使用 async/await,包括任务执行、内存操作和知识查询。
async def akickoff(self, inputs: dict) -> CrewOutput:inputs(dict):包含任务所需输入数据的字典。
CrewOutput:表示 crew 执行结果的对象。
示例:原生异步 Crew 执行
Section titled “示例:原生异步 Crew 执行”import asynciofrom crewai import Crew, Agent, Task
# Create an agentcoding_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 taskdata_analysis_task = Task( description="Analyze the given dataset and calculate the average age of participants. Ages: {ages}", agent=coding_agent, expected_output="The average age of the participants.")
# Create a crewanalysis_crew = Crew( agents=[coding_agent], tasks=[data_analysis_task])
# Native async executionasync def main(): result = await analysis_crew.akickoff(inputs={"ages": [25, 30, 35, 40, 45]}) print("Crew Result:", result)
asyncio.run(main())示例:多个原生异步 Crew
Section titled “示例:多个原生异步 Crew”使用 asyncio.gather() 并行运行多个 crew:
import asynciofrom crewai import Crew, Agent, Task
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)
task_1 = Task( description="Analyze the first dataset and calculate the average age. Ages: {ages}", agent=coding_agent, expected_output="The average age of the participants.")
task_2 = Task( description="Analyze the second dataset and calculate the average age. Ages: {ages}", agent=coding_agent, expected_output="The average age of the participants.")
crew_1 = Crew(agents=[coding_agent], tasks=[task_1])crew_2 = Crew(agents=[coding_agent], tasks=[task_2])
async def main(): results = await asyncio.gather( crew_1.akickoff(inputs={"ages": [25, 30, 35, 40, 45]}), crew_2.akickoff(inputs={"ages": [20, 22, 24, 28, 30]}) )
for i, result in enumerate(results, 1): print(f"Crew {i} Result:", result)
asyncio.run(main())示例:多个输入的原生异步
Section titled “示例:多个输入的原生异步”使用 akickoff_for_each() 以原生异步并发执行你的 crew,处理多个输入:
import asynciofrom crewai import Crew, Agent, Task
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)
data_analysis_task = Task( description="Analyze the dataset and calculate the average age. Ages: {ages}", agent=coding_agent, expected_output="The average age of the participants.")
analysis_crew = Crew( agents=[coding_agent], tasks=[data_analysis_task])
async def main(): datasets = [ {"ages": [25, 30, 35, 40, 45]}, {"ages": [20, 22, 24, 28, 30]}, {"ages": [30, 35, 40, 45, 50]} ]
results = await analysis_crew.akickoff_for_each(datasets)
for i, result in enumerate(results, 1): print(f"Dataset {i} Result:", result)
asyncio.run(main())基于线程的异步:kickoff_async()
Section titled “基于线程的异步:kickoff_async()”kickoff_async() 通过把同步的 kickoff() 包装进线程来提供异步执行。这适合更简单的异步集成或向后兼容。
async def kickoff_async(self, inputs: dict) -> CrewOutput:inputs(dict):包含任务所需输入数据的字典。
CrewOutput:表示 crew 执行结果的对象。
示例:基于线程的异步执行
Section titled “示例:基于线程的异步执行”import asynciofrom crewai import Crew, Agent, Task
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)
data_analysis_task = Task( description="Analyze the given dataset and calculate the average age of participants. Ages: {ages}", agent=coding_agent, expected_output="The average age of the participants.")
analysis_crew = Crew( agents=[coding_agent], tasks=[data_analysis_task])
async def async_crew_execution(): result = await analysis_crew.kickoff_async(inputs={"ages": [25, 30, 35, 40, 45]}) print("Crew Result:", result)
asyncio.run(async_crew_execution())示例:多个基于线程的异步 Crew
Section titled “示例:多个基于线程的异步 Crew”import asynciofrom crewai import Crew, Agent, Task
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)
task_1 = Task( description="Analyze the first dataset and calculate the average age of participants. Ages: {ages}", agent=coding_agent, expected_output="The average age of the participants.")
task_2 = Task( description="Analyze the second dataset and calculate the average age of participants. Ages: {ages}", agent=coding_agent, expected_output="The average age of the participants.")
crew_1 = Crew(agents=[coding_agent], tasks=[task_1])crew_2 = Crew(agents=[coding_agent], tasks=[task_2])
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]})
results = await asyncio.gather(result_1, result_2)
for i, result in enumerate(results, 1): print(f"Crew {i} Result:", result)
asyncio.run(async_multiple_crews())异步流式输出
Section titled “异步流式输出”当 stream=True 设置在 crew 上时,两种异步方法都支持流式输出:
import asynciofrom crewai import Crew, Agent, Task
agent = Agent( role="Researcher", goal="Research and summarize topics", backstory="You are an expert researcher.")
task = Task( description="Research the topic: {topic}", agent=agent, expected_output="A comprehensive summary of the topic.")
crew = Crew( agents=[agent], tasks=[task], stream=True # Enable streaming)
async def main(): streaming_output = await crew.akickoff(inputs={"topic": "AI trends in 2024"})
# Async iteration over streaming chunks async for chunk in streaming_output: print(f"Chunk: {chunk.content}")
# Access final result after streaming completes result = streaming_output.result print(f"Final result: {result.raw}")
asyncio.run(main())-
并行内容生成:异步启动多个彼此独立的 crew,分别负责不同主题的内容生成。例如,一个 crew 研究并撰写 AI 趋势文章,另一个 crew 为新品发布生成社交媒体文案。
-
并发市场调研任务:并行启动多个 crew,分别执行市场调研。一个 crew 分析行业趋势,另一个分析竞争对手策略,第三个评估消费者情绪。
-
独立的旅行规划模块:把旅行的不同部分分给独立 crew 分别规划。一个 crew 处理机票方案,一个处理住宿,一个规划活动。
选择 akickoff() 还是 kickoff_async()
Section titled “选择 akickoff() 还是 kickoff_async()”| 特性 | akickoff() | kickoff_async() |
|---|---|---|
| 执行模型 | 原生 async/await | 基于线程的包装 |
| 任务执行 | 使用 aexecute_sync() 的异步执行 | 在线程池中同步执行 |
| 内存操作 | 异步 | 在线程池中同步 |
| 知识检索 | 异步 | 在线程池中同步 |
| 最适合 | 高并发、I/O 密集型工作负载 | 简单的异步集成 |
| 流式支持 | 是 | 是 |