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