|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT license. |
| 3 | + |
| 4 | +from dataclasses import dataclass |
| 5 | +from typing import Any, Literal |
| 6 | + |
| 7 | +# Voices supported by OpenAI Chat Completions API audio output. |
| 8 | +# OpenAI SDK: openai/types/chat/chat_completion_audio_param.py voice field |
| 9 | +# SDK Literal includes: alloy, ash, ballad, coral, echo, sage, shimmer, verse, marin, cedar |
| 10 | +# SDK docstring also lists: fable, nova, onyx (we include these for completeness) |
| 11 | +# Note: SDK uses Union[str, Literal[...]] so any string is accepted by the API. |
| 12 | +ChatAudioVoice = Literal[ |
| 13 | + "alloy", "ash", "ballad", "coral", "echo", "fable", "nova", "onyx", "sage", "shimmer", "verse", "marin", "cedar" |
| 14 | +] |
| 15 | + |
| 16 | +# Audio output formats supported by OpenAI Chat Completions API. |
| 17 | +# OpenAI SDK: openai/types/chat/chat_completion_audio_param.py format field |
| 18 | +# defines format: Required[Literal["wav", "aac", "mp3", "flac", "opus", "pcm16"]] |
| 19 | +ChatAudioFormat = Literal["wav", "aac", "mp3", "flac", "opus", "pcm16"] |
| 20 | + |
| 21 | + |
| 22 | +@dataclass |
| 23 | +class OpenAIChatAudioConfig: |
| 24 | + """ |
| 25 | + Configuration for audio output from OpenAI Chat Completions API. |
| 26 | +
|
| 27 | + When provided to OpenAIChatTarget, this enables audio output from models |
| 28 | + that support it (e.g., gpt-4o-audio-preview). |
| 29 | +
|
| 30 | + Note: This is specific to the Chat Completions API. The Responses API does not |
| 31 | + support audio input or output. For real-time audio, use RealtimeTarget instead. |
| 32 | + """ |
| 33 | + |
| 34 | + # The voice to use for audio output. Supported voices are: |
| 35 | + voice: ChatAudioVoice |
| 36 | + |
| 37 | + # The audio format for the response. Supported formats are: |
| 38 | + audio_format: ChatAudioFormat = "wav" |
| 39 | + |
| 40 | + # If True, historical user messages that contain both audio and text will only send |
| 41 | + # the text (transcript) to reduce bandwidth and token usage. The current (last) user |
| 42 | + # message will still include audio. Defaults to True. |
| 43 | + prefer_transcript_for_history: bool = True |
| 44 | + |
| 45 | + def to_extra_body_parameters(self) -> dict[str, Any]: |
| 46 | + """ |
| 47 | + Convert the config to extra_body_parameters format for OpenAI API. |
| 48 | +
|
| 49 | + Returns: |
| 50 | + dict: Parameters to include in the request body for audio output. |
| 51 | + """ |
| 52 | + return { |
| 53 | + "modalities": ["text", "audio"], |
| 54 | + "audio": {"voice": self.voice, "format": self.audio_format}, |
| 55 | + } |
0 commit comments