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