Files
crewAI/docs/how-to/Conditional-Tasks.md
2024-07-16 16:04:41 -07:00

2.4 KiB

title, description
title description
Conditional Tasks Learn how to use conditional tasks in a crewAI kickoff

Introduction

Conditional Tasks in crewAI allow for dynamic workflow adaptation based on the outcomes of previous tasks. This powerful feature enables crews to make decisions and execute tasks selectively, enhancing the flexibility and efficiency of your AI-driven processes.

from typing import List

from pydantic import BaseModel
from crewai import Agent, Crew
from crewai.tasks.conditional_task import ConditionalTask
from crewai.tasks.task_output import TaskOutput
from crewai.task import Task
from crewai_tools import SerperDevTool


# Define a condition function for the conditional task - if false task will be skipped, true, then execute task
def is_data_fetched(output: TaskOutput) -> bool:
    if len(output.pydantic.events) >= 10: # this will skip this task
        return False
    return True


# Define the agents
data_fetcher_agent = Agent(
    role="Data Fetcher",
    goal="Fetch data online using Serper tool",
    backstory="Backstory 1",
    verbose=True,
    tools=[SerperDevTool()],
)

data_processor_agent = Agent(
    role="Data Processor",
    goal="Process fetched data",
    backstory="Backstory 2",
    verbose=True,
)

summary_generator_agent = Agent(
    role="Summary Generator",
    goal="Generate summary from fetched data",
    backstory="Backstory 3",
    verbose=True,
)


class EventOutput(BaseModel):
    events: List[str]


task1 = Task(
    name="Data Fetching Task",
    description="Fetch data about events in San Francisco using Serper tool",
    expected_output="List of 10 things to do in SF this week",
    agent=data_fetcher_agent,
    output_pydantic=EventOutput,
)

conditional_task = ConditionalTask(
    name="Data Processing Task",
    description="Process data if data fetching is successful",
    expected_output="List of 11 Things to do in SF this week ",
    condition=is_data_fetched,
    agent=data_processor_agent,
)

task3 = Task(
    name="Summary Generation Task",
    description="Generate summary of events in San Francisco from fetched data",
    expected_output="summary_generated",
    agent=summary_generator_agent,
)

# Create a crew with the tasks
crew = Crew(
    agents=[data_fetcher_agent, data_processor_agent, summary_generator_agent],
    tasks=[task1, conditional_task, task3],
    verbose=2,
)

result = crew.kickoff()
print("results", result)