sessions.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. from __future__ import annotations
  3. from typing import List, Union, Iterable
  4. from typing_extensions import Literal
  5. import httpx
  6. from .... import _legacy_response
  7. from ...._types import NOT_GIVEN, Body, Query, Headers, NotGiven
  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 to_streamed_response_wrapper, async_to_streamed_response_wrapper
  12. from ...._base_client import make_request_options
  13. from ....types.beta.realtime import session_create_params
  14. from ....types.beta.realtime.session_create_response import SessionCreateResponse
  15. __all__ = ["Sessions", "AsyncSessions"]
  16. class Sessions(SyncAPIResource):
  17. @cached_property
  18. def with_raw_response(self) -> SessionsWithRawResponse:
  19. """
  20. This property can be used as a prefix for any HTTP method call to return
  21. the raw response object instead of the parsed content.
  22. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  23. """
  24. return SessionsWithRawResponse(self)
  25. @cached_property
  26. def with_streaming_response(self) -> SessionsWithStreamingResponse:
  27. """
  28. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  29. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  30. """
  31. return SessionsWithStreamingResponse(self)
  32. def create(
  33. self,
  34. *,
  35. client_secret: session_create_params.ClientSecret | NotGiven = NOT_GIVEN,
  36. input_audio_format: Literal["pcm16", "g711_ulaw", "g711_alaw"] | NotGiven = NOT_GIVEN,
  37. input_audio_noise_reduction: session_create_params.InputAudioNoiseReduction | NotGiven = NOT_GIVEN,
  38. input_audio_transcription: session_create_params.InputAudioTranscription | NotGiven = NOT_GIVEN,
  39. instructions: str | NotGiven = NOT_GIVEN,
  40. max_response_output_tokens: Union[int, Literal["inf"]] | NotGiven = NOT_GIVEN,
  41. modalities: List[Literal["text", "audio"]] | NotGiven = NOT_GIVEN,
  42. model: Literal[
  43. "gpt-realtime",
  44. "gpt-realtime-2025-08-28",
  45. "gpt-4o-realtime-preview",
  46. "gpt-4o-realtime-preview-2024-10-01",
  47. "gpt-4o-realtime-preview-2024-12-17",
  48. "gpt-4o-realtime-preview-2025-06-03",
  49. "gpt-4o-mini-realtime-preview",
  50. "gpt-4o-mini-realtime-preview-2024-12-17",
  51. ]
  52. | NotGiven = NOT_GIVEN,
  53. output_audio_format: Literal["pcm16", "g711_ulaw", "g711_alaw"] | NotGiven = NOT_GIVEN,
  54. speed: float | NotGiven = NOT_GIVEN,
  55. temperature: float | NotGiven = NOT_GIVEN,
  56. tool_choice: str | NotGiven = NOT_GIVEN,
  57. tools: Iterable[session_create_params.Tool] | NotGiven = NOT_GIVEN,
  58. tracing: session_create_params.Tracing | NotGiven = NOT_GIVEN,
  59. turn_detection: session_create_params.TurnDetection | NotGiven = NOT_GIVEN,
  60. voice: Union[str, Literal["alloy", "ash", "ballad", "coral", "echo", "sage", "shimmer", "verse"]]
  61. | NotGiven = NOT_GIVEN,
  62. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  63. # The extra values given here take precedence over values defined on the client or passed to this method.
  64. extra_headers: Headers | None = None,
  65. extra_query: Query | None = None,
  66. extra_body: Body | None = None,
  67. timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
  68. ) -> SessionCreateResponse:
  69. """
  70. Create an ephemeral API token for use in client-side applications with the
  71. Realtime API. Can be configured with the same session parameters as the
  72. `session.update` client event.
  73. It responds with a session object, plus a `client_secret` key which contains a
  74. usable ephemeral API token that can be used to authenticate browser clients for
  75. the Realtime API.
  76. Args:
  77. client_secret: Configuration options for the generated client secret.
  78. input_audio_format: The format of input audio. Options are `pcm16`, `g711_ulaw`, or `g711_alaw`. For
  79. `pcm16`, input audio must be 16-bit PCM at a 24kHz sample rate, single channel
  80. (mono), and little-endian byte order.
  81. input_audio_noise_reduction: Configuration for input audio noise reduction. This can be set to `null` to turn
  82. off. Noise reduction filters audio added to the input audio buffer before it is
  83. sent to VAD and the model. Filtering the audio can improve VAD and turn
  84. detection accuracy (reducing false positives) and model performance by improving
  85. perception of the input audio.
  86. input_audio_transcription: Configuration for input audio transcription, defaults to off and can be set to
  87. `null` to turn off once on. Input audio transcription is not native to the
  88. model, since the model consumes audio directly. Transcription runs
  89. asynchronously through
  90. [the /audio/transcriptions endpoint](https://platform.openai.com/docs/api-reference/audio/createTranscription)
  91. and should be treated as guidance of input audio content rather than precisely
  92. what the model heard. The client can optionally set the language and prompt for
  93. transcription, these offer additional guidance to the transcription service.
  94. instructions: The default system instructions (i.e. system message) prepended to model calls.
  95. This field allows the client to guide the model on desired responses. The model
  96. can be instructed on response content and format, (e.g. "be extremely succinct",
  97. "act friendly", "here are examples of good responses") and on audio behavior
  98. (e.g. "talk quickly", "inject emotion into your voice", "laugh frequently"). The
  99. instructions are not guaranteed to be followed by the model, but they provide
  100. guidance to the model on the desired behavior.
  101. Note that the server sets default instructions which will be used if this field
  102. is not set and are visible in the `session.created` event at the start of the
  103. session.
  104. max_response_output_tokens: Maximum number of output tokens for a single assistant response, inclusive of
  105. tool calls. Provide an integer between 1 and 4096 to limit output tokens, or
  106. `inf` for the maximum available tokens for a given model. Defaults to `inf`.
  107. modalities: The set of modalities the model can respond with. To disable audio, set this to
  108. ["text"].
  109. model: The Realtime model used for this session.
  110. output_audio_format: The format of output audio. Options are `pcm16`, `g711_ulaw`, or `g711_alaw`.
  111. For `pcm16`, output audio is sampled at a rate of 24kHz.
  112. speed: The speed of the model's spoken response. 1.0 is the default speed. 0.25 is the
  113. minimum speed. 1.5 is the maximum speed. This value can only be changed in
  114. between model turns, not while a response is in progress.
  115. temperature: Sampling temperature for the model, limited to [0.6, 1.2]. For audio models a
  116. temperature of 0.8 is highly recommended for best performance.
  117. tool_choice: How the model chooses tools. Options are `auto`, `none`, `required`, or specify
  118. a function.
  119. tools: Tools (functions) available to the model.
  120. tracing: Configuration options for tracing. Set to null to disable tracing. Once tracing
  121. is enabled for a session, the configuration cannot be modified.
  122. `auto` will create a trace for the session with default values for the workflow
  123. name, group id, and metadata.
  124. turn_detection: Configuration for turn detection, ether Server VAD or Semantic VAD. This can be
  125. set to `null` to turn off, in which case the client must manually trigger model
  126. response. Server VAD means that the model will detect the start and end of
  127. speech based on audio volume and respond at the end of user speech. Semantic VAD
  128. is more advanced and uses a turn detection model (in conjunction with VAD) to
  129. semantically estimate whether the user has finished speaking, then dynamically
  130. sets a timeout based on this probability. For example, if user audio trails off
  131. with "uhhm", the model will score a low probability of turn end and wait longer
  132. for the user to continue speaking. This can be useful for more natural
  133. conversations, but may have a higher latency.
  134. voice: The voice the model uses to respond. Voice cannot be changed during the session
  135. once the model has responded with audio at least once. Current voice options are
  136. `alloy`, `ash`, `ballad`, `coral`, `echo`, `sage`, `shimmer`, and `verse`.
  137. extra_headers: Send extra headers
  138. extra_query: Add additional query parameters to the request
  139. extra_body: Add additional JSON properties to the request
  140. timeout: Override the client-level default timeout for this request, in seconds
  141. """
  142. extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
  143. return self._post(
  144. "/realtime/sessions",
  145. body=maybe_transform(
  146. {
  147. "client_secret": client_secret,
  148. "input_audio_format": input_audio_format,
  149. "input_audio_noise_reduction": input_audio_noise_reduction,
  150. "input_audio_transcription": input_audio_transcription,
  151. "instructions": instructions,
  152. "max_response_output_tokens": max_response_output_tokens,
  153. "modalities": modalities,
  154. "model": model,
  155. "output_audio_format": output_audio_format,
  156. "speed": speed,
  157. "temperature": temperature,
  158. "tool_choice": tool_choice,
  159. "tools": tools,
  160. "tracing": tracing,
  161. "turn_detection": turn_detection,
  162. "voice": voice,
  163. },
  164. session_create_params.SessionCreateParams,
  165. ),
  166. options=make_request_options(
  167. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  168. ),
  169. cast_to=SessionCreateResponse,
  170. )
  171. class AsyncSessions(AsyncAPIResource):
  172. @cached_property
  173. def with_raw_response(self) -> AsyncSessionsWithRawResponse:
  174. """
  175. This property can be used as a prefix for any HTTP method call to return
  176. the raw response object instead of the parsed content.
  177. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  178. """
  179. return AsyncSessionsWithRawResponse(self)
  180. @cached_property
  181. def with_streaming_response(self) -> AsyncSessionsWithStreamingResponse:
  182. """
  183. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  184. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  185. """
  186. return AsyncSessionsWithStreamingResponse(self)
  187. async def create(
  188. self,
  189. *,
  190. client_secret: session_create_params.ClientSecret | NotGiven = NOT_GIVEN,
  191. input_audio_format: Literal["pcm16", "g711_ulaw", "g711_alaw"] | NotGiven = NOT_GIVEN,
  192. input_audio_noise_reduction: session_create_params.InputAudioNoiseReduction | NotGiven = NOT_GIVEN,
  193. input_audio_transcription: session_create_params.InputAudioTranscription | NotGiven = NOT_GIVEN,
  194. instructions: str | NotGiven = NOT_GIVEN,
  195. max_response_output_tokens: Union[int, Literal["inf"]] | NotGiven = NOT_GIVEN,
  196. modalities: List[Literal["text", "audio"]] | NotGiven = NOT_GIVEN,
  197. model: Literal[
  198. "gpt-realtime",
  199. "gpt-realtime-2025-08-28",
  200. "gpt-4o-realtime-preview",
  201. "gpt-4o-realtime-preview-2024-10-01",
  202. "gpt-4o-realtime-preview-2024-12-17",
  203. "gpt-4o-realtime-preview-2025-06-03",
  204. "gpt-4o-mini-realtime-preview",
  205. "gpt-4o-mini-realtime-preview-2024-12-17",
  206. ]
  207. | NotGiven = NOT_GIVEN,
  208. output_audio_format: Literal["pcm16", "g711_ulaw", "g711_alaw"] | NotGiven = NOT_GIVEN,
  209. speed: float | NotGiven = NOT_GIVEN,
  210. temperature: float | NotGiven = NOT_GIVEN,
  211. tool_choice: str | NotGiven = NOT_GIVEN,
  212. tools: Iterable[session_create_params.Tool] | NotGiven = NOT_GIVEN,
  213. tracing: session_create_params.Tracing | NotGiven = NOT_GIVEN,
  214. turn_detection: session_create_params.TurnDetection | NotGiven = NOT_GIVEN,
  215. voice: Union[str, Literal["alloy", "ash", "ballad", "coral", "echo", "sage", "shimmer", "verse"]]
  216. | NotGiven = NOT_GIVEN,
  217. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  218. # The extra values given here take precedence over values defined on the client or passed to this method.
  219. extra_headers: Headers | None = None,
  220. extra_query: Query | None = None,
  221. extra_body: Body | None = None,
  222. timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
  223. ) -> SessionCreateResponse:
  224. """
  225. Create an ephemeral API token for use in client-side applications with the
  226. Realtime API. Can be configured with the same session parameters as the
  227. `session.update` client event.
  228. It responds with a session object, plus a `client_secret` key which contains a
  229. usable ephemeral API token that can be used to authenticate browser clients for
  230. the Realtime API.
  231. Args:
  232. client_secret: Configuration options for the generated client secret.
  233. input_audio_format: The format of input audio. Options are `pcm16`, `g711_ulaw`, or `g711_alaw`. For
  234. `pcm16`, input audio must be 16-bit PCM at a 24kHz sample rate, single channel
  235. (mono), and little-endian byte order.
  236. input_audio_noise_reduction: Configuration for input audio noise reduction. This can be set to `null` to turn
  237. off. Noise reduction filters audio added to the input audio buffer before it is
  238. sent to VAD and the model. Filtering the audio can improve VAD and turn
  239. detection accuracy (reducing false positives) and model performance by improving
  240. perception of the input audio.
  241. input_audio_transcription: Configuration for input audio transcription, defaults to off and can be set to
  242. `null` to turn off once on. Input audio transcription is not native to the
  243. model, since the model consumes audio directly. Transcription runs
  244. asynchronously through
  245. [the /audio/transcriptions endpoint](https://platform.openai.com/docs/api-reference/audio/createTranscription)
  246. and should be treated as guidance of input audio content rather than precisely
  247. what the model heard. The client can optionally set the language and prompt for
  248. transcription, these offer additional guidance to the transcription service.
  249. instructions: The default system instructions (i.e. system message) prepended to model calls.
  250. This field allows the client to guide the model on desired responses. The model
  251. can be instructed on response content and format, (e.g. "be extremely succinct",
  252. "act friendly", "here are examples of good responses") and on audio behavior
  253. (e.g. "talk quickly", "inject emotion into your voice", "laugh frequently"). The
  254. instructions are not guaranteed to be followed by the model, but they provide
  255. guidance to the model on the desired behavior.
  256. Note that the server sets default instructions which will be used if this field
  257. is not set and are visible in the `session.created` event at the start of the
  258. session.
  259. max_response_output_tokens: Maximum number of output tokens for a single assistant response, inclusive of
  260. tool calls. Provide an integer between 1 and 4096 to limit output tokens, or
  261. `inf` for the maximum available tokens for a given model. Defaults to `inf`.
  262. modalities: The set of modalities the model can respond with. To disable audio, set this to
  263. ["text"].
  264. model: The Realtime model used for this session.
  265. output_audio_format: The format of output audio. Options are `pcm16`, `g711_ulaw`, or `g711_alaw`.
  266. For `pcm16`, output audio is sampled at a rate of 24kHz.
  267. speed: The speed of the model's spoken response. 1.0 is the default speed. 0.25 is the
  268. minimum speed. 1.5 is the maximum speed. This value can only be changed in
  269. between model turns, not while a response is in progress.
  270. temperature: Sampling temperature for the model, limited to [0.6, 1.2]. For audio models a
  271. temperature of 0.8 is highly recommended for best performance.
  272. tool_choice: How the model chooses tools. Options are `auto`, `none`, `required`, or specify
  273. a function.
  274. tools: Tools (functions) available to the model.
  275. tracing: Configuration options for tracing. Set to null to disable tracing. Once tracing
  276. is enabled for a session, the configuration cannot be modified.
  277. `auto` will create a trace for the session with default values for the workflow
  278. name, group id, and metadata.
  279. turn_detection: Configuration for turn detection, ether Server VAD or Semantic VAD. This can be
  280. set to `null` to turn off, in which case the client must manually trigger model
  281. response. Server VAD means that the model will detect the start and end of
  282. speech based on audio volume and respond at the end of user speech. Semantic VAD
  283. is more advanced and uses a turn detection model (in conjunction with VAD) to
  284. semantically estimate whether the user has finished speaking, then dynamically
  285. sets a timeout based on this probability. For example, if user audio trails off
  286. with "uhhm", the model will score a low probability of turn end and wait longer
  287. for the user to continue speaking. This can be useful for more natural
  288. conversations, but may have a higher latency.
  289. voice: The voice the model uses to respond. Voice cannot be changed during the session
  290. once the model has responded with audio at least once. Current voice options are
  291. `alloy`, `ash`, `ballad`, `coral`, `echo`, `sage`, `shimmer`, and `verse`.
  292. extra_headers: Send extra headers
  293. extra_query: Add additional query parameters to the request
  294. extra_body: Add additional JSON properties to the request
  295. timeout: Override the client-level default timeout for this request, in seconds
  296. """
  297. extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
  298. return await self._post(
  299. "/realtime/sessions",
  300. body=await async_maybe_transform(
  301. {
  302. "client_secret": client_secret,
  303. "input_audio_format": input_audio_format,
  304. "input_audio_noise_reduction": input_audio_noise_reduction,
  305. "input_audio_transcription": input_audio_transcription,
  306. "instructions": instructions,
  307. "max_response_output_tokens": max_response_output_tokens,
  308. "modalities": modalities,
  309. "model": model,
  310. "output_audio_format": output_audio_format,
  311. "speed": speed,
  312. "temperature": temperature,
  313. "tool_choice": tool_choice,
  314. "tools": tools,
  315. "tracing": tracing,
  316. "turn_detection": turn_detection,
  317. "voice": voice,
  318. },
  319. session_create_params.SessionCreateParams,
  320. ),
  321. options=make_request_options(
  322. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  323. ),
  324. cast_to=SessionCreateResponse,
  325. )
  326. class SessionsWithRawResponse:
  327. def __init__(self, sessions: Sessions) -> None:
  328. self._sessions = sessions
  329. self.create = _legacy_response.to_raw_response_wrapper(
  330. sessions.create,
  331. )
  332. class AsyncSessionsWithRawResponse:
  333. def __init__(self, sessions: AsyncSessions) -> None:
  334. self._sessions = sessions
  335. self.create = _legacy_response.async_to_raw_response_wrapper(
  336. sessions.create,
  337. )
  338. class SessionsWithStreamingResponse:
  339. def __init__(self, sessions: Sessions) -> None:
  340. self._sessions = sessions
  341. self.create = to_streamed_response_wrapper(
  342. sessions.create,
  343. )
  344. class AsyncSessionsWithStreamingResponse:
  345. def __init__(self, sessions: AsyncSessions) -> None:
  346. self._sessions = sessions
  347. self.create = async_to_streamed_response_wrapper(
  348. sessions.create,
  349. )