Skip to content

Commit

Permalink
Add CompletionChunkEvent subtypes
Browse files Browse the repository at this point in the history
  • Loading branch information
vachillo committed Oct 17, 2024
1 parent 1ca46e2 commit 4a67b54
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
22 changes: 14 additions & 8 deletions griptape/drivers/prompt/base_prompt_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
TextMessageContent,
observable,
)
from griptape.events import CompletionChunkEvent, EventBus, FinishPromptEvent, StartPromptEvent
from griptape.events import (
ActionCallCompletionChunkEvent,
EventBus,
FinishPromptEvent,
StartPromptEvent,
TextCompletionChunkEvent,
)
from griptape.mixins.exponential_backoff_mixin import ExponentialBackoffMixin
from griptape.mixins.serializable_mixin import SerializableMixin

Expand Down Expand Up @@ -127,13 +133,13 @@ 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, meta={"type": "text"}))
elif isinstance(content, ActionCallDeltaMessageContent):
meta = {"type": "action"}
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), meta=meta))
elif content.partial_input is not None:
EventBus.publish_event(CompletionChunkEvent(token=content.partial_input, meta=meta))
EventBus.publish_event(TextCompletionChunkEvent(token=content.text))
elif isinstance(content, ActionCallDeltaMessageContent) and content.partial_input is not None:
EventBus.publish_event(
ActionCallCompletionChunkEvent(
token=content.partial_input, tag=content.tag, name=content.name, path=content.path
)
)

# Build a complete content from the content deltas
return self.__build_message(list(delta_contents.values()), usage)
Expand Down
4 changes: 4 additions & 0 deletions griptape/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from .start_structure_run_event import StartStructureRunEvent
from .finish_structure_run_event import FinishStructureRunEvent
from .completion_chunk_event import CompletionChunkEvent
from .text_completion_chunk_event import TextCompletionChunkEvent
from .action_call_completion_chunk_event import ActionCallCompletionChunkEvent
from .event_listener import EventListener
from .start_image_generation_event import StartImageGenerationEvent
from .finish_image_generation_event import FinishImageGenerationEvent
Expand Down Expand Up @@ -38,6 +40,8 @@
"StartStructureRunEvent",
"FinishStructureRunEvent",
"CompletionChunkEvent",
"TextCompletionChunkEvent",
"ActionCallCompletionChunkEvent",
"EventListener",
"StartImageGenerationEvent",
"FinishImageGenerationEvent",
Expand Down
14 changes: 14 additions & 0 deletions griptape/events/action_call_completion_chunk_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from __future__ import annotations

from typing import Optional

from attrs import define, field

from griptape.events.completion_chunk_event import CompletionChunkEvent


@define
class ActionCallCompletionChunkEvent(CompletionChunkEvent):
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})
8 changes: 8 additions & 0 deletions griptape/events/text_completion_chunk_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from attrs import define

from griptape.events.completion_chunk_event import CompletionChunkEvent


@define
class TextCompletionChunkEvent(CompletionChunkEvent):
pass

0 comments on commit 4a67b54

Please sign in to comment.