speech.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. from __future__ import annotations
  3. from typing import Union
  4. from typing_extensions import Literal
  5. import httpx
  6. from ... import _legacy_response
  7. from ..._types import Body, Omit, Query, Headers, NotGiven, omit, not_given
  8. from ..._utils import maybe_transform, async_maybe_transform
  9. from ..._compat import cached_property
  10. from ..._resource import SyncAPIResource, AsyncAPIResource
  11. from ..._response import (
  12. StreamedBinaryAPIResponse,
  13. AsyncStreamedBinaryAPIResponse,
  14. to_custom_streamed_response_wrapper,
  15. async_to_custom_streamed_response_wrapper,
  16. )
  17. from ...types.audio import speech_create_params
  18. from ..._base_client import make_request_options
  19. from ...types.audio.speech_model import SpeechModel
  20. __all__ = ["Speech", "AsyncSpeech"]
  21. class Speech(SyncAPIResource):
  22. @cached_property
  23. def with_raw_response(self) -> SpeechWithRawResponse:
  24. """
  25. This property can be used as a prefix for any HTTP method call to return
  26. the raw response object instead of the parsed content.
  27. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  28. """
  29. return SpeechWithRawResponse(self)
  30. @cached_property
  31. def with_streaming_response(self) -> SpeechWithStreamingResponse:
  32. """
  33. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  34. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  35. """
  36. return SpeechWithStreamingResponse(self)
  37. def create(
  38. self,
  39. *,
  40. input: str,
  41. model: Union[str, SpeechModel],
  42. voice: Union[
  43. str, Literal["alloy", "ash", "ballad", "coral", "echo", "sage", "shimmer", "verse", "marin", "cedar"]
  44. ],
  45. instructions: str | Omit = omit,
  46. response_format: Literal["mp3", "opus", "aac", "flac", "wav", "pcm"] | Omit = omit,
  47. speed: float | Omit = omit,
  48. stream_format: Literal["sse", "audio"] | Omit = omit,
  49. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  50. # The extra values given here take precedence over values defined on the client or passed to this method.
  51. extra_headers: Headers | None = None,
  52. extra_query: Query | None = None,
  53. extra_body: Body | None = None,
  54. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  55. ) -> _legacy_response.HttpxBinaryResponseContent:
  56. """
  57. Generates audio from the input text.
  58. Args:
  59. input: The text to generate audio for. The maximum length is 4096 characters.
  60. model:
  61. One of the available [TTS models](https://platform.openai.com/docs/models#tts):
  62. `tts-1`, `tts-1-hd` or `gpt-4o-mini-tts`.
  63. voice: The voice to use when generating the audio. Supported voices are `alloy`, `ash`,
  64. `ballad`, `coral`, `echo`, `fable`, `onyx`, `nova`, `sage`, `shimmer`, and
  65. `verse`. Previews of the voices are available in the
  66. [Text to speech guide](https://platform.openai.com/docs/guides/text-to-speech#voice-options).
  67. instructions: Control the voice of your generated audio with additional instructions. Does not
  68. work with `tts-1` or `tts-1-hd`.
  69. response_format: The format to audio in. Supported formats are `mp3`, `opus`, `aac`, `flac`,
  70. `wav`, and `pcm`.
  71. speed: The speed of the generated audio. Select a value from `0.25` to `4.0`. `1.0` is
  72. the default.
  73. stream_format: The format to stream the audio in. Supported formats are `sse` and `audio`.
  74. `sse` is not supported for `tts-1` or `tts-1-hd`.
  75. extra_headers: Send extra headers
  76. extra_query: Add additional query parameters to the request
  77. extra_body: Add additional JSON properties to the request
  78. timeout: Override the client-level default timeout for this request, in seconds
  79. """
  80. extra_headers = {"Accept": "application/octet-stream", **(extra_headers or {})}
  81. return self._post(
  82. "/audio/speech",
  83. body=maybe_transform(
  84. {
  85. "input": input,
  86. "model": model,
  87. "voice": voice,
  88. "instructions": instructions,
  89. "response_format": response_format,
  90. "speed": speed,
  91. "stream_format": stream_format,
  92. },
  93. speech_create_params.SpeechCreateParams,
  94. ),
  95. options=make_request_options(
  96. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  97. ),
  98. cast_to=_legacy_response.HttpxBinaryResponseContent,
  99. )
  100. class AsyncSpeech(AsyncAPIResource):
  101. @cached_property
  102. def with_raw_response(self) -> AsyncSpeechWithRawResponse:
  103. """
  104. This property can be used as a prefix for any HTTP method call to return
  105. the raw response object instead of the parsed content.
  106. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  107. """
  108. return AsyncSpeechWithRawResponse(self)
  109. @cached_property
  110. def with_streaming_response(self) -> AsyncSpeechWithStreamingResponse:
  111. """
  112. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  113. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  114. """
  115. return AsyncSpeechWithStreamingResponse(self)
  116. async def create(
  117. self,
  118. *,
  119. input: str,
  120. model: Union[str, SpeechModel],
  121. voice: Union[
  122. str, Literal["alloy", "ash", "ballad", "coral", "echo", "sage", "shimmer", "verse", "marin", "cedar"]
  123. ],
  124. instructions: str | Omit = omit,
  125. response_format: Literal["mp3", "opus", "aac", "flac", "wav", "pcm"] | Omit = omit,
  126. speed: float | Omit = omit,
  127. stream_format: Literal["sse", "audio"] | Omit = omit,
  128. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  129. # The extra values given here take precedence over values defined on the client or passed to this method.
  130. extra_headers: Headers | None = None,
  131. extra_query: Query | None = None,
  132. extra_body: Body | None = None,
  133. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  134. ) -> _legacy_response.HttpxBinaryResponseContent:
  135. """
  136. Generates audio from the input text.
  137. Args:
  138. input: The text to generate audio for. The maximum length is 4096 characters.
  139. model:
  140. One of the available [TTS models](https://platform.openai.com/docs/models#tts):
  141. `tts-1`, `tts-1-hd` or `gpt-4o-mini-tts`.
  142. voice: The voice to use when generating the audio. Supported voices are `alloy`, `ash`,
  143. `ballad`, `coral`, `echo`, `fable`, `onyx`, `nova`, `sage`, `shimmer`, and
  144. `verse`. Previews of the voices are available in the
  145. [Text to speech guide](https://platform.openai.com/docs/guides/text-to-speech#voice-options).
  146. instructions: Control the voice of your generated audio with additional instructions. Does not
  147. work with `tts-1` or `tts-1-hd`.
  148. response_format: The format to audio in. Supported formats are `mp3`, `opus`, `aac`, `flac`,
  149. `wav`, and `pcm`.
  150. speed: The speed of the generated audio. Select a value from `0.25` to `4.0`. `1.0` is
  151. the default.
  152. stream_format: The format to stream the audio in. Supported formats are `sse` and `audio`.
  153. `sse` is not supported for `tts-1` or `tts-1-hd`.
  154. extra_headers: Send extra headers
  155. extra_query: Add additional query parameters to the request
  156. extra_body: Add additional JSON properties to the request
  157. timeout: Override the client-level default timeout for this request, in seconds
  158. """
  159. extra_headers = {"Accept": "application/octet-stream", **(extra_headers or {})}
  160. return await self._post(
  161. "/audio/speech",
  162. body=await async_maybe_transform(
  163. {
  164. "input": input,
  165. "model": model,
  166. "voice": voice,
  167. "instructions": instructions,
  168. "response_format": response_format,
  169. "speed": speed,
  170. "stream_format": stream_format,
  171. },
  172. speech_create_params.SpeechCreateParams,
  173. ),
  174. options=make_request_options(
  175. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  176. ),
  177. cast_to=_legacy_response.HttpxBinaryResponseContent,
  178. )
  179. class SpeechWithRawResponse:
  180. def __init__(self, speech: Speech) -> None:
  181. self._speech = speech
  182. self.create = _legacy_response.to_raw_response_wrapper(
  183. speech.create,
  184. )
  185. class AsyncSpeechWithRawResponse:
  186. def __init__(self, speech: AsyncSpeech) -> None:
  187. self._speech = speech
  188. self.create = _legacy_response.async_to_raw_response_wrapper(
  189. speech.create,
  190. )
  191. class SpeechWithStreamingResponse:
  192. def __init__(self, speech: Speech) -> None:
  193. self._speech = speech
  194. self.create = to_custom_streamed_response_wrapper(
  195. speech.create,
  196. StreamedBinaryAPIResponse,
  197. )
  198. class AsyncSpeechWithStreamingResponse:
  199. def __init__(self, speech: AsyncSpeech) -> None:
  200. self._speech = speech
  201. self.create = async_to_custom_streamed_response_wrapper(
  202. speech.create,
  203. AsyncStreamedBinaryAPIResponse,
  204. )