-
Notifications
You must be signed in to change notification settings - Fork 176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add AzureOpenAiTextToSpeechDriver
#1150
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 20 additions & 0 deletions
20
docs/griptape-framework/drivers/src/text_to_speech_drivers_3.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,20 @@ | ||
import os | ||
|
||
from griptape.drivers import AzureOpenAiTextToSpeechDriver | ||
from griptape.engines import TextToSpeechEngine | ||
from griptape.structures import Agent | ||
from griptape.tools.text_to_speech.tool import TextToSpeechTool | ||
|
||
driver = AzureOpenAiTextToSpeechDriver( | ||
api_key=os.environ["AZURE_OPENAI_API_KEY_4"], | ||
model="tts", | ||
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT_4"], | ||
) | ||
|
||
tool = TextToSpeechTool( | ||
engine=TextToSpeechEngine( | ||
text_to_speech_driver=driver, | ||
), | ||
) | ||
|
||
Agent(tools=[tool]).run("Generate audio from this text: 'Hello, world!'") |
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
51 changes: 51 additions & 0 deletions
51
griptape/drivers/text_to_speech/azure_openai_text_to_speech_driver.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,51 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Callable, Optional | ||
|
||
import openai | ||
from attrs import Factory, define, field | ||
|
||
from griptape.drivers import OpenAiTextToSpeechDriver | ||
|
||
|
||
@define | ||
class AzureOpenAiTextToSpeechDriver(OpenAiTextToSpeechDriver): | ||
"""Azure OpenAi Text to Speech Driver. | ||
|
||
Attributes: | ||
azure_deployment: An optional Azure OpenAi deployment id. Defaults to the model name. | ||
azure_endpoint: An Azure OpenAi endpoint. | ||
azure_ad_token: An optional Azure Active Directory token. | ||
azure_ad_token_provider: An optional Azure Active Directory token provider. | ||
api_version: An Azure OpenAi API version. | ||
client: An `openai.AzureOpenAI` client. | ||
""" | ||
|
||
model: str = field(default="tts", kw_only=True, metadata={"serializable": True}) | ||
azure_deployment: str = field( | ||
kw_only=True, | ||
default=Factory(lambda self: self.model, takes_self=True), | ||
metadata={"serializable": True}, | ||
) | ||
azure_endpoint: str = field(kw_only=True, metadata={"serializable": True}) | ||
azure_ad_token: Optional[str] = field(kw_only=True, default=None, metadata={"serializable": False}) | ||
azure_ad_token_provider: Optional[Callable[[], str]] = field( | ||
kw_only=True, | ||
default=None, | ||
metadata={"serializable": False}, | ||
) | ||
api_version: str = field(default="2024-07-01-preview", kw_only=True, metadata={"serializable": True}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need the latest preview API version, not GA yet. |
||
client: openai.AzureOpenAI = field( | ||
default=Factory( | ||
lambda self: openai.AzureOpenAI( | ||
organization=self.organization, | ||
api_key=self.api_key, | ||
api_version=self.api_version, | ||
azure_endpoint=self.azure_endpoint, | ||
azure_deployment=self.azure_deployment, | ||
azure_ad_token=self.azure_ad_token, | ||
azure_ad_token_provider=self.azure_ad_token_provider, | ||
), | ||
takes_self=True, | ||
), | ||
) |
33 changes: 33 additions & 0 deletions
33
tests/unit/drivers/text_to_speech/test_azure_openai_text_to_speech_driver.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,33 @@ | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
|
||
from griptape.drivers import AzureOpenAiTextToSpeechDriver | ||
|
||
|
||
class TestAzureOpenAiTextToSpeechDriver: | ||
@pytest.fixture() | ||
def mock_speech_create(self, mocker): | ||
mock_speech_create = mocker.patch("openai.AzureOpenAI").return_value.audio.speech.create | ||
mock_function = Mock(arguments='{"foo": "bar"}', id="mock-id") | ||
mock_function.name = "MockTool_test" | ||
mock_speech_create.return_value = Mock( | ||
content=b"speech", | ||
) | ||
|
||
return mock_speech_create | ||
|
||
def test_init(self): | ||
assert AzureOpenAiTextToSpeechDriver(azure_endpoint="foobar", azure_deployment="foobar") | ||
assert AzureOpenAiTextToSpeechDriver(azure_endpoint="foobar").azure_deployment == "tts" | ||
|
||
def test_run_text_to_audio(self, mock_speech_create): | ||
driver = AzureOpenAiTextToSpeechDriver(azure_endpoint="foobar") | ||
output = driver.run_text_to_audio(["foo", "bar"]) | ||
mock_speech_create.assert_called_once_with( | ||
input="foo. bar", | ||
model=driver.model, | ||
response_format=driver.format, | ||
voice=driver.voice, | ||
) | ||
assert output.value == b"speech" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
default model is
tts
nottts-1