openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/chat/test_completions.py
389lines · modecode
| 1 | # File generated from our OpenAPI spec by Stainless. |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import os |
| 6 | |
| 7 | import pytest |
| 8 | |
| 9 | from openai import OpenAI, AsyncOpenAI |
| 10 | from tests.utils import assert_matches_type |
| 11 | from openai._client import OpenAI, AsyncOpenAI |
| 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 | api_key = "My API Key" |
| 16 | |
| 17 | |
| 18 | class TestCompletions: |
| 19 | strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 20 | loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 21 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 22 | |
| 23 | @parametrize |
| 24 | def test_method_create_overload_1(self, client: OpenAI) -> None: |
| 25 | completion = client.chat.completions.create( |
| 26 | messages=[ |
| 27 | { |
| 28 | "content": "string", |
| 29 | "role": "system", |
| 30 | } |
| 31 | ], |
| 32 | model="gpt-3.5-turbo", |
| 33 | ) |
| 34 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 35 | |
| 36 | @parametrize |
| 37 | def test_method_create_with_all_params_overload_1(self, client: OpenAI) -> None: |
| 38 | completion = client.chat.completions.create( |
| 39 | messages=[ |
| 40 | { |
| 41 | "content": "string", |
| 42 | "role": "system", |
| 43 | "name": "string", |
| 44 | } |
| 45 | ], |
| 46 | model="gpt-3.5-turbo", |
| 47 | frequency_penalty=-2, |
| 48 | function_call="none", |
| 49 | functions=[ |
| 50 | { |
| 51 | "description": "string", |
| 52 | "name": "string", |
| 53 | "parameters": {"foo": "bar"}, |
| 54 | } |
| 55 | ], |
| 56 | logit_bias={"foo": 0}, |
| 57 | logprobs=True, |
| 58 | max_tokens=0, |
| 59 | n=1, |
| 60 | presence_penalty=-2, |
| 61 | response_format={"type": "json_object"}, |
| 62 | seed=-9223372036854776000, |
| 63 | stop="string", |
| 64 | stream=False, |
| 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-3.5-turbo", |
| 109 | ) |
| 110 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 111 | completion = response.parse() |
| 112 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 113 | |
| 114 | @parametrize |
| 115 | def test_method_create_overload_2(self, client: OpenAI) -> None: |
| 116 | client.chat.completions.create( |
| 117 | messages=[ |
| 118 | { |
| 119 | "content": "string", |
| 120 | "role": "system", |
| 121 | } |
| 122 | ], |
| 123 | model="gpt-3.5-turbo", |
| 124 | stream=True, |
| 125 | ) |
| 126 | |
| 127 | @parametrize |
| 128 | def test_method_create_with_all_params_overload_2(self, client: OpenAI) -> None: |
| 129 | client.chat.completions.create( |
| 130 | messages=[ |
| 131 | { |
| 132 | "content": "string", |
| 133 | "role": "system", |
| 134 | "name": "string", |
| 135 | } |
| 136 | ], |
| 137 | model="gpt-3.5-turbo", |
| 138 | stream=True, |
| 139 | frequency_penalty=-2, |
| 140 | function_call="none", |
| 141 | functions=[ |
| 142 | { |
| 143 | "description": "string", |
| 144 | "name": "string", |
| 145 | "parameters": {"foo": "bar"}, |
| 146 | } |
| 147 | ], |
| 148 | logit_bias={"foo": 0}, |
| 149 | logprobs=True, |
| 150 | max_tokens=0, |
| 151 | n=1, |
| 152 | presence_penalty=-2, |
| 153 | response_format={"type": "json_object"}, |
| 154 | seed=-9223372036854776000, |
| 155 | stop="string", |
| 156 | temperature=1, |
| 157 | tool_choice="none", |
| 158 | tools=[ |
| 159 | { |
| 160 | "type": "function", |
| 161 | "function": { |
| 162 | "description": "string", |
| 163 | "name": "string", |
| 164 | "parameters": {"foo": "bar"}, |
| 165 | }, |
| 166 | }, |
| 167 | { |
| 168 | "type": "function", |
| 169 | "function": { |
| 170 | "description": "string", |
| 171 | "name": "string", |
| 172 | "parameters": {"foo": "bar"}, |
| 173 | }, |
| 174 | }, |
| 175 | { |
| 176 | "type": "function", |
| 177 | "function": { |
| 178 | "description": "string", |
| 179 | "name": "string", |
| 180 | "parameters": {"foo": "bar"}, |
| 181 | }, |
| 182 | }, |
| 183 | ], |
| 184 | top_logprobs=0, |
| 185 | top_p=1, |
| 186 | user="user-1234", |
| 187 | ) |
| 188 | |
| 189 | @parametrize |
| 190 | def test_raw_response_create_overload_2(self, client: OpenAI) -> None: |
| 191 | response = client.chat.completions.with_raw_response.create( |
| 192 | messages=[ |
| 193 | { |
| 194 | "content": "string", |
| 195 | "role": "system", |
| 196 | } |
| 197 | ], |
| 198 | model="gpt-3.5-turbo", |
| 199 | stream=True, |
| 200 | ) |
| 201 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 202 | response.parse() |
| 203 | |
| 204 | |
| 205 | class TestAsyncCompletions: |
| 206 | strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 207 | loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 208 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 209 | |
| 210 | @parametrize |
| 211 | async def test_method_create_overload_1(self, client: AsyncOpenAI) -> None: |
| 212 | completion = await client.chat.completions.create( |
| 213 | messages=[ |
| 214 | { |
| 215 | "content": "string", |
| 216 | "role": "system", |
| 217 | } |
| 218 | ], |
| 219 | model="gpt-3.5-turbo", |
| 220 | ) |
| 221 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 222 | |
| 223 | @parametrize |
| 224 | async def test_method_create_with_all_params_overload_1(self, client: AsyncOpenAI) -> None: |
| 225 | completion = await client.chat.completions.create( |
| 226 | messages=[ |
| 227 | { |
| 228 | "content": "string", |
| 229 | "role": "system", |
| 230 | "name": "string", |
| 231 | } |
| 232 | ], |
| 233 | model="gpt-3.5-turbo", |
| 234 | frequency_penalty=-2, |
| 235 | function_call="none", |
| 236 | functions=[ |
| 237 | { |
| 238 | "description": "string", |
| 239 | "name": "string", |
| 240 | "parameters": {"foo": "bar"}, |
| 241 | } |
| 242 | ], |
| 243 | logit_bias={"foo": 0}, |
| 244 | logprobs=True, |
| 245 | max_tokens=0, |
| 246 | n=1, |
| 247 | presence_penalty=-2, |
| 248 | response_format={"type": "json_object"}, |
| 249 | seed=-9223372036854776000, |
| 250 | stop="string", |
| 251 | stream=False, |
| 252 | temperature=1, |
| 253 | tool_choice="none", |
| 254 | tools=[ |
| 255 | { |
| 256 | "type": "function", |
| 257 | "function": { |
| 258 | "description": "string", |
| 259 | "name": "string", |
| 260 | "parameters": {"foo": "bar"}, |
| 261 | }, |
| 262 | }, |
| 263 | { |
| 264 | "type": "function", |
| 265 | "function": { |
| 266 | "description": "string", |
| 267 | "name": "string", |
| 268 | "parameters": {"foo": "bar"}, |
| 269 | }, |
| 270 | }, |
| 271 | { |
| 272 | "type": "function", |
| 273 | "function": { |
| 274 | "description": "string", |
| 275 | "name": "string", |
| 276 | "parameters": {"foo": "bar"}, |
| 277 | }, |
| 278 | }, |
| 279 | ], |
| 280 | top_logprobs=0, |
| 281 | top_p=1, |
| 282 | user="user-1234", |
| 283 | ) |
| 284 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 285 | |
| 286 | @parametrize |
| 287 | async def test_raw_response_create_overload_1(self, client: AsyncOpenAI) -> None: |
| 288 | response = await client.chat.completions.with_raw_response.create( |
| 289 | messages=[ |
| 290 | { |
| 291 | "content": "string", |
| 292 | "role": "system", |
| 293 | } |
| 294 | ], |
| 295 | model="gpt-3.5-turbo", |
| 296 | ) |
| 297 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 298 | completion = response.parse() |
| 299 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 300 | |
| 301 | @parametrize |
| 302 | async def test_method_create_overload_2(self, client: AsyncOpenAI) -> None: |
| 303 | await client.chat.completions.create( |
| 304 | messages=[ |
| 305 | { |
| 306 | "content": "string", |
| 307 | "role": "system", |
| 308 | } |
| 309 | ], |
| 310 | model="gpt-3.5-turbo", |
| 311 | stream=True, |
| 312 | ) |
| 313 | |
| 314 | @parametrize |
| 315 | async def test_method_create_with_all_params_overload_2(self, client: AsyncOpenAI) -> None: |
| 316 | await client.chat.completions.create( |
| 317 | messages=[ |
| 318 | { |
| 319 | "content": "string", |
| 320 | "role": "system", |
| 321 | "name": "string", |
| 322 | } |
| 323 | ], |
| 324 | model="gpt-3.5-turbo", |
| 325 | stream=True, |
| 326 | frequency_penalty=-2, |
| 327 | function_call="none", |
| 328 | functions=[ |
| 329 | { |
| 330 | "description": "string", |
| 331 | "name": "string", |
| 332 | "parameters": {"foo": "bar"}, |
| 333 | } |
| 334 | ], |
| 335 | logit_bias={"foo": 0}, |
| 336 | logprobs=True, |
| 337 | max_tokens=0, |
| 338 | n=1, |
| 339 | presence_penalty=-2, |
| 340 | response_format={"type": "json_object"}, |
| 341 | seed=-9223372036854776000, |
| 342 | stop="string", |
| 343 | temperature=1, |
| 344 | tool_choice="none", |
| 345 | tools=[ |
| 346 | { |
| 347 | "type": "function", |
| 348 | "function": { |
| 349 | "description": "string", |
| 350 | "name": "string", |
| 351 | "parameters": {"foo": "bar"}, |
| 352 | }, |
| 353 | }, |
| 354 | { |
| 355 | "type": "function", |
| 356 | "function": { |
| 357 | "description": "string", |
| 358 | "name": "string", |
| 359 | "parameters": {"foo": "bar"}, |
| 360 | }, |
| 361 | }, |
| 362 | { |
| 363 | "type": "function", |
| 364 | "function": { |
| 365 | "description": "string", |
| 366 | "name": "string", |
| 367 | "parameters": {"foo": "bar"}, |
| 368 | }, |
| 369 | }, |
| 370 | ], |
| 371 | top_logprobs=0, |
| 372 | top_p=1, |
| 373 | user="user-1234", |
| 374 | ) |
| 375 | |
| 376 | @parametrize |
| 377 | async def test_raw_response_create_overload_2(self, client: AsyncOpenAI) -> None: |
| 378 | response = await client.chat.completions.with_raw_response.create( |
| 379 | messages=[ |
| 380 | { |
| 381 | "content": "string", |
| 382 | "role": "system", |
| 383 | } |
| 384 | ], |
| 385 | model="gpt-3.5-turbo", |
| 386 | stream=True, |
| 387 | ) |
| 388 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 389 | response.parse() |
| 390 | |