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