openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/beta/test_assistants.py
404lines · modecode
| 1 | # File generated from our OpenAPI spec by Stainless. |
| 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._client import OpenAI, AsyncOpenAI |
| 13 | from openai.pagination import SyncCursorPage, AsyncCursorPage |
| 14 | from openai.types.beta import ( |
| 15 | Assistant, |
| 16 | AssistantDeleted, |
| 17 | ) |
| 18 | |
| 19 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 20 | api_key = "My API Key" |
| 21 | |
| 22 | |
| 23 | class TestAssistants: |
| 24 | strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 25 | loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 26 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 27 | |
| 28 | @parametrize |
| 29 | def test_method_create(self, client: OpenAI) -> None: |
| 30 | assistant = client.beta.assistants.create( |
| 31 | model="string", |
| 32 | ) |
| 33 | assert_matches_type(Assistant, assistant, path=["response"]) |
| 34 | |
| 35 | @parametrize |
| 36 | def test_method_create_with_all_params(self, client: OpenAI) -> None: |
| 37 | assistant = client.beta.assistants.create( |
| 38 | model="string", |
| 39 | description="string", |
| 40 | file_ids=["string", "string", "string"], |
| 41 | instructions="string", |
| 42 | metadata={}, |
| 43 | name="string", |
| 44 | tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}], |
| 45 | ) |
| 46 | assert_matches_type(Assistant, assistant, path=["response"]) |
| 47 | |
| 48 | @parametrize |
| 49 | def test_raw_response_create(self, client: OpenAI) -> None: |
| 50 | response = client.beta.assistants.with_raw_response.create( |
| 51 | model="string", |
| 52 | ) |
| 53 | |
| 54 | assert response.is_closed is True |
| 55 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 56 | assistant = response.parse() |
| 57 | assert_matches_type(Assistant, assistant, path=["response"]) |
| 58 | |
| 59 | @parametrize |
| 60 | def test_streaming_response_create(self, client: OpenAI) -> None: |
| 61 | with client.beta.assistants.with_streaming_response.create( |
| 62 | model="string", |
| 63 | ) as response: |
| 64 | assert not response.is_closed |
| 65 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 66 | |
| 67 | assistant = response.parse() |
| 68 | assert_matches_type(Assistant, assistant, path=["response"]) |
| 69 | |
| 70 | assert cast(Any, response.is_closed) is True |
| 71 | |
| 72 | @parametrize |
| 73 | def test_method_retrieve(self, client: OpenAI) -> None: |
| 74 | assistant = client.beta.assistants.retrieve( |
| 75 | "string", |
| 76 | ) |
| 77 | assert_matches_type(Assistant, assistant, path=["response"]) |
| 78 | |
| 79 | @parametrize |
| 80 | def test_raw_response_retrieve(self, client: OpenAI) -> None: |
| 81 | response = client.beta.assistants.with_raw_response.retrieve( |
| 82 | "string", |
| 83 | ) |
| 84 | |
| 85 | assert response.is_closed is True |
| 86 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 87 | assistant = response.parse() |
| 88 | assert_matches_type(Assistant, assistant, path=["response"]) |
| 89 | |
| 90 | @parametrize |
| 91 | def test_streaming_response_retrieve(self, client: OpenAI) -> None: |
| 92 | with client.beta.assistants.with_streaming_response.retrieve( |
| 93 | "string", |
| 94 | ) as response: |
| 95 | assert not response.is_closed |
| 96 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 97 | |
| 98 | assistant = response.parse() |
| 99 | assert_matches_type(Assistant, assistant, path=["response"]) |
| 100 | |
| 101 | assert cast(Any, response.is_closed) is True |
| 102 | |
| 103 | @parametrize |
| 104 | def test_method_update(self, client: OpenAI) -> None: |
| 105 | assistant = client.beta.assistants.update( |
| 106 | "string", |
| 107 | ) |
| 108 | assert_matches_type(Assistant, assistant, path=["response"]) |
| 109 | |
| 110 | @parametrize |
| 111 | def test_method_update_with_all_params(self, client: OpenAI) -> None: |
| 112 | assistant = client.beta.assistants.update( |
| 113 | "string", |
| 114 | description="string", |
| 115 | file_ids=["string", "string", "string"], |
| 116 | instructions="string", |
| 117 | metadata={}, |
| 118 | model="string", |
| 119 | name="string", |
| 120 | tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}], |
| 121 | ) |
| 122 | assert_matches_type(Assistant, assistant, path=["response"]) |
| 123 | |
| 124 | @parametrize |
| 125 | def test_raw_response_update(self, client: OpenAI) -> None: |
| 126 | response = client.beta.assistants.with_raw_response.update( |
| 127 | "string", |
| 128 | ) |
| 129 | |
| 130 | assert response.is_closed is True |
| 131 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 132 | assistant = response.parse() |
| 133 | assert_matches_type(Assistant, assistant, path=["response"]) |
| 134 | |
| 135 | @parametrize |
| 136 | def test_streaming_response_update(self, client: OpenAI) -> None: |
| 137 | with client.beta.assistants.with_streaming_response.update( |
| 138 | "string", |
| 139 | ) as response: |
| 140 | assert not response.is_closed |
| 141 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 142 | |
| 143 | assistant = response.parse() |
| 144 | assert_matches_type(Assistant, assistant, path=["response"]) |
| 145 | |
| 146 | assert cast(Any, response.is_closed) is True |
| 147 | |
| 148 | @parametrize |
| 149 | def test_method_list(self, client: OpenAI) -> None: |
| 150 | assistant = client.beta.assistants.list() |
| 151 | assert_matches_type(SyncCursorPage[Assistant], assistant, path=["response"]) |
| 152 | |
| 153 | @parametrize |
| 154 | def test_method_list_with_all_params(self, client: OpenAI) -> None: |
| 155 | assistant = client.beta.assistants.list( |
| 156 | after="string", |
| 157 | before="string", |
| 158 | limit=0, |
| 159 | order="asc", |
| 160 | ) |
| 161 | assert_matches_type(SyncCursorPage[Assistant], assistant, path=["response"]) |
| 162 | |
| 163 | @parametrize |
| 164 | def test_raw_response_list(self, client: OpenAI) -> None: |
| 165 | response = client.beta.assistants.with_raw_response.list() |
| 166 | |
| 167 | assert response.is_closed is True |
| 168 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 169 | assistant = response.parse() |
| 170 | assert_matches_type(SyncCursorPage[Assistant], assistant, path=["response"]) |
| 171 | |
| 172 | @parametrize |
| 173 | def test_streaming_response_list(self, client: OpenAI) -> None: |
| 174 | with client.beta.assistants.with_streaming_response.list() as response: |
| 175 | assert not response.is_closed |
| 176 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 177 | |
| 178 | assistant = response.parse() |
| 179 | assert_matches_type(SyncCursorPage[Assistant], assistant, path=["response"]) |
| 180 | |
| 181 | assert cast(Any, response.is_closed) is True |
| 182 | |
| 183 | @parametrize |
| 184 | def test_method_delete(self, client: OpenAI) -> None: |
| 185 | assistant = client.beta.assistants.delete( |
| 186 | "string", |
| 187 | ) |
| 188 | assert_matches_type(AssistantDeleted, assistant, path=["response"]) |
| 189 | |
| 190 | @parametrize |
| 191 | def test_raw_response_delete(self, client: OpenAI) -> None: |
| 192 | response = client.beta.assistants.with_raw_response.delete( |
| 193 | "string", |
| 194 | ) |
| 195 | |
| 196 | assert response.is_closed is True |
| 197 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 198 | assistant = response.parse() |
| 199 | assert_matches_type(AssistantDeleted, assistant, path=["response"]) |
| 200 | |
| 201 | @parametrize |
| 202 | def test_streaming_response_delete(self, client: OpenAI) -> None: |
| 203 | with client.beta.assistants.with_streaming_response.delete( |
| 204 | "string", |
| 205 | ) as response: |
| 206 | assert not response.is_closed |
| 207 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 208 | |
| 209 | assistant = response.parse() |
| 210 | assert_matches_type(AssistantDeleted, assistant, path=["response"]) |
| 211 | |
| 212 | assert cast(Any, response.is_closed) is True |
| 213 | |
| 214 | |
| 215 | class TestAsyncAssistants: |
| 216 | strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 217 | loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 218 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 219 | |
| 220 | @parametrize |
| 221 | async def test_method_create(self, client: AsyncOpenAI) -> None: |
| 222 | assistant = await client.beta.assistants.create( |
| 223 | model="string", |
| 224 | ) |
| 225 | assert_matches_type(Assistant, assistant, path=["response"]) |
| 226 | |
| 227 | @parametrize |
| 228 | async def test_method_create_with_all_params(self, client: AsyncOpenAI) -> None: |
| 229 | assistant = await client.beta.assistants.create( |
| 230 | model="string", |
| 231 | description="string", |
| 232 | file_ids=["string", "string", "string"], |
| 233 | instructions="string", |
| 234 | metadata={}, |
| 235 | name="string", |
| 236 | tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}], |
| 237 | ) |
| 238 | assert_matches_type(Assistant, assistant, path=["response"]) |
| 239 | |
| 240 | @parametrize |
| 241 | async def test_raw_response_create(self, client: AsyncOpenAI) -> None: |
| 242 | response = await client.beta.assistants.with_raw_response.create( |
| 243 | model="string", |
| 244 | ) |
| 245 | |
| 246 | assert response.is_closed is True |
| 247 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 248 | assistant = response.parse() |
| 249 | assert_matches_type(Assistant, assistant, path=["response"]) |
| 250 | |
| 251 | @parametrize |
| 252 | async def test_streaming_response_create(self, client: AsyncOpenAI) -> None: |
| 253 | async with client.beta.assistants.with_streaming_response.create( |
| 254 | model="string", |
| 255 | ) as response: |
| 256 | assert not response.is_closed |
| 257 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 258 | |
| 259 | assistant = await response.parse() |
| 260 | assert_matches_type(Assistant, assistant, path=["response"]) |
| 261 | |
| 262 | assert cast(Any, response.is_closed) is True |
| 263 | |
| 264 | @parametrize |
| 265 | async def test_method_retrieve(self, client: AsyncOpenAI) -> None: |
| 266 | assistant = await client.beta.assistants.retrieve( |
| 267 | "string", |
| 268 | ) |
| 269 | assert_matches_type(Assistant, assistant, path=["response"]) |
| 270 | |
| 271 | @parametrize |
| 272 | async def test_raw_response_retrieve(self, client: AsyncOpenAI) -> None: |
| 273 | response = await client.beta.assistants.with_raw_response.retrieve( |
| 274 | "string", |
| 275 | ) |
| 276 | |
| 277 | assert response.is_closed is True |
| 278 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 279 | assistant = response.parse() |
| 280 | assert_matches_type(Assistant, assistant, path=["response"]) |
| 281 | |
| 282 | @parametrize |
| 283 | async def test_streaming_response_retrieve(self, client: AsyncOpenAI) -> None: |
| 284 | async with client.beta.assistants.with_streaming_response.retrieve( |
| 285 | "string", |
| 286 | ) as response: |
| 287 | assert not response.is_closed |
| 288 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 289 | |
| 290 | assistant = await response.parse() |
| 291 | assert_matches_type(Assistant, assistant, path=["response"]) |
| 292 | |
| 293 | assert cast(Any, response.is_closed) is True |
| 294 | |
| 295 | @parametrize |
| 296 | async def test_method_update(self, client: AsyncOpenAI) -> None: |
| 297 | assistant = await client.beta.assistants.update( |
| 298 | "string", |
| 299 | ) |
| 300 | assert_matches_type(Assistant, assistant, path=["response"]) |
| 301 | |
| 302 | @parametrize |
| 303 | async def test_method_update_with_all_params(self, client: AsyncOpenAI) -> None: |
| 304 | assistant = await client.beta.assistants.update( |
| 305 | "string", |
| 306 | description="string", |
| 307 | file_ids=["string", "string", "string"], |
| 308 | instructions="string", |
| 309 | metadata={}, |
| 310 | model="string", |
| 311 | name="string", |
| 312 | tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}], |
| 313 | ) |
| 314 | assert_matches_type(Assistant, assistant, path=["response"]) |
| 315 | |
| 316 | @parametrize |
| 317 | async def test_raw_response_update(self, client: AsyncOpenAI) -> None: |
| 318 | response = await client.beta.assistants.with_raw_response.update( |
| 319 | "string", |
| 320 | ) |
| 321 | |
| 322 | assert response.is_closed is True |
| 323 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 324 | assistant = response.parse() |
| 325 | assert_matches_type(Assistant, assistant, path=["response"]) |
| 326 | |
| 327 | @parametrize |
| 328 | async def test_streaming_response_update(self, client: AsyncOpenAI) -> None: |
| 329 | async with client.beta.assistants.with_streaming_response.update( |
| 330 | "string", |
| 331 | ) as response: |
| 332 | assert not response.is_closed |
| 333 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 334 | |
| 335 | assistant = await response.parse() |
| 336 | assert_matches_type(Assistant, assistant, path=["response"]) |
| 337 | |
| 338 | assert cast(Any, response.is_closed) is True |
| 339 | |
| 340 | @parametrize |
| 341 | async def test_method_list(self, client: AsyncOpenAI) -> None: |
| 342 | assistant = await client.beta.assistants.list() |
| 343 | assert_matches_type(AsyncCursorPage[Assistant], assistant, path=["response"]) |
| 344 | |
| 345 | @parametrize |
| 346 | async def test_method_list_with_all_params(self, client: AsyncOpenAI) -> None: |
| 347 | assistant = await client.beta.assistants.list( |
| 348 | after="string", |
| 349 | before="string", |
| 350 | limit=0, |
| 351 | order="asc", |
| 352 | ) |
| 353 | assert_matches_type(AsyncCursorPage[Assistant], assistant, path=["response"]) |
| 354 | |
| 355 | @parametrize |
| 356 | async def test_raw_response_list(self, client: AsyncOpenAI) -> None: |
| 357 | response = await client.beta.assistants.with_raw_response.list() |
| 358 | |
| 359 | assert response.is_closed is True |
| 360 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 361 | assistant = response.parse() |
| 362 | assert_matches_type(AsyncCursorPage[Assistant], assistant, path=["response"]) |
| 363 | |
| 364 | @parametrize |
| 365 | async def test_streaming_response_list(self, client: AsyncOpenAI) -> None: |
| 366 | async with client.beta.assistants.with_streaming_response.list() as response: |
| 367 | assert not response.is_closed |
| 368 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 369 | |
| 370 | assistant = await response.parse() |
| 371 | assert_matches_type(AsyncCursorPage[Assistant], assistant, path=["response"]) |
| 372 | |
| 373 | assert cast(Any, response.is_closed) is True |
| 374 | |
| 375 | @parametrize |
| 376 | async def test_method_delete(self, client: AsyncOpenAI) -> None: |
| 377 | assistant = await client.beta.assistants.delete( |
| 378 | "string", |
| 379 | ) |
| 380 | assert_matches_type(AssistantDeleted, assistant, path=["response"]) |
| 381 | |
| 382 | @parametrize |
| 383 | async def test_raw_response_delete(self, client: AsyncOpenAI) -> None: |
| 384 | response = await client.beta.assistants.with_raw_response.delete( |
| 385 | "string", |
| 386 | ) |
| 387 | |
| 388 | assert response.is_closed is True |
| 389 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 390 | assistant = response.parse() |
| 391 | assert_matches_type(AssistantDeleted, assistant, path=["response"]) |
| 392 | |
| 393 | @parametrize |
| 394 | async def test_streaming_response_delete(self, client: AsyncOpenAI) -> None: |
| 395 | async with client.beta.assistants.with_streaming_response.delete( |
| 396 | "string", |
| 397 | ) as response: |
| 398 | assert not response.is_closed |
| 399 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 400 | |
| 401 | assistant = await response.parse() |
| 402 | assert_matches_type(AssistantDeleted, assistant, path=["response"]) |
| 403 | |
| 404 | assert cast(Any, response.is_closed) is True |
| 405 | |