openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/realtime/test_client_secrets.py
208lines · modeblame
3d3d16abstainless-app[bot]9 months ago | 1 | # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | | |
| 3 | from __future__ import annotations | |
| 4 | | |
| 5 | import os | |
| 6 | from typing import Any, cast | |
| 7 | | |
| 8 | import pytest | |
| 9 | | |
| 10 | from openai import OpenAI, AsyncOpenAI | |
| 11 | from tests.utils import assert_matches_type | |
| 12 | from openai.types.realtime import ClientSecretCreateResponse | |
| 13 | | |
| 14 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") | |
| 15 | | |
| 16 | | |
| 17 | class TestClientSecrets: | |
| 18 | parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) | |
| 19 | | |
| 20 | @parametrize | |
| 21 | def test_method_create(self, client: OpenAI) -> None: | |
| 22 | client_secret = client.realtime.client_secrets.create() | |
| 23 | assert_matches_type(ClientSecretCreateResponse, client_secret, path=["response"]) | |
| 24 | | |
| 25 | @parametrize | |
| 26 | def test_method_create_with_all_params(self, client: OpenAI) -> None: | |
| 27 | client_secret = client.realtime.client_secrets.create( | |
| 28 | expires_after={ | |
| 29 | "anchor": "created_at", | |
| 30 | "seconds": 10, | |
| 31 | }, | |
| 32 | session={ | |
| 33 | "model": "string", | |
| 34 | "type": "realtime", | |
| 35 | "audio": { | |
| 36 | "input": { | |
| 37 | "format": "pcm16", | |
| 38 | "noise_reduction": {"type": "near_field"}, | |
| 39 | "transcription": { | |
| 40 | "language": "language", | |
| 41 | "model": "whisper-1", | |
| 42 | "prompt": "prompt", | |
| 43 | }, | |
| 44 | "turn_detection": { | |
| 45 | "create_response": True, | |
| 46 | "eagerness": "low", | |
| 47 | "idle_timeout_ms": 0, | |
| 48 | "interrupt_response": True, | |
| 49 | "prefix_padding_ms": 0, | |
| 50 | "silence_duration_ms": 0, | |
| 51 | "threshold": 0, | |
| 52 | "type": "server_vad", | |
| 53 | }, | |
| 54 | }, | |
| 55 | "output": { | |
| 56 | "format": "pcm16", | |
| 57 | "speed": 0.25, | |
| 58 | "voice": "ash", | |
| 59 | }, | |
| 60 | }, | |
| 61 | "client_secret": { | |
| 62 | "expires_after": { | |
| 63 | "anchor": "created_at", | |
| 64 | "seconds": 0, | |
| 65 | } | |
| 66 | }, | |
| 67 | "include": ["item.input_audio_transcription.logprobs"], | |
| 68 | "instructions": "instructions", | |
| 69 | "max_output_tokens": 0, | |
| 70 | "output_modalities": ["text"], | |
| 71 | "prompt": { | |
| 72 | "id": "id", | |
| 73 | "variables": {"foo": "string"}, | |
| 74 | "version": "version", | |
| 75 | }, | |
| 76 | "temperature": 0, | |
| 77 | "tool_choice": "none", | |
| 78 | "tools": [ | |
| 79 | { | |
| 80 | "description": "description", | |
| 81 | "name": "name", | |
| 82 | "parameters": {}, | |
| 83 | "type": "function", | |
| 84 | } | |
| 85 | ], | |
| 86 | "tracing": "auto", | |
| 87 | "truncation": "auto", | |
| 88 | }, | |
| 89 | ) | |
| 90 | assert_matches_type(ClientSecretCreateResponse, client_secret, path=["response"]) | |
| 91 | | |
| 92 | @parametrize | |
| 93 | def test_raw_response_create(self, client: OpenAI) -> None: | |
| 94 | response = client.realtime.client_secrets.with_raw_response.create() | |
| 95 | | |
| 96 | assert response.is_closed is True | |
| 97 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 98 | client_secret = response.parse() | |
| 99 | assert_matches_type(ClientSecretCreateResponse, client_secret, path=["response"]) | |
| 100 | | |
| 101 | @parametrize | |
| 102 | def test_streaming_response_create(self, client: OpenAI) -> None: | |
| 103 | with client.realtime.client_secrets.with_streaming_response.create() as response: | |
| 104 | assert not response.is_closed | |
| 105 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 106 | | |
| 107 | client_secret = response.parse() | |
| 108 | assert_matches_type(ClientSecretCreateResponse, client_secret, path=["response"]) | |
| 109 | | |
| 110 | assert cast(Any, response.is_closed) is True | |
| 111 | | |
| 112 | | |
| 113 | class TestAsyncClientSecrets: | |
| 114 | parametrize = pytest.mark.parametrize( | |
| 115 | "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"] | |
| 116 | ) | |
| 117 | | |
| 118 | @parametrize | |
| 119 | async def test_method_create(self, async_client: AsyncOpenAI) -> None: | |
| 120 | client_secret = await async_client.realtime.client_secrets.create() | |
| 121 | assert_matches_type(ClientSecretCreateResponse, client_secret, path=["response"]) | |
| 122 | | |
| 123 | @parametrize | |
| 124 | async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None: | |
| 125 | client_secret = await async_client.realtime.client_secrets.create( | |
| 126 | expires_after={ | |
| 127 | "anchor": "created_at", | |
| 128 | "seconds": 10, | |
| 129 | }, | |
| 130 | session={ | |
| 131 | "model": "string", | |
| 132 | "type": "realtime", | |
| 133 | "audio": { | |
| 134 | "input": { | |
| 135 | "format": "pcm16", | |
| 136 | "noise_reduction": {"type": "near_field"}, | |
| 137 | "transcription": { | |
| 138 | "language": "language", | |
| 139 | "model": "whisper-1", | |
| 140 | "prompt": "prompt", | |
| 141 | }, | |
| 142 | "turn_detection": { | |
| 143 | "create_response": True, | |
| 144 | "eagerness": "low", | |
| 145 | "idle_timeout_ms": 0, | |
| 146 | "interrupt_response": True, | |
| 147 | "prefix_padding_ms": 0, | |
| 148 | "silence_duration_ms": 0, | |
| 149 | "threshold": 0, | |
| 150 | "type": "server_vad", | |
| 151 | }, | |
| 152 | }, | |
| 153 | "output": { | |
| 154 | "format": "pcm16", | |
| 155 | "speed": 0.25, | |
| 156 | "voice": "ash", | |
| 157 | }, | |
| 158 | }, | |
| 159 | "client_secret": { | |
| 160 | "expires_after": { | |
| 161 | "anchor": "created_at", | |
| 162 | "seconds": 0, | |
| 163 | } | |
| 164 | }, | |
| 165 | "include": ["item.input_audio_transcription.logprobs"], | |
| 166 | "instructions": "instructions", | |
| 167 | "max_output_tokens": 0, | |
| 168 | "output_modalities": ["text"], | |
| 169 | "prompt": { | |
| 170 | "id": "id", | |
| 171 | "variables": {"foo": "string"}, | |
| 172 | "version": "version", | |
| 173 | }, | |
| 174 | "temperature": 0, | |
| 175 | "tool_choice": "none", | |
| 176 | "tools": [ | |
| 177 | { | |
| 178 | "description": "description", | |
| 179 | "name": "name", | |
| 180 | "parameters": {}, | |
| 181 | "type": "function", | |
| 182 | } | |
| 183 | ], | |
| 184 | "tracing": "auto", | |
| 185 | "truncation": "auto", | |
| 186 | }, | |
| 187 | ) | |
| 188 | assert_matches_type(ClientSecretCreateResponse, client_secret, path=["response"]) | |
| 189 | | |
| 190 | @parametrize | |
| 191 | async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None: | |
| 192 | response = await async_client.realtime.client_secrets.with_raw_response.create() | |
| 193 | | |
| 194 | assert response.is_closed is True | |
| 195 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 196 | client_secret = response.parse() | |
| 197 | assert_matches_type(ClientSecretCreateResponse, client_secret, path=["response"]) | |
| 198 | | |
| 199 | @parametrize | |
| 200 | async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None: | |
| 201 | async with async_client.realtime.client_secrets.with_streaming_response.create() as response: | |
| 202 | assert not response.is_closed | |
| 203 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 204 | | |
| 205 | client_secret = await response.parse() | |
| 206 | assert_matches_type(ClientSecretCreateResponse, client_secret, path=["response"]) | |
| 207 | | |
| 208 | assert cast(Any, response.is_closed) is True |