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