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