리스트별 크루 실행
리스트별 크루 실행 (Kickoff Crew for Each)
크루가 실행할 입력값이 리스트 형태로 여러 개 있을 때, kickoff_for_each()를 쓰면 리스트의 각 항목마다 크루를 실행할 수 있습니다. 동일한 태스크 집합을 여러 항목에 반복 적용해야 할 때 특히 유용합니다. 예컨데 서로 다른 데이터셋에 같은 분석 로직을 적용하는 식입니다.
출처: 공식문서
각 항목마다 크루 실행하기
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,
expected_output="The average age calculated from the dataset"
)
# Create a crew and add the task
analysis_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 crew
result = analysis_crew.kickoff_for_each(inputs=datasets)
리스트의 각 항목마다 크루를 실행하려면 kickoff_for_each() 메서드를 사용합니다. 이 메서드는 리스트의 각 항목에 대해 크루를 실행해 여러 항목을 효율적으로 처리하게 해 줍니다. inputs에 각 입력 딕셔너리들의 리스트를 넘기면, 크루가 항목별로 한 번씩 실행되고 결과 리스트를 반환합니다.
더 알아보기
- Kickoff Crew Asynchronously — 비동기 크루 실행과
akickoff_for_each() - CrewAI 공식 개념: Crews
- Tasks 개념