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