updated fixes for conditional tasks

This commit is contained in:
Lorenze Jay
2024-07-16 15:10:13 -07:00
parent 8710e6f545
commit e329e09b58
5 changed files with 3152 additions and 9 deletions

View File

@@ -0,0 +1,40 @@
from typing import Callable, Any
from pydantic import Field
from crewai.task import Task
from crewai.tasks.task_output import TaskOutput
class ConditionalTask(Task):
"""
A task that can be conditionally executed based on the output of another task.
Note: This cannot be the only task you have in your crew and cannot be the first since its needs context from the previous task.
"""
condition: Callable[[TaskOutput], bool] = Field(
default=None,
description="Maximum number of retries for an agent to execute a task when an error occurs.",
)
def __init__(
self,
*args,
condition: Callable[[Any], bool],
**kwargs,
):
super().__init__(*args, **kwargs)
self.condition = condition
def should_execute(self, context: TaskOutput) -> bool:
"""
Determines whether the conditional task should be executed based on the provided context.
Args:
context (Any): The context or output from the previous task that will be evaluated by the condition.
Returns:
bool: True if the task should be executed, False otherwise.
"""
if self.condition:
return self.condition(context)
return True