-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(drivers-prompt-openai):add audio input/output support to OpenAiC…
…hatPromptDriver
- Loading branch information
1 parent
ce01b8b
commit f023372
Showing
20 changed files
with
535 additions
and
98 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
24 changes: 24 additions & 0 deletions
24
griptape/common/prompt_stack/contents/audio_delta_message_content.py
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,24 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Optional | ||
|
||
from attrs import define, field | ||
|
||
from griptape.common import BaseDeltaMessageContent | ||
|
||
|
||
@define(kw_only=True) | ||
class AudioDeltaMessageContent(BaseDeltaMessageContent): | ||
"""A delta message content for audio data. | ||
Attributes: | ||
id: The ID of the audio data. | ||
data: Base64 encoded audio data. | ||
transcript: The transcript of the audio data. | ||
expires_at: The Unix timestamp (in seconds) for when this audio data will no longer be accessible. | ||
""" | ||
|
||
id: Optional[str] = field(default=None, metadata={"serializable": True}) | ||
data: Optional[str] = field(default=None, metadata={"serializable": True}) | ||
transcript: Optional[str] = field(default=None, metadata={"serializable": True}) | ||
expires_at: Optional[int] = field(default=None, metadata={"serializable": True}) |
43 changes: 43 additions & 0 deletions
43
griptape/common/prompt_stack/contents/audio_message_content.py
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,43 @@ | ||
from __future__ import annotations | ||
|
||
import base64 | ||
from typing import TYPE_CHECKING | ||
|
||
from attrs import define, field | ||
|
||
from griptape.artifacts import AudioArtifact | ||
from griptape.common import ( | ||
AudioDeltaMessageContent, | ||
BaseDeltaMessageContent, | ||
BaseMessageContent, | ||
) | ||
|
||
if TYPE_CHECKING: | ||
from collections.abc import Sequence | ||
|
||
|
||
@define | ||
class AudioMessageContent(BaseMessageContent): | ||
artifact: AudioArtifact = field(metadata={"serializable": True}) | ||
|
||
@classmethod | ||
def from_deltas(cls, deltas: Sequence[BaseDeltaMessageContent]) -> AudioMessageContent: | ||
audio_deltas = [delta for delta in deltas if isinstance(delta, AudioDeltaMessageContent)] | ||
audio_data = [delta.data for delta in audio_deltas if delta.data is not None] | ||
transcript_data = [delta.transcript for delta in audio_deltas if delta.transcript is not None] | ||
expires_at = next(delta.expires_at for delta in audio_deltas if delta.expires_at is not None) | ||
audio_id = next(delta.id for delta in audio_deltas if delta.id is not None) | ||
|
||
audio_transcript = "".join(data for data in transcript_data) | ||
|
||
artifact = AudioArtifact( | ||
value=b"".join(base64.b64decode(data) for data in audio_data), | ||
format="wav", | ||
meta={ | ||
"audio_id": audio_id, | ||
"expires_at": expires_at, | ||
"transcript": audio_transcript, | ||
}, | ||
) | ||
|
||
return cls(artifact=artifact) |
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,17 @@ | ||
from attrs import define, field | ||
|
||
from griptape.events.base_chunk_event import BaseChunkEvent | ||
|
||
|
||
@define | ||
class AudioChunkEvent(BaseChunkEvent): | ||
"""Stores a chunk of audio data. | ||
Attributes: | ||
data: Base64 encoded audio data. | ||
""" | ||
|
||
data: str = field(kw_only=True, metadata={"serializable": True}) | ||
|
||
def __str__(self) -> str: | ||
return self.data |
Oops, something went wrong.