-
Notifications
You must be signed in to change notification settings - Fork 171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ControlFlowTask
#913
Closed
Closed
Add ControlFlowTask
#913
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from attrs import define | ||
|
||
from griptape.artifacts import BaseArtifact | ||
|
||
|
||
@define | ||
class ControlFlowArtifact(BaseArtifact): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from __future__ import annotations | ||
|
||
from abc import ABC | ||
|
||
from attrs import define | ||
|
||
from griptape.tasks import BaseTask | ||
|
||
|
||
@define | ||
class BaseControlFlowTask(BaseTask, ABC): | ||
def before_run(self) -> None: | ||
super().before_run() | ||
|
||
self.structure.logger.info("%s %s\nInput: %s", self.__class__.__name__, self.id, self.input.to_text()) | ||
|
||
def after_run(self) -> None: | ||
super().after_run() | ||
|
||
self.structure.logger.info("%s %s\nOutput: %s", self.__class__.__name__, self.id, self.output.to_text()) | ||
|
||
def _cancel_children_rec(self, task: BaseTask, chosen_task: BaseTask) -> None: | ||
for child in filter(lambda child: child != chosen_task, task.children): | ||
if all(parent.is_complete() for parent in filter(lambda parent: parent != task, child.parents)): | ||
child.state = BaseTask.State.CANCELLED | ||
self._cancel_children_rec(child, chosen_task) | ||
|
||
def _get_task(self, task: str | BaseTask) -> BaseTask: | ||
return self.structure.find_task(task) if isinstance(task, str) else task |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Callable | ||
|
||
from attrs import define, field | ||
|
||
from griptape.artifacts import BaseArtifact, ErrorArtifact, ListArtifact, TextArtifact | ||
from griptape.tasks import BaseControlFlowTask, BaseTask | ||
|
||
|
||
@define | ||
class ChoiceControlFlowTask(BaseControlFlowTask): | ||
control_flow_fn: Callable[[list[BaseTask] | BaseTask], list[BaseTask | str] | BaseTask | str] = field( | ||
metadata={"serializable": False} | ||
) | ||
|
||
@property | ||
def input(self) -> BaseArtifact: | ||
if len(self.parents) == 1: | ||
return self.parents[0].output if self.parents[0].output is not None else TextArtifact("") | ||
parents = filter(lambda parent: parent.output is not None, self.parents) | ||
return ListArtifact( | ||
[ | ||
parent.output | ||
for parent in parents # pyright: ignore[reportArgumentType] | ||
] | ||
) | ||
|
||
def run(self) -> BaseArtifact: | ||
tasks = self.control_flow_fn( | ||
[artifact.value for artifact in self.input.value] | ||
if isinstance(self.input, ListArtifact) | ||
else self.input.value | ||
) | ||
|
||
if not isinstance(tasks, list): | ||
tasks = [tasks] | ||
|
||
if tasks is None: | ||
tasks = [] | ||
|
||
tasks = [self._get_task(task) for task in tasks] | ||
|
||
for task in tasks: | ||
if task.id not in self.child_ids: | ||
self.output = ErrorArtifact(f"ControlFlowTask {self.id} did not return a valid child task") | ||
return self.output | ||
|
||
self.output = ( | ||
ListArtifact( | ||
[ | ||
parent.value.output | ||
for parent in filter(lambda parent: parent.value.output is not None, self.input.value) | ||
] | ||
) | ||
if isinstance(self.input, ListArtifact) | ||
else self.input.value.output | ||
) | ||
self._cancel_children_rec(self, task) | ||
|
||
return self.output # pyright: ignore[reportReturnType] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need a dedicated Task for this? What if we added
control_flow_fn
toBaseTask
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
future proof if there is other types of control flow tasks