|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +from collections.abc import Mapping |
| 4 | +from typing import Any, TypeVar |
| 5 | + |
| 6 | +from openai import AsyncAzureOpenAI |
| 7 | +from openai.lib.azure import AsyncAzureADTokenProvider |
| 8 | +from pydantic import ValidationError |
| 9 | + |
| 10 | +from semantic_kernel.connectors.ai.open_ai.services.azure_config_base import AzureOpenAIConfigBase |
| 11 | +from semantic_kernel.connectors.ai.open_ai.services.open_ai_model_types import OpenAIModelTypes |
| 12 | +from semantic_kernel.connectors.ai.open_ai.services.open_ai_text_to_audio_base import OpenAITextToAudioBase |
| 13 | +from semantic_kernel.connectors.ai.open_ai.settings.azure_open_ai_settings import AzureOpenAISettings |
| 14 | +from semantic_kernel.exceptions.service_exceptions import ServiceInitializationError |
| 15 | + |
| 16 | +T_ = TypeVar("T_", bound="AzureTextToAudio") |
| 17 | + |
| 18 | + |
| 19 | +class AzureTextToAudio(AzureOpenAIConfigBase, OpenAITextToAudioBase): |
| 20 | + """Azure text to audio service.""" |
| 21 | + |
| 22 | + def __init__( |
| 23 | + self, |
| 24 | + service_id: str | None = None, |
| 25 | + api_key: str | None = None, |
| 26 | + deployment_name: str | None = None, |
| 27 | + endpoint: str | None = None, |
| 28 | + base_url: str | None = None, |
| 29 | + api_version: str | None = "2024-10-01-preview", |
| 30 | + ad_token: str | None = None, |
| 31 | + ad_token_provider: AsyncAzureADTokenProvider | None = None, |
| 32 | + token_endpoint: str | None = None, |
| 33 | + default_headers: Mapping[str, str] | None = None, |
| 34 | + async_client: AsyncAzureOpenAI | None = None, |
| 35 | + env_file_path: str | None = None, |
| 36 | + env_file_encoding: str | None = None, |
| 37 | + ) -> None: |
| 38 | + """Initialize an AzureTextToAudio service. |
| 39 | +
|
| 40 | + Args: |
| 41 | + service_id: The service ID. (Optional) |
| 42 | + api_key: The optional api key. If provided, will override the value in the |
| 43 | + env vars or .env file. |
| 44 | + deployment_name: The optional deployment. If provided, will override the value |
| 45 | + (text_to_audio_deployment_name) in the env vars or .env file. |
| 46 | + endpoint: The optional deployment endpoint. If provided will override the value |
| 47 | + in the env vars or .env file. |
| 48 | + base_url: The optional deployment base_url. If provided will override the value |
| 49 | + in the env vars or .env file. |
| 50 | + api_version: The optional deployment api version. If provided will override the value |
| 51 | + in the env vars or .env file. Default is "2024-10-01-preview". |
| 52 | + ad_token: The Azure AD token for authentication. (Optional) |
| 53 | + ad_token_provider: Azure AD Token provider. (Optional) |
| 54 | + token_endpoint: The Azure AD token endpoint. (Optional) |
| 55 | + default_headers: The default headers mapping of string keys to |
| 56 | + string values for HTTP requests. (Optional) |
| 57 | + async_client: An existing client to use. (Optional) |
| 58 | + env_file_path: Use the environment settings file as a fallback to |
| 59 | + environment variables. (Optional) |
| 60 | + env_file_encoding: The encoding of the environment settings file. (Optional) |
| 61 | + """ |
| 62 | + try: |
| 63 | + azure_openai_settings = AzureOpenAISettings.create( |
| 64 | + env_file_path=env_file_path, |
| 65 | + env_file_encoding=env_file_encoding, |
| 66 | + api_key=api_key, |
| 67 | + text_to_audio_deployment_name=deployment_name, |
| 68 | + endpoint=endpoint, |
| 69 | + base_url=base_url, |
| 70 | + api_version=api_version, |
| 71 | + token_endpoint=token_endpoint, |
| 72 | + ) |
| 73 | + except ValidationError as exc: |
| 74 | + raise ServiceInitializationError(f"Invalid settings: {exc}") from exc |
| 75 | + if not azure_openai_settings.text_to_audio_deployment_name: |
| 76 | + raise ServiceInitializationError("The Azure OpenAI text to audio deployment name is required.") |
| 77 | + |
| 78 | + super().__init__( |
| 79 | + deployment_name=azure_openai_settings.text_to_audio_deployment_name, |
| 80 | + endpoint=azure_openai_settings.endpoint, |
| 81 | + base_url=azure_openai_settings.base_url, |
| 82 | + api_version=azure_openai_settings.api_version, |
| 83 | + service_id=service_id, |
| 84 | + api_key=azure_openai_settings.api_key.get_secret_value() if azure_openai_settings.api_key else None, |
| 85 | + ad_token=ad_token, |
| 86 | + ad_token_provider=ad_token_provider, |
| 87 | + token_endpoint=azure_openai_settings.token_endpoint, |
| 88 | + default_headers=default_headers, |
| 89 | + ai_model_type=OpenAIModelTypes.TEXT_TO_AUDIO, |
| 90 | + client=async_client, |
| 91 | + ) |
| 92 | + |
| 93 | + @classmethod |
| 94 | + def from_dict(cls: type[T_], settings: dict[str, Any]) -> T_: |
| 95 | + """Initialize an Azure OpenAI service from a dictionary of settings. |
| 96 | +
|
| 97 | + Args: |
| 98 | + settings: A dictionary of settings for the service. |
| 99 | + should contain keys: deployment_name, endpoint, api_key |
| 100 | + and optionally: api_version, ad_auth |
| 101 | + """ |
| 102 | + return cls( |
| 103 | + service_id=settings.get("service_id"), |
| 104 | + api_key=settings.get("api_key"), |
| 105 | + deployment_name=settings.get("deployment_name"), |
| 106 | + endpoint=settings.get("endpoint"), |
| 107 | + base_url=settings.get("base_url"), |
| 108 | + api_version=settings.get("api_version"), |
| 109 | + ad_token=settings.get("ad_token"), |
| 110 | + ad_token_provider=settings.get("ad_token_provider"), |
| 111 | + default_headers=settings.get("default_headers"), |
| 112 | + env_file_path=settings.get("env_file_path"), |
| 113 | + ) |
0 commit comments