openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/chat/test_completions.py
480lines · modecode
| 1 | # File generated from our OpenAPI spec by Stainless. |
| 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._client import OpenAI, AsyncOpenAI |
| 13 | from openai.types.chat import ChatCompletion |
| 14 | |
| 15 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 16 | api_key = "My API Key" |
| 17 | |
| 18 | |
| 19 | class TestCompletions: |
| 20 | strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 21 | loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 22 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 23 | |
| 24 | @parametrize |
| 25 | def test_method_create_overload_1(self, client: OpenAI) -> None: |
| 26 | completion = client.chat.completions.create( |
| 27 | messages=[ |
| 28 | { |
| 29 | "content": "string", |
| 30 | "role": "system", |
| 31 | } |
| 32 | ], |
| 33 | model="gpt-3.5-turbo", |
| 34 | ) |
| 35 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 36 | |
| 37 | @parametrize |
| 38 | def test_method_create_with_all_params_overload_1(self, client: OpenAI) -> None: |
| 39 | completion = client.chat.completions.create( |
| 40 | messages=[ |
| 41 | { |
| 42 | "content": "string", |
| 43 | "role": "system", |
| 44 | "name": "string", |
| 45 | } |
| 46 | ], |
| 47 | model="gpt-3.5-turbo", |
| 48 | frequency_penalty=-2, |
| 49 | function_call="none", |
| 50 | functions=[ |
| 51 | { |
| 52 | "description": "string", |
| 53 | "name": "string", |
| 54 | "parameters": {"foo": "bar"}, |
| 55 | } |
| 56 | ], |
| 57 | logit_bias={"foo": 0}, |
| 58 | logprobs=True, |
| 59 | max_tokens=0, |
| 60 | n=1, |
| 61 | presence_penalty=-2, |
| 62 | response_format={"type": "json_object"}, |
| 63 | seed=-9223372036854776000, |
| 64 | stop="string", |
| 65 | stream=False, |
| 66 | temperature=1, |
| 67 | tool_choice="none", |
| 68 | tools=[ |
| 69 | { |
| 70 | "type": "function", |
| 71 | "function": { |
| 72 | "description": "string", |
| 73 | "name": "string", |
| 74 | "parameters": {"foo": "bar"}, |
| 75 | }, |
| 76 | }, |
| 77 | { |
| 78 | "type": "function", |
| 79 | "function": { |
| 80 | "description": "string", |
| 81 | "name": "string", |
| 82 | "parameters": {"foo": "bar"}, |
| 83 | }, |
| 84 | }, |
| 85 | { |
| 86 | "type": "function", |
| 87 | "function": { |
| 88 | "description": "string", |
| 89 | "name": "string", |
| 90 | "parameters": {"foo": "bar"}, |
| 91 | }, |
| 92 | }, |
| 93 | ], |
| 94 | top_logprobs=0, |
| 95 | top_p=1, |
| 96 | user="user-1234", |
| 97 | ) |
| 98 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 99 | |
| 100 | @parametrize |
| 101 | def test_raw_response_create_overload_1(self, client: OpenAI) -> None: |
| 102 | response = client.chat.completions.with_raw_response.create( |
| 103 | messages=[ |
| 104 | { |
| 105 | "content": "string", |
| 106 | "role": "system", |
| 107 | } |
| 108 | ], |
| 109 | model="gpt-3.5-turbo", |
| 110 | ) |
| 111 | |
| 112 | assert response.is_closed is True |
| 113 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 114 | completion = response.parse() |
| 115 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 116 | |
| 117 | @parametrize |
| 118 | def test_streaming_response_create_overload_1(self, client: OpenAI) -> None: |
| 119 | with client.chat.completions.with_streaming_response.create( |
| 120 | messages=[ |
| 121 | { |
| 122 | "content": "string", |
| 123 | "role": "system", |
| 124 | } |
| 125 | ], |
| 126 | model="gpt-3.5-turbo", |
| 127 | ) as response: |
| 128 | assert not response.is_closed |
| 129 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 130 | |
| 131 | completion = response.parse() |
| 132 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 133 | |
| 134 | assert cast(Any, response.is_closed) is True |
| 135 | |
| 136 | @parametrize |
| 137 | def test_method_create_overload_2(self, client: OpenAI) -> None: |
| 138 | completion_stream = client.chat.completions.create( |
| 139 | messages=[ |
| 140 | { |
| 141 | "content": "string", |
| 142 | "role": "system", |
| 143 | } |
| 144 | ], |
| 145 | model="gpt-3.5-turbo", |
| 146 | stream=True, |
| 147 | ) |
| 148 | completion_stream.response.close() |
| 149 | |
| 150 | @parametrize |
| 151 | def test_method_create_with_all_params_overload_2(self, client: OpenAI) -> None: |
| 152 | completion_stream = client.chat.completions.create( |
| 153 | messages=[ |
| 154 | { |
| 155 | "content": "string", |
| 156 | "role": "system", |
| 157 | "name": "string", |
| 158 | } |
| 159 | ], |
| 160 | model="gpt-3.5-turbo", |
| 161 | stream=True, |
| 162 | frequency_penalty=-2, |
| 163 | function_call="none", |
| 164 | functions=[ |
| 165 | { |
| 166 | "description": "string", |
| 167 | "name": "string", |
| 168 | "parameters": {"foo": "bar"}, |
| 169 | } |
| 170 | ], |
| 171 | logit_bias={"foo": 0}, |
| 172 | logprobs=True, |
| 173 | max_tokens=0, |
| 174 | n=1, |
| 175 | presence_penalty=-2, |
| 176 | response_format={"type": "json_object"}, |
| 177 | seed=-9223372036854776000, |
| 178 | stop="string", |
| 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-3.5-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-3.5-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 | strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 253 | loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 254 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 255 | |
| 256 | @parametrize |
| 257 | async def test_method_create_overload_1(self, client: AsyncOpenAI) -> None: |
| 258 | completion = await client.chat.completions.create( |
| 259 | messages=[ |
| 260 | { |
| 261 | "content": "string", |
| 262 | "role": "system", |
| 263 | } |
| 264 | ], |
| 265 | model="gpt-3.5-turbo", |
| 266 | ) |
| 267 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 268 | |
| 269 | @parametrize |
| 270 | async def test_method_create_with_all_params_overload_1(self, client: AsyncOpenAI) -> None: |
| 271 | completion = await client.chat.completions.create( |
| 272 | messages=[ |
| 273 | { |
| 274 | "content": "string", |
| 275 | "role": "system", |
| 276 | "name": "string", |
| 277 | } |
| 278 | ], |
| 279 | model="gpt-3.5-turbo", |
| 280 | frequency_penalty=-2, |
| 281 | function_call="none", |
| 282 | functions=[ |
| 283 | { |
| 284 | "description": "string", |
| 285 | "name": "string", |
| 286 | "parameters": {"foo": "bar"}, |
| 287 | } |
| 288 | ], |
| 289 | logit_bias={"foo": 0}, |
| 290 | logprobs=True, |
| 291 | max_tokens=0, |
| 292 | n=1, |
| 293 | presence_penalty=-2, |
| 294 | response_format={"type": "json_object"}, |
| 295 | seed=-9223372036854776000, |
| 296 | stop="string", |
| 297 | stream=False, |
| 298 | temperature=1, |
| 299 | tool_choice="none", |
| 300 | tools=[ |
| 301 | { |
| 302 | "type": "function", |
| 303 | "function": { |
| 304 | "description": "string", |
| 305 | "name": "string", |
| 306 | "parameters": {"foo": "bar"}, |
| 307 | }, |
| 308 | }, |
| 309 | { |
| 310 | "type": "function", |
| 311 | "function": { |
| 312 | "description": "string", |
| 313 | "name": "string", |
| 314 | "parameters": {"foo": "bar"}, |
| 315 | }, |
| 316 | }, |
| 317 | { |
| 318 | "type": "function", |
| 319 | "function": { |
| 320 | "description": "string", |
| 321 | "name": "string", |
| 322 | "parameters": {"foo": "bar"}, |
| 323 | }, |
| 324 | }, |
| 325 | ], |
| 326 | top_logprobs=0, |
| 327 | top_p=1, |
| 328 | user="user-1234", |
| 329 | ) |
| 330 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 331 | |
| 332 | @parametrize |
| 333 | async def test_raw_response_create_overload_1(self, client: AsyncOpenAI) -> None: |
| 334 | response = await client.chat.completions.with_raw_response.create( |
| 335 | messages=[ |
| 336 | { |
| 337 | "content": "string", |
| 338 | "role": "system", |
| 339 | } |
| 340 | ], |
| 341 | model="gpt-3.5-turbo", |
| 342 | ) |
| 343 | |
| 344 | assert response.is_closed is True |
| 345 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 346 | completion = response.parse() |
| 347 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 348 | |
| 349 | @parametrize |
| 350 | async def test_streaming_response_create_overload_1(self, client: AsyncOpenAI) -> None: |
| 351 | async with client.chat.completions.with_streaming_response.create( |
| 352 | messages=[ |
| 353 | { |
| 354 | "content": "string", |
| 355 | "role": "system", |
| 356 | } |
| 357 | ], |
| 358 | model="gpt-3.5-turbo", |
| 359 | ) as response: |
| 360 | assert not response.is_closed |
| 361 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 362 | |
| 363 | completion = await response.parse() |
| 364 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 365 | |
| 366 | assert cast(Any, response.is_closed) is True |
| 367 | |
| 368 | @parametrize |
| 369 | async def test_method_create_overload_2(self, client: AsyncOpenAI) -> None: |
| 370 | completion_stream = await client.chat.completions.create( |
| 371 | messages=[ |
| 372 | { |
| 373 | "content": "string", |
| 374 | "role": "system", |
| 375 | } |
| 376 | ], |
| 377 | model="gpt-3.5-turbo", |
| 378 | stream=True, |
| 379 | ) |
| 380 | await completion_stream.response.aclose() |
| 381 | |
| 382 | @parametrize |
| 383 | async def test_method_create_with_all_params_overload_2(self, client: AsyncOpenAI) -> None: |
| 384 | completion_stream = await client.chat.completions.create( |
| 385 | messages=[ |
| 386 | { |
| 387 | "content": "string", |
| 388 | "role": "system", |
| 389 | "name": "string", |
| 390 | } |
| 391 | ], |
| 392 | model="gpt-3.5-turbo", |
| 393 | stream=True, |
| 394 | frequency_penalty=-2, |
| 395 | function_call="none", |
| 396 | functions=[ |
| 397 | { |
| 398 | "description": "string", |
| 399 | "name": "string", |
| 400 | "parameters": {"foo": "bar"}, |
| 401 | } |
| 402 | ], |
| 403 | logit_bias={"foo": 0}, |
| 404 | logprobs=True, |
| 405 | max_tokens=0, |
| 406 | n=1, |
| 407 | presence_penalty=-2, |
| 408 | response_format={"type": "json_object"}, |
| 409 | seed=-9223372036854776000, |
| 410 | stop="string", |
| 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, client: AsyncOpenAI) -> None: |
| 447 | response = await client.chat.completions.with_raw_response.create( |
| 448 | messages=[ |
| 449 | { |
| 450 | "content": "string", |
| 451 | "role": "system", |
| 452 | } |
| 453 | ], |
| 454 | model="gpt-3.5-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, client: AsyncOpenAI) -> None: |
| 464 | async with client.chat.completions.with_streaming_response.create( |
| 465 | messages=[ |
| 466 | { |
| 467 | "content": "string", |
| 468 | "role": "system", |
| 469 | } |
| 470 | ], |
| 471 | model="gpt-3.5-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 | |