Skip to content

Commit

Permalink
Rename EventBus to event_bus
Browse files Browse the repository at this point in the history
  • Loading branch information
collindutter committed Aug 8, 2024
1 parent 025437b commit 0f19385
Show file tree
Hide file tree
Showing 23 changed files with 109 additions and 109 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Ability to set custom schema properties on Tool Activities via `extra_schema_properties`.
- Parameter `structure` to `BaseTask`.
- Method `try_find_task` to `Structure`.
- Global event bus, `griptape.events.EventBus`, for publishing and subscribing to events.
- Global event bus, `griptape.events.event_bus`, for publishing and subscribing to events.

### Changed
- **BREAKING**: Removed all uses of `EventPublisherMixin` in favor of `EventBus`.
- **BREAKING**: Removed all uses of `EventPublisherMixin` in favor of `event_bus`.
- **BREAKING**: Removed `EventPublisherMixin`.
- `BaseTask.add_parent/child` will now call `self.structure.add_task` if possible.

Expand Down
24 changes: 12 additions & 12 deletions docs/griptape-framework/drivers/event-listener-drivers.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import os

from griptape.drivers import AmazonSqsEventListenerDriver
from griptape.events import (
EventListener, EventBus
EventListener, event_bus
)
from griptape.rules import Rule
from griptape.structures import Agent

EventBus.add_event_listeners(
event_bus.add_event_listeners(
[
EventListener(
driver=AmazonSqsEventListenerDriver(
Expand Down Expand Up @@ -84,12 +84,12 @@ import os

from griptape.drivers import AmazonSqsEventListenerDriver
from griptape.events import (
EventListener, EventBus
EventListener, event_bus
)
from griptape.rules import Rule
from griptape.structures import Agent

EventBus.add_event_listeners(
event_bus.add_event_listeners(
[
EventListener(
driver=AmazonSqsEventListenerDriver(
Expand Down Expand Up @@ -132,12 +132,12 @@ from griptape.drivers import AwsIotCoreEventListenerDriver, OpenAiChatPromptDriv
from griptape.events import (
EventListener,
FinishStructureRunEvent,
EventBus
event_bus
)
from griptape.rules import Rule
from griptape.structures import Agent

EventBus.add_event_listeners(
event_bus.add_event_listeners(
[
EventListener(
event_types=[FinishStructureRunEvent],
Expand Down Expand Up @@ -179,11 +179,11 @@ from griptape.drivers import GriptapeCloudEventListenerDriver
from griptape.events import (
EventListener,
FinishStructureRunEvent,
EventBus
event_bus
)
from griptape.structures import Agent

EventBus.add_event_listeners(
event_bus.add_event_listeners(
[
EventListener(
event_types=[FinishStructureRunEvent],
Expand All @@ -210,11 +210,11 @@ from griptape.drivers import WebhookEventListenerDriver
from griptape.events import (
EventListener,
FinishStructureRunEvent,
EventBus
event_bus
)
from griptape.structures import Agent

EventBus.add_event_listeners(
event_bus.add_event_listeners(
[
EventListener(
event_types=[FinishStructureRunEvent],
Expand Down Expand Up @@ -242,11 +242,11 @@ from griptape.drivers import PusherEventListenerDriver
from griptape.events import (
EventListener,
FinishStructureRunEvent,
EventBus
event_bus
)
from griptape.structures import Agent

EventBus.add_event_listeners(
event_bus.add_event_listeners(
[
EventListener(
event_types=[FinishStructureRunEvent],
Expand Down
22 changes: 11 additions & 11 deletions docs/griptape-framework/misc/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ search:

## Overview

You can configure the global [EventBus](../../reference/griptape/events/event_bus.md) with [EventListener](../../reference/griptape/events/event_listener.md)s to listen for various framework events.
You can configure the global [event_bus](../../reference/griptape/events/event_bus.md) with [EventListener](../../reference/griptape/events/event_listener.md)s to listen for various framework events.
See [Event Listener Drivers](../drivers/event-listener-drivers.md) for examples on forwarding events to external services.

## Specific Event Types
Expand All @@ -23,14 +23,14 @@ from griptape.events import (
StartPromptEvent,
FinishPromptEvent,
EventListener,
EventBus
event_bus
)


def handler(event: BaseEvent):
print(event.__class__)

EventBus.add_event_listeners([
event_bus.add_event_listeners([
EventListener(
handler,
event_types=[
Expand Down Expand Up @@ -69,7 +69,7 @@ Or listen to all events:

```python
from griptape.structures import Agent
from griptape.events import BaseEvent, EventListener, EventBus
from griptape.events import BaseEvent, EventListener, event_bus



Expand All @@ -80,7 +80,7 @@ def handler1(event: BaseEvent):
def handler2(event: BaseEvent):
print("Handler 2", event.__class__)

EventBus.event_listeners=[
event_bus.event_listeners=[
EventListener(handler1),
EventListener(handler2),
]
Expand Down Expand Up @@ -131,7 +131,7 @@ Handler 2 <class 'griptape.events.finish_structure_run_event.FinishStructureRunE
You can use the [CompletionChunkEvent](../../reference/griptape/events/completion_chunk_event.md) to stream the completion results from Prompt Drivers.

```python
from griptape.events import CompletionChunkEvent, EventListener, EventBus
from griptape.events import CompletionChunkEvent, EventListener, event_bus
from griptape.tasks import ToolkitTask
from griptape.structures import Pipeline
from griptape.tools import WebScraper, TaskMemoryClient
Expand All @@ -140,7 +140,7 @@ from griptape.drivers import OpenAiChatPromptDriver



EventBus.add_event_listeners([
event_bus.add_event_listeners([
EventListener(
lambda e: print(e.token, end="", flush=True),
event_types=[CompletionChunkEvent],
Expand Down Expand Up @@ -188,13 +188,13 @@ To count tokens, you can use Event Listeners and the [TokenCounter](../../refere

```python
from griptape import utils
from griptape.events import BaseEvent, StartPromptEvent, FinishPromptEvent, EventListener, EventBus
from griptape.events import BaseEvent, StartPromptEvent, FinishPromptEvent, EventListener, event_bus
from griptape.structures import Agent


token_counter = utils.TokenCounter()

EventBus.add_event_listeners([
event_bus.add_event_listeners([
EventListener(
lambda e: token_counter.add_tokens(e.token_count),
event_types=[StartPromptEvent, FinishPromptEvent],
Expand Down Expand Up @@ -245,10 +245,10 @@ You can use the [StartPromptEvent](../../reference/griptape/events/start_prompt_

```python
from griptape.structures import Agent
from griptape.events import BaseEvent, StartPromptEvent, EventListener, EventBus
from griptape.events import BaseEvent, StartPromptEvent, EventListener, event_bus


EventBus.add_event_listeners([EventListener(handler=lambda e: print(e), event_types=[StartPromptEvent])])
event_bus.add_event_listeners([EventListener(handler=lambda e: print(e), event_types=[StartPromptEvent])])

def handler(event: BaseEvent):
if isinstance(event, StartPromptEvent):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from attrs import define, field

from griptape.events import EventBus, FinishAudioTranscriptionEvent, StartAudioTranscriptionEvent
from griptape.events import FinishAudioTranscriptionEvent, StartAudioTranscriptionEvent, event_bus
from griptape.mixins import ExponentialBackoffMixin, SerializableMixin

if TYPE_CHECKING:
Expand All @@ -17,10 +17,10 @@ class BaseAudioTranscriptionDriver(SerializableMixin, ExponentialBackoffMixin, A
model: str = field(kw_only=True, metadata={"serializable": True})

def before_run(self) -> None:
EventBus.publish_event(StartAudioTranscriptionEvent())
event_bus.publish_event(StartAudioTranscriptionEvent())

def after_run(self) -> None:
EventBus.publish_event(FinishAudioTranscriptionEvent())
event_bus.publish_event(FinishAudioTranscriptionEvent())

def run(self, audio: AudioArtifact, prompts: Optional[list[str]] = None) -> TextArtifact:
for attempt in self.retrying():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from attrs import define, field

from griptape.events import EventBus, FinishImageGenerationEvent, StartImageGenerationEvent
from griptape.events import FinishImageGenerationEvent, StartImageGenerationEvent, event_bus
from griptape.mixins import ExponentialBackoffMixin, SerializableMixin

if TYPE_CHECKING:
Expand All @@ -17,10 +17,10 @@ class BaseImageGenerationDriver(SerializableMixin, ExponentialBackoffMixin, ABC)
model: str = field(kw_only=True, metadata={"serializable": True})

def before_run(self, prompts: list[str], negative_prompts: Optional[list[str]] = None) -> None:
EventBus.publish_event(StartImageGenerationEvent(prompts=prompts, negative_prompts=negative_prompts))
event_bus.publish_event(StartImageGenerationEvent(prompts=prompts, negative_prompts=negative_prompts))

def after_run(self) -> None:
EventBus.publish_event(FinishImageGenerationEvent())
event_bus.publish_event(FinishImageGenerationEvent())

def run_text_to_image(self, prompts: list[str], negative_prompts: Optional[list[str]] = None) -> ImageArtifact:
for attempt in self.retrying():
Expand Down
6 changes: 3 additions & 3 deletions griptape/drivers/image_query/base_image_query_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from attrs import define, field

from griptape.events import EventBus, FinishImageQueryEvent, StartImageQueryEvent
from griptape.events import FinishImageQueryEvent, StartImageQueryEvent, event_bus
from griptape.mixins import ExponentialBackoffMixin, SerializableMixin

if TYPE_CHECKING:
Expand All @@ -17,12 +17,12 @@ class BaseImageQueryDriver(SerializableMixin, ExponentialBackoffMixin, ABC):
max_tokens: int = field(default=256, kw_only=True, metadata={"serializable": True})

def before_run(self, query: str, images: list[ImageArtifact]) -> None:
EventBus.publish_event(
event_bus.publish_event(
StartImageQueryEvent(query=query, images_info=[image.to_text() for image in images]),
)

def after_run(self, result: str) -> None:
EventBus.publish_event(FinishImageQueryEvent(result=result))
event_bus.publish_event(FinishImageQueryEvent(result=result))

def query(self, query: str, images: list[ImageArtifact]) -> TextArtifact:
for attempt in self.retrying():
Expand Down
12 changes: 6 additions & 6 deletions griptape/drivers/prompt/base_prompt_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
TextMessageContent,
observable,
)
from griptape.events import CompletionChunkEvent, EventBus, FinishPromptEvent, StartPromptEvent
from griptape.events import CompletionChunkEvent, FinishPromptEvent, StartPromptEvent, event_bus
from griptape.mixins import ExponentialBackoffMixin, SerializableMixin

if TYPE_CHECKING:
Expand Down Expand Up @@ -49,10 +49,10 @@ class BasePromptDriver(SerializableMixin, ExponentialBackoffMixin, ABC):
use_native_tools: bool = field(default=False, kw_only=True, metadata={"serializable": True})

def before_run(self, prompt_stack: PromptStack) -> None:
EventBus.publish_event(StartPromptEvent(model=self.model, prompt_stack=prompt_stack))
event_bus.publish_event(StartPromptEvent(model=self.model, prompt_stack=prompt_stack))

def after_run(self, result: Message) -> None:
EventBus.publish_event(
event_bus.publish_event(
FinishPromptEvent(
model=self.model,
result=result.value,
Expand Down Expand Up @@ -128,12 +128,12 @@ def __process_stream(self, prompt_stack: PromptStack) -> Message:
else:
delta_contents[content.index] = [content]
if isinstance(content, TextDeltaMessageContent):
EventBus.publish_event(CompletionChunkEvent(token=content.text))
event_bus.publish_event(CompletionChunkEvent(token=content.text))
elif isinstance(content, ActionCallDeltaMessageContent):
if content.tag is not None and content.name is not None and content.path is not None:
EventBus.publish_event(CompletionChunkEvent(token=str(content)))
event_bus.publish_event(CompletionChunkEvent(token=str(content)))
elif content.partial_input is not None:
EventBus.publish_event(CompletionChunkEvent(token=content.partial_input))
event_bus.publish_event(CompletionChunkEvent(token=content.partial_input))

# Build a complete content from the content deltas
result = self.__build_message(list(delta_contents.values()), usage)
Expand Down
6 changes: 3 additions & 3 deletions griptape/drivers/text_to_speech/base_text_to_speech_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from attrs import define, field

from griptape.events import EventBus
from griptape.events import event_bus
from griptape.events.finish_text_to_speech_event import FinishTextToSpeechEvent
from griptape.events.start_text_to_speech_event import StartTextToSpeechEvent
from griptape.mixins import ExponentialBackoffMixin, SerializableMixin
Expand All @@ -19,10 +19,10 @@ class BaseTextToSpeechDriver(SerializableMixin, ExponentialBackoffMixin, ABC):
model: str = field(kw_only=True, metadata={"serializable": True})

def before_run(self, prompts: list[str]) -> None:
EventBus.publish_event(StartTextToSpeechEvent(prompts=prompts))
event_bus.publish_event(StartTextToSpeechEvent(prompts=prompts))

def after_run(self) -> None:
EventBus.publish_event(FinishTextToSpeechEvent())
event_bus.publish_event(FinishTextToSpeechEvent())

def run_text_to_audio(self, prompts: list[str]) -> AudioArtifact:
for attempt in self.retrying():
Expand Down
4 changes: 2 additions & 2 deletions griptape/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from .base_audio_transcription_event import BaseAudioTranscriptionEvent
from .start_audio_transcription_event import StartAudioTranscriptionEvent
from .finish_audio_transcription_event import FinishAudioTranscriptionEvent
from .event_bus import EventBus
from .event_bus import event_bus

__all__ = [
"BaseEvent",
Expand All @@ -49,5 +49,5 @@
"BaseAudioTranscriptionEvent",
"StartAudioTranscriptionEvent",
"FinishAudioTranscriptionEvent",
"EventBus",
"event_bus",
]
2 changes: 1 addition & 1 deletion griptape/events/event_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ def clear_event_listeners(self) -> None:
self._event_listeners.clear()


EventBus = _EventBus()
event_bus = _EventBus()
6 changes: 3 additions & 3 deletions griptape/structures/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
VectorStoreRetrievalRagModule,
)
from griptape.engines.rag.stages import ResponseRagStage, RetrievalRagStage
from griptape.events import EventBus, FinishStructureRunEvent, StartStructureRunEvent
from griptape.events import FinishStructureRunEvent, StartStructureRunEvent, event_bus
from griptape.memory import TaskMemory
from griptape.memory.meta import MetaMemory
from griptape.memory.structure import ConversationMemory
Expand Down Expand Up @@ -257,7 +257,7 @@ def before_run(self, args: Any) -> None:

[task.reset() for task in self.tasks]

EventBus.publish_event(
event_bus.publish_event(
StartStructureRunEvent(
structure_id=self.id,
input_task_input=self.input_task.input,
Expand All @@ -269,7 +269,7 @@ def before_run(self, args: Any) -> None:

@observable
def after_run(self) -> None:
EventBus.publish_event(
event_bus.publish_event(
FinishStructureRunEvent(
structure_id=self.id,
output_task_input=self.output_task.input,
Expand Down
6 changes: 3 additions & 3 deletions griptape/tasks/actions_subtask.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from griptape import utils
from griptape.artifacts import ActionArtifact, BaseArtifact, ErrorArtifact, ListArtifact, TextArtifact
from griptape.common import ToolAction
from griptape.events import EventBus, FinishActionsSubtaskEvent, StartActionsSubtaskEvent
from griptape.events import FinishActionsSubtaskEvent, StartActionsSubtaskEvent, event_bus
from griptape.mixins import ActionsSubtaskOriginMixin
from griptape.tasks import BaseTask
from griptape.utils import remove_null_values_in_dict_recursively
Expand Down Expand Up @@ -91,7 +91,7 @@ def attach_to(self, parent_task: BaseTask) -> None:
self.output = ErrorArtifact(f"ToolAction input parsing error: {e}", exception=e)

def before_run(self) -> None:
EventBus.publish_event(
event_bus.publish_event(
StartActionsSubtaskEvent(
task_id=self.id,
task_parent_ids=self.parent_ids,
Expand Down Expand Up @@ -157,7 +157,7 @@ def execute_action(self, action: ToolAction) -> tuple[str, BaseArtifact]:
def after_run(self) -> None:
response = self.output.to_text() if isinstance(self.output, BaseArtifact) else str(self.output)

EventBus.publish_event(
event_bus.publish_event(
FinishActionsSubtaskEvent(
task_id=self.id,
task_parent_ids=self.parent_ids,
Expand Down
Loading

0 comments on commit 0f19385

Please sign in to comment.