-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New streaming events,
EventListener
listen on parent types (#1266)
- Loading branch information
Showing
22 changed files
with
334 additions
and
60 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
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 griptape.drivers import OpenAiChatPromptDriver | ||
from griptape.events import ActionChunkEvent, EventBus, EventListener, TextChunkEvent | ||
from griptape.structures import Pipeline | ||
from griptape.tasks import ToolkitTask | ||
from griptape.tools import PromptSummaryTool, WebScraperTool | ||
|
||
EventBus.add_event_listeners( | ||
[ | ||
EventListener( | ||
lambda e: print(str(e), end="", flush=True), | ||
event_types=[TextChunkEvent], | ||
), | ||
EventListener( | ||
lambda e: print(str(e), end="", flush=True), | ||
event_types=[ActionChunkEvent], | ||
), | ||
] | ||
) | ||
|
||
pipeline = Pipeline() | ||
pipeline.add_tasks( | ||
ToolkitTask( | ||
"Based on https://griptape.ai, tell me what griptape is.", | ||
prompt_driver=OpenAiChatPromptDriver(model="gpt-4o", stream=True), | ||
tools=[WebScraperTool(off_prompt=True), PromptSummaryTool(off_prompt=False)], | ||
) | ||
) | ||
|
||
pipeline.run() |
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,33 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Optional | ||
|
||
from attrs import define, field | ||
|
||
from griptape.events.base_chunk_event import BaseChunkEvent | ||
|
||
|
||
@define | ||
class ActionChunkEvent(BaseChunkEvent): | ||
partial_input: Optional[str] = field(default=None, kw_only=True, metadata={"serializable": True}) | ||
tag: Optional[str] = field(default=None, kw_only=True, metadata={"serializable": True}) | ||
name: Optional[str] = field(default=None, kw_only=True, metadata={"serializable": True}) | ||
path: Optional[str] = field(default=None, kw_only=True, metadata={"serializable": True}) | ||
|
||
def __str__(self) -> str: | ||
parts = [] | ||
|
||
if self.name: | ||
parts.append(self.name) | ||
if self.path: | ||
parts.append(f".{self.path}") | ||
if self.tag: | ||
parts.append(f" ({self.tag})") | ||
|
||
if self.partial_input: | ||
if parts: | ||
parts.append(f"\n{self.partial_input}") | ||
else: | ||
parts.append(self.partial_input) | ||
|
||
return "".join(parts) |
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,13 @@ | ||
from abc import abstractmethod | ||
|
||
from attrs import define, field | ||
|
||
from griptape.events.base_event import BaseEvent | ||
|
||
|
||
@define | ||
class BaseChunkEvent(BaseEvent): | ||
index: int = field(default=0, metadata={"serializable": True}) | ||
|
||
@abstractmethod | ||
def __str__(self) -> str: ... |
This file was deleted.
Oops, something went wrong.
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,11 @@ | ||
from attrs import define, field | ||
|
||
from griptape.events.base_chunk_event import BaseChunkEvent | ||
|
||
|
||
@define | ||
class TextChunkEvent(BaseChunkEvent): | ||
token: str = field(kw_only=True, metadata={"serializable": True}) | ||
|
||
def __str__(self) -> str: | ||
return self.token |
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,11 @@ | ||
from attrs import define, field | ||
|
||
from griptape.events.base_chunk_event import BaseChunkEvent | ||
|
||
|
||
@define | ||
class MockChunkEvent(BaseChunkEvent): | ||
token: str = field(kw_only=True, metadata={"serializable": True}) | ||
|
||
def __str__(self) -> str: | ||
return "mock " + self.token |
Oops, something went wrong.