为每个条目启动 Crew
CrewAI 提供了针对列表中每个条目分别启动 crew 的能力,让你能够对列表中的每个项目执行同一组任务。这个功能在需要对多个条目执行相同任务集时尤其有用。
为每个条目启动 Crew
Section titled “为每个条目启动 Crew”要为列表中的每个条目启动 crew,请使用 kickoff_for_each() 方法。这个方法会针对列表中的每个条目执行 crew,从而高效处理多个项目。
下面是一个为列表中的每个条目启动 crew 的示例:
from crewai import Crew, Agent, Task
# Create an agent with code execution enabledcoding_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 executiondata_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 calculated from the dataset")
# Create a crew and add the taskanalysis_crew = Crew( agents=[coding_agent], tasks=[data_analysis_task], verbose=True, memory=False)
datasets = [ { "ages": [25, 30, 35, 40, 45] }, { "ages": [20, 25, 30, 35, 40] }, { "ages": [30, 35, 40, 45, 50] }]
# Execute the crewresult = analysis_crew.kickoff_for_each(inputs=datasets)