-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RunnableMixin, implement in Structures, Tasks, Tools
- Loading branch information
1 parent
bf96755
commit 1a003bd
Showing
41 changed files
with
269 additions
and
71 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import json | ||
import re | ||
|
||
from griptape.structures import Agent | ||
from griptape.tasks import PromptTask | ||
from griptape.tasks.base_task import BaseTask | ||
|
||
SSN_PATTERN = re.compile(r"\b\d{3}-\d{2}-\d{4}\b") | ||
|
||
original_input = None | ||
|
||
|
||
def on_before_run(task: BaseTask) -> None: | ||
global original_input | ||
|
||
original_input = task.input.value | ||
|
||
if isinstance(task, PromptTask): | ||
task.input = SSN_PATTERN.sub("xxx-xx-xxxx", task.input.value) | ||
|
||
|
||
def on_after_run(task: BaseTask) -> None: | ||
if task.output is not None: | ||
task.output.value = json.dumps( | ||
{"original_input": original_input, "masked_input": task.input.value, "output": task.output.value}, indent=2 | ||
) | ||
|
||
|
||
agent = Agent( | ||
tasks=[ | ||
PromptTask( | ||
"Respond to this user: {{ args[0] }}", | ||
on_before_run=on_before_run, | ||
on_after_run=on_after_run, | ||
) | ||
] | ||
) | ||
agent.run("Hello! My favorite color is blue, and my social security number is 123-45-6789.") |
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,35 @@ | ||
from __future__ import annotations | ||
|
||
from abc import ABC, abstractmethod | ||
from typing import Any, Callable, Generic, Optional, TypeVar, cast | ||
|
||
from attrs import define, field | ||
|
||
# Generics magic that allows us to reference the type of the class that is implementing the mixin | ||
T = TypeVar("T", bound="RunnableMixin") | ||
|
||
|
||
@define() | ||
class RunnableMixin(ABC, Generic[T]): | ||
"""Mixin for classes that can be "run". | ||
Implementing classes should pass themselves as the generic type to ensure that the correct type is used in the callbacks. | ||
Attributes: | ||
on_before_run: Optional callback that is called at the very beginning of the `run` method. | ||
on_after_run: Optional callback that is called at the very end of the `run` method. | ||
""" | ||
|
||
on_before_run: Optional[Callable[[T], None]] = field(kw_only=True, default=None) | ||
on_after_run: Optional[Callable[[T], None]] = field(kw_only=True, default=None) | ||
|
||
def before_run(self, *args, **kwargs) -> Any: | ||
if self.on_before_run is not None: | ||
self.on_before_run(cast(T, self)) | ||
|
||
@abstractmethod | ||
def run(self, *args, **kwargs) -> Any: ... | ||
|
||
def after_run(self, *args, **kwargs) -> Any: | ||
if self.on_after_run is not None: | ||
self.on_after_run(cast(T, self)) |
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
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
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
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
Oops, something went wrong.