openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/chat/test_completions.py
567lines · modecode
| 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 | import pydantic |
| 10 | |
| 11 | from openai import OpenAI, AsyncOpenAI |
| 12 | from tests.utils import assert_matches_type |
| 13 | from openai.types.chat import ( |
| 14 | ChatCompletion, |
| 15 | ) |
| 16 | |
| 17 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 18 | |
| 19 | |
| 20 | class TestCompletions: |
| 21 | parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 22 | |
| 23 | @parametrize |
| 24 | def test_method_create_overload_1(self, client: OpenAI) -> None: |
| 25 | completion = client.chat.completions.create( |
| 26 | messages=[ |
| 27 | { |
| 28 | "content": "string", |
| 29 | "role": "system", |
| 30 | } |
| 31 | ], |
| 32 | model="gpt-4o", |
| 33 | ) |
| 34 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 35 | |
| 36 | @parametrize |
| 37 | def test_method_create_with_all_params_overload_1(self, client: OpenAI) -> None: |
| 38 | completion = client.chat.completions.create( |
| 39 | messages=[ |
| 40 | { |
| 41 | "content": "string", |
| 42 | "role": "system", |
| 43 | "name": "string", |
| 44 | } |
| 45 | ], |
| 46 | model="gpt-4o", |
| 47 | audio={ |
| 48 | "format": "wav", |
| 49 | "voice": "alloy", |
| 50 | }, |
| 51 | frequency_penalty=-2, |
| 52 | function_call="none", |
| 53 | functions=[ |
| 54 | { |
| 55 | "name": "name", |
| 56 | "description": "description", |
| 57 | "parameters": {"foo": "bar"}, |
| 58 | } |
| 59 | ], |
| 60 | logit_bias={"foo": 0}, |
| 61 | logprobs=True, |
| 62 | max_completion_tokens=0, |
| 63 | max_tokens=0, |
| 64 | metadata={"foo": "string"}, |
| 65 | modalities=["text", "audio"], |
| 66 | n=1, |
| 67 | parallel_tool_calls=True, |
| 68 | presence_penalty=-2, |
| 69 | response_format={"type": "text"}, |
| 70 | seed=-9007199254740991, |
| 71 | service_tier="auto", |
| 72 | stop="string", |
| 73 | store=True, |
| 74 | stream=False, |
| 75 | stream_options={"include_usage": True}, |
| 76 | temperature=1, |
| 77 | tool_choice="none", |
| 78 | tools=[ |
| 79 | { |
| 80 | "function": { |
| 81 | "name": "name", |
| 82 | "description": "description", |
| 83 | "parameters": {"foo": "bar"}, |
| 84 | "strict": True, |
| 85 | }, |
| 86 | "type": "function", |
| 87 | }, |
| 88 | { |
| 89 | "function": { |
| 90 | "name": "name", |
| 91 | "description": "description", |
| 92 | "parameters": {"foo": "bar"}, |
| 93 | "strict": True, |
| 94 | }, |
| 95 | "type": "function", |
| 96 | }, |
| 97 | { |
| 98 | "function": { |
| 99 | "name": "name", |
| 100 | "description": "description", |
| 101 | "parameters": {"foo": "bar"}, |
| 102 | "strict": True, |
| 103 | }, |
| 104 | "type": "function", |
| 105 | }, |
| 106 | ], |
| 107 | top_logprobs=0, |
| 108 | top_p=1, |
| 109 | user="user-1234", |
| 110 | ) |
| 111 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 112 | |
| 113 | @parametrize |
| 114 | def test_raw_response_create_overload_1(self, client: OpenAI) -> None: |
| 115 | response = client.chat.completions.with_raw_response.create( |
| 116 | messages=[ |
| 117 | { |
| 118 | "content": "string", |
| 119 | "role": "system", |
| 120 | } |
| 121 | ], |
| 122 | model="gpt-4o", |
| 123 | ) |
| 124 | |
| 125 | assert response.is_closed is True |
| 126 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 127 | completion = response.parse() |
| 128 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 129 | |
| 130 | @parametrize |
| 131 | def test_streaming_response_create_overload_1(self, client: OpenAI) -> None: |
| 132 | with client.chat.completions.with_streaming_response.create( |
| 133 | messages=[ |
| 134 | { |
| 135 | "content": "string", |
| 136 | "role": "system", |
| 137 | } |
| 138 | ], |
| 139 | model="gpt-4o", |
| 140 | ) as response: |
| 141 | assert not response.is_closed |
| 142 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 143 | |
| 144 | completion = response.parse() |
| 145 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 146 | |
| 147 | assert cast(Any, response.is_closed) is True |
| 148 | |
| 149 | @parametrize |
| 150 | def test_method_create_overload_2(self, client: OpenAI) -> None: |
| 151 | completion_stream = client.chat.completions.create( |
| 152 | messages=[ |
| 153 | { |
| 154 | "content": "string", |
| 155 | "role": "system", |
| 156 | } |
| 157 | ], |
| 158 | model="gpt-4o", |
| 159 | stream=True, |
| 160 | ) |
| 161 | completion_stream.response.close() |
| 162 | |
| 163 | @parametrize |
| 164 | def test_method_create_with_all_params_overload_2(self, client: OpenAI) -> None: |
| 165 | completion_stream = client.chat.completions.create( |
| 166 | messages=[ |
| 167 | { |
| 168 | "content": "string", |
| 169 | "role": "system", |
| 170 | "name": "string", |
| 171 | } |
| 172 | ], |
| 173 | model="gpt-4o", |
| 174 | stream=True, |
| 175 | audio={ |
| 176 | "format": "wav", |
| 177 | "voice": "alloy", |
| 178 | }, |
| 179 | frequency_penalty=-2, |
| 180 | function_call="none", |
| 181 | functions=[ |
| 182 | { |
| 183 | "name": "name", |
| 184 | "description": "description", |
| 185 | "parameters": {"foo": "bar"}, |
| 186 | } |
| 187 | ], |
| 188 | logit_bias={"foo": 0}, |
| 189 | logprobs=True, |
| 190 | max_completion_tokens=0, |
| 191 | max_tokens=0, |
| 192 | metadata={"foo": "string"}, |
| 193 | modalities=["text", "audio"], |
| 194 | n=1, |
| 195 | parallel_tool_calls=True, |
| 196 | presence_penalty=-2, |
| 197 | response_format={"type": "text"}, |
| 198 | seed=-9007199254740991, |
| 199 | service_tier="auto", |
| 200 | stop="string", |
| 201 | store=True, |
| 202 | stream_options={"include_usage": True}, |
| 203 | temperature=1, |
| 204 | tool_choice="none", |
| 205 | tools=[ |
| 206 | { |
| 207 | "function": { |
| 208 | "name": "name", |
| 209 | "description": "description", |
| 210 | "parameters": {"foo": "bar"}, |
| 211 | "strict": True, |
| 212 | }, |
| 213 | "type": "function", |
| 214 | }, |
| 215 | { |
| 216 | "function": { |
| 217 | "name": "name", |
| 218 | "description": "description", |
| 219 | "parameters": {"foo": "bar"}, |
| 220 | "strict": True, |
| 221 | }, |
| 222 | "type": "function", |
| 223 | }, |
| 224 | { |
| 225 | "function": { |
| 226 | "name": "name", |
| 227 | "description": "description", |
| 228 | "parameters": {"foo": "bar"}, |
| 229 | "strict": True, |
| 230 | }, |
| 231 | "type": "function", |
| 232 | }, |
| 233 | ], |
| 234 | top_logprobs=0, |
| 235 | top_p=1, |
| 236 | user="user-1234", |
| 237 | ) |
| 238 | completion_stream.response.close() |
| 239 | |
| 240 | @parametrize |
| 241 | def test_raw_response_create_overload_2(self, client: OpenAI) -> None: |
| 242 | response = client.chat.completions.with_raw_response.create( |
| 243 | messages=[ |
| 244 | { |
| 245 | "content": "string", |
| 246 | "role": "system", |
| 247 | } |
| 248 | ], |
| 249 | model="gpt-4o", |
| 250 | stream=True, |
| 251 | ) |
| 252 | |
| 253 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 254 | stream = response.parse() |
| 255 | stream.close() |
| 256 | |
| 257 | @parametrize |
| 258 | def test_streaming_response_create_overload_2(self, client: OpenAI) -> None: |
| 259 | with client.chat.completions.with_streaming_response.create( |
| 260 | messages=[ |
| 261 | { |
| 262 | "content": "string", |
| 263 | "role": "system", |
| 264 | } |
| 265 | ], |
| 266 | model="gpt-4o", |
| 267 | stream=True, |
| 268 | ) as response: |
| 269 | assert not response.is_closed |
| 270 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 271 | |
| 272 | stream = response.parse() |
| 273 | stream.close() |
| 274 | |
| 275 | assert cast(Any, response.is_closed) is True |
| 276 | |
| 277 | @parametrize |
| 278 | def test_method_create_disallows_pydantic(self, client: OpenAI) -> None: |
| 279 | class MyModel(pydantic.BaseModel): |
| 280 | a: str |
| 281 | |
| 282 | with pytest.raises(TypeError, match=r"You tried to pass a `BaseModel` class"): |
| 283 | client.chat.completions.create( |
| 284 | messages=[ |
| 285 | { |
| 286 | "content": "string", |
| 287 | "role": "system", |
| 288 | } |
| 289 | ], |
| 290 | model="gpt-4o", |
| 291 | response_format=cast(Any, MyModel), |
| 292 | ) |
| 293 | |
| 294 | |
| 295 | class TestAsyncCompletions: |
| 296 | parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 297 | |
| 298 | @parametrize |
| 299 | async def test_method_create_overload_1(self, async_client: AsyncOpenAI) -> None: |
| 300 | completion = await async_client.chat.completions.create( |
| 301 | messages=[ |
| 302 | { |
| 303 | "content": "string", |
| 304 | "role": "system", |
| 305 | } |
| 306 | ], |
| 307 | model="gpt-4o", |
| 308 | ) |
| 309 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 310 | |
| 311 | @parametrize |
| 312 | async def test_method_create_with_all_params_overload_1(self, async_client: AsyncOpenAI) -> None: |
| 313 | completion = await async_client.chat.completions.create( |
| 314 | messages=[ |
| 315 | { |
| 316 | "content": "string", |
| 317 | "role": "system", |
| 318 | "name": "string", |
| 319 | } |
| 320 | ], |
| 321 | model="gpt-4o", |
| 322 | audio={ |
| 323 | "format": "wav", |
| 324 | "voice": "alloy", |
| 325 | }, |
| 326 | frequency_penalty=-2, |
| 327 | function_call="none", |
| 328 | functions=[ |
| 329 | { |
| 330 | "name": "name", |
| 331 | "description": "description", |
| 332 | "parameters": {"foo": "bar"}, |
| 333 | } |
| 334 | ], |
| 335 | logit_bias={"foo": 0}, |
| 336 | logprobs=True, |
| 337 | max_completion_tokens=0, |
| 338 | max_tokens=0, |
| 339 | metadata={"foo": "string"}, |
| 340 | modalities=["text", "audio"], |
| 341 | n=1, |
| 342 | parallel_tool_calls=True, |
| 343 | presence_penalty=-2, |
| 344 | response_format={"type": "text"}, |
| 345 | seed=-9007199254740991, |
| 346 | service_tier="auto", |
| 347 | stop="string", |
| 348 | store=True, |
| 349 | stream=False, |
| 350 | stream_options={"include_usage": True}, |
| 351 | temperature=1, |
| 352 | tool_choice="none", |
| 353 | tools=[ |
| 354 | { |
| 355 | "function": { |
| 356 | "name": "name", |
| 357 | "description": "description", |
| 358 | "parameters": {"foo": "bar"}, |
| 359 | "strict": True, |
| 360 | }, |
| 361 | "type": "function", |
| 362 | }, |
| 363 | { |
| 364 | "function": { |
| 365 | "name": "name", |
| 366 | "description": "description", |
| 367 | "parameters": {"foo": "bar"}, |
| 368 | "strict": True, |
| 369 | }, |
| 370 | "type": "function", |
| 371 | }, |
| 372 | { |
| 373 | "function": { |
| 374 | "name": "name", |
| 375 | "description": "description", |
| 376 | "parameters": {"foo": "bar"}, |
| 377 | "strict": True, |
| 378 | }, |
| 379 | "type": "function", |
| 380 | }, |
| 381 | ], |
| 382 | top_logprobs=0, |
| 383 | top_p=1, |
| 384 | user="user-1234", |
| 385 | ) |
| 386 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 387 | |
| 388 | @parametrize |
| 389 | async def test_raw_response_create_overload_1(self, async_client: AsyncOpenAI) -> None: |
| 390 | response = await async_client.chat.completions.with_raw_response.create( |
| 391 | messages=[ |
| 392 | { |
| 393 | "content": "string", |
| 394 | "role": "system", |
| 395 | } |
| 396 | ], |
| 397 | model="gpt-4o", |
| 398 | ) |
| 399 | |
| 400 | assert response.is_closed is True |
| 401 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 402 | completion = response.parse() |
| 403 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 404 | |
| 405 | @parametrize |
| 406 | async def test_streaming_response_create_overload_1(self, async_client: AsyncOpenAI) -> None: |
| 407 | async with async_client.chat.completions.with_streaming_response.create( |
| 408 | messages=[ |
| 409 | { |
| 410 | "content": "string", |
| 411 | "role": "system", |
| 412 | } |
| 413 | ], |
| 414 | model="gpt-4o", |
| 415 | ) as response: |
| 416 | assert not response.is_closed |
| 417 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 418 | |
| 419 | completion = await response.parse() |
| 420 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 421 | |
| 422 | assert cast(Any, response.is_closed) is True |
| 423 | |
| 424 | @parametrize |
| 425 | async def test_method_create_overload_2(self, async_client: AsyncOpenAI) -> None: |
| 426 | completion_stream = await async_client.chat.completions.create( |
| 427 | messages=[ |
| 428 | { |
| 429 | "content": "string", |
| 430 | "role": "system", |
| 431 | } |
| 432 | ], |
| 433 | model="gpt-4o", |
| 434 | stream=True, |
| 435 | ) |
| 436 | await completion_stream.response.aclose() |
| 437 | |
| 438 | @parametrize |
| 439 | async def test_method_create_with_all_params_overload_2(self, async_client: AsyncOpenAI) -> None: |
| 440 | completion_stream = await async_client.chat.completions.create( |
| 441 | messages=[ |
| 442 | { |
| 443 | "content": "string", |
| 444 | "role": "system", |
| 445 | "name": "string", |
| 446 | } |
| 447 | ], |
| 448 | model="gpt-4o", |
| 449 | stream=True, |
| 450 | audio={ |
| 451 | "format": "wav", |
| 452 | "voice": "alloy", |
| 453 | }, |
| 454 | frequency_penalty=-2, |
| 455 | function_call="none", |
| 456 | functions=[ |
| 457 | { |
| 458 | "name": "name", |
| 459 | "description": "description", |
| 460 | "parameters": {"foo": "bar"}, |
| 461 | } |
| 462 | ], |
| 463 | logit_bias={"foo": 0}, |
| 464 | logprobs=True, |
| 465 | max_completion_tokens=0, |
| 466 | max_tokens=0, |
| 467 | metadata={"foo": "string"}, |
| 468 | modalities=["text", "audio"], |
| 469 | n=1, |
| 470 | parallel_tool_calls=True, |
| 471 | presence_penalty=-2, |
| 472 | response_format={"type": "text"}, |
| 473 | seed=-9007199254740991, |
| 474 | service_tier="auto", |
| 475 | stop="string", |
| 476 | store=True, |
| 477 | stream_options={"include_usage": True}, |
| 478 | temperature=1, |
| 479 | tool_choice="none", |
| 480 | tools=[ |
| 481 | { |
| 482 | "function": { |
| 483 | "name": "name", |
| 484 | "description": "description", |
| 485 | "parameters": {"foo": "bar"}, |
| 486 | "strict": True, |
| 487 | }, |
| 488 | "type": "function", |
| 489 | }, |
| 490 | { |
| 491 | "function": { |
| 492 | "name": "name", |
| 493 | "description": "description", |
| 494 | "parameters": {"foo": "bar"}, |
| 495 | "strict": True, |
| 496 | }, |
| 497 | "type": "function", |
| 498 | }, |
| 499 | { |
| 500 | "function": { |
| 501 | "name": "name", |
| 502 | "description": "description", |
| 503 | "parameters": {"foo": "bar"}, |
| 504 | "strict": True, |
| 505 | }, |
| 506 | "type": "function", |
| 507 | }, |
| 508 | ], |
| 509 | top_logprobs=0, |
| 510 | top_p=1, |
| 511 | user="user-1234", |
| 512 | ) |
| 513 | await completion_stream.response.aclose() |
| 514 | |
| 515 | @parametrize |
| 516 | async def test_raw_response_create_overload_2(self, async_client: AsyncOpenAI) -> None: |
| 517 | response = await async_client.chat.completions.with_raw_response.create( |
| 518 | messages=[ |
| 519 | { |
| 520 | "content": "string", |
| 521 | "role": "system", |
| 522 | } |
| 523 | ], |
| 524 | model="gpt-4o", |
| 525 | stream=True, |
| 526 | ) |
| 527 | |
| 528 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 529 | stream = response.parse() |
| 530 | await stream.close() |
| 531 | |
| 532 | @parametrize |
| 533 | async def test_streaming_response_create_overload_2(self, async_client: AsyncOpenAI) -> None: |
| 534 | async with async_client.chat.completions.with_streaming_response.create( |
| 535 | messages=[ |
| 536 | { |
| 537 | "content": "string", |
| 538 | "role": "system", |
| 539 | } |
| 540 | ], |
| 541 | model="gpt-4o", |
| 542 | stream=True, |
| 543 | ) as response: |
| 544 | assert not response.is_closed |
| 545 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 546 | |
| 547 | stream = await response.parse() |
| 548 | await stream.close() |
| 549 | |
| 550 | assert cast(Any, response.is_closed) is True |
| 551 | |
| 552 | @parametrize |
| 553 | async def test_method_create_disallows_pydantic(self, async_client: AsyncOpenAI) -> None: |
| 554 | class MyModel(pydantic.BaseModel): |
| 555 | a: str |
| 556 | |
| 557 | with pytest.raises(TypeError, match=r"You tried to pass a `BaseModel` class"): |
| 558 | await async_client.chat.completions.create( |
| 559 | messages=[ |
| 560 | { |
| 561 | "content": "string", |
| 562 | "role": "system", |
| 563 | } |
| 564 | ], |
| 565 | model="gpt-4o", |
| 566 | response_format=cast(Any, MyModel), |
| 567 | ) |