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