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