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