openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/api_resources/chat/test_completions.py
377lines · 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 | } |
| 44 | ], |
| 45 | model="gpt-3.5-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 | max_tokens=0, |
| 57 | n=1, |
| 58 | presence_penalty=-2, |
| 59 | response_format={"type": "json_object"}, |
| 60 | seed=-9223372036854776000, |
| 61 | stop="string", |
| 62 | stream=False, |
| 63 | temperature=1, |
| 64 | tool_choice="none", |
| 65 | tools=[ |
| 66 | { |
| 67 | "type": "function", |
| 68 | "function": { |
| 69 | "description": "string", |
| 70 | "name": "string", |
| 71 | "parameters": {"foo": "bar"}, |
| 72 | }, |
| 73 | }, |
| 74 | { |
| 75 | "type": "function", |
| 76 | "function": { |
| 77 | "description": "string", |
| 78 | "name": "string", |
| 79 | "parameters": {"foo": "bar"}, |
| 80 | }, |
| 81 | }, |
| 82 | { |
| 83 | "type": "function", |
| 84 | "function": { |
| 85 | "description": "string", |
| 86 | "name": "string", |
| 87 | "parameters": {"foo": "bar"}, |
| 88 | }, |
| 89 | }, |
| 90 | ], |
| 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-3.5-turbo", |
| 106 | ) |
| 107 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 108 | completion = response.parse() |
| 109 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 110 | |
| 111 | @parametrize |
| 112 | def test_method_create_overload_2(self, client: OpenAI) -> None: |
| 113 | client.chat.completions.create( |
| 114 | messages=[ |
| 115 | { |
| 116 | "content": "string", |
| 117 | "role": "system", |
| 118 | } |
| 119 | ], |
| 120 | model="gpt-3.5-turbo", |
| 121 | stream=True, |
| 122 | ) |
| 123 | |
| 124 | @parametrize |
| 125 | def test_method_create_with_all_params_overload_2(self, client: OpenAI) -> None: |
| 126 | client.chat.completions.create( |
| 127 | messages=[ |
| 128 | { |
| 129 | "content": "string", |
| 130 | "role": "system", |
| 131 | } |
| 132 | ], |
| 133 | model="gpt-3.5-turbo", |
| 134 | stream=True, |
| 135 | frequency_penalty=-2, |
| 136 | function_call="none", |
| 137 | functions=[ |
| 138 | { |
| 139 | "description": "string", |
| 140 | "name": "string", |
| 141 | "parameters": {"foo": "bar"}, |
| 142 | } |
| 143 | ], |
| 144 | logit_bias={"foo": 0}, |
| 145 | max_tokens=0, |
| 146 | n=1, |
| 147 | presence_penalty=-2, |
| 148 | response_format={"type": "json_object"}, |
| 149 | seed=-9223372036854776000, |
| 150 | stop="string", |
| 151 | temperature=1, |
| 152 | tool_choice="none", |
| 153 | tools=[ |
| 154 | { |
| 155 | "type": "function", |
| 156 | "function": { |
| 157 | "description": "string", |
| 158 | "name": "string", |
| 159 | "parameters": {"foo": "bar"}, |
| 160 | }, |
| 161 | }, |
| 162 | { |
| 163 | "type": "function", |
| 164 | "function": { |
| 165 | "description": "string", |
| 166 | "name": "string", |
| 167 | "parameters": {"foo": "bar"}, |
| 168 | }, |
| 169 | }, |
| 170 | { |
| 171 | "type": "function", |
| 172 | "function": { |
| 173 | "description": "string", |
| 174 | "name": "string", |
| 175 | "parameters": {"foo": "bar"}, |
| 176 | }, |
| 177 | }, |
| 178 | ], |
| 179 | top_p=1, |
| 180 | user="user-1234", |
| 181 | ) |
| 182 | |
| 183 | @parametrize |
| 184 | def test_raw_response_create_overload_2(self, client: OpenAI) -> None: |
| 185 | response = client.chat.completions.with_raw_response.create( |
| 186 | messages=[ |
| 187 | { |
| 188 | "content": "string", |
| 189 | "role": "system", |
| 190 | } |
| 191 | ], |
| 192 | model="gpt-3.5-turbo", |
| 193 | stream=True, |
| 194 | ) |
| 195 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 196 | response.parse() |
| 197 | |
| 198 | |
| 199 | class TestAsyncCompletions: |
| 200 | strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 201 | loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 202 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 203 | |
| 204 | @parametrize |
| 205 | async def test_method_create_overload_1(self, client: AsyncOpenAI) -> None: |
| 206 | completion = await client.chat.completions.create( |
| 207 | messages=[ |
| 208 | { |
| 209 | "content": "string", |
| 210 | "role": "system", |
| 211 | } |
| 212 | ], |
| 213 | model="gpt-3.5-turbo", |
| 214 | ) |
| 215 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 216 | |
| 217 | @parametrize |
| 218 | async def test_method_create_with_all_params_overload_1(self, client: AsyncOpenAI) -> None: |
| 219 | completion = await client.chat.completions.create( |
| 220 | messages=[ |
| 221 | { |
| 222 | "content": "string", |
| 223 | "role": "system", |
| 224 | } |
| 225 | ], |
| 226 | model="gpt-3.5-turbo", |
| 227 | frequency_penalty=-2, |
| 228 | function_call="none", |
| 229 | functions=[ |
| 230 | { |
| 231 | "description": "string", |
| 232 | "name": "string", |
| 233 | "parameters": {"foo": "bar"}, |
| 234 | } |
| 235 | ], |
| 236 | logit_bias={"foo": 0}, |
| 237 | max_tokens=0, |
| 238 | n=1, |
| 239 | presence_penalty=-2, |
| 240 | response_format={"type": "json_object"}, |
| 241 | seed=-9223372036854776000, |
| 242 | stop="string", |
| 243 | stream=False, |
| 244 | temperature=1, |
| 245 | tool_choice="none", |
| 246 | tools=[ |
| 247 | { |
| 248 | "type": "function", |
| 249 | "function": { |
| 250 | "description": "string", |
| 251 | "name": "string", |
| 252 | "parameters": {"foo": "bar"}, |
| 253 | }, |
| 254 | }, |
| 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 | top_p=1, |
| 273 | user="user-1234", |
| 274 | ) |
| 275 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 276 | |
| 277 | @parametrize |
| 278 | async def test_raw_response_create_overload_1(self, client: AsyncOpenAI) -> None: |
| 279 | response = await client.chat.completions.with_raw_response.create( |
| 280 | messages=[ |
| 281 | { |
| 282 | "content": "string", |
| 283 | "role": "system", |
| 284 | } |
| 285 | ], |
| 286 | model="gpt-3.5-turbo", |
| 287 | ) |
| 288 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 289 | completion = response.parse() |
| 290 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 291 | |
| 292 | @parametrize |
| 293 | async def test_method_create_overload_2(self, client: AsyncOpenAI) -> None: |
| 294 | await client.chat.completions.create( |
| 295 | messages=[ |
| 296 | { |
| 297 | "content": "string", |
| 298 | "role": "system", |
| 299 | } |
| 300 | ], |
| 301 | model="gpt-3.5-turbo", |
| 302 | stream=True, |
| 303 | ) |
| 304 | |
| 305 | @parametrize |
| 306 | async def test_method_create_with_all_params_overload_2(self, client: AsyncOpenAI) -> None: |
| 307 | await client.chat.completions.create( |
| 308 | messages=[ |
| 309 | { |
| 310 | "content": "string", |
| 311 | "role": "system", |
| 312 | } |
| 313 | ], |
| 314 | model="gpt-3.5-turbo", |
| 315 | stream=True, |
| 316 | frequency_penalty=-2, |
| 317 | function_call="none", |
| 318 | functions=[ |
| 319 | { |
| 320 | "description": "string", |
| 321 | "name": "string", |
| 322 | "parameters": {"foo": "bar"}, |
| 323 | } |
| 324 | ], |
| 325 | logit_bias={"foo": 0}, |
| 326 | max_tokens=0, |
| 327 | n=1, |
| 328 | presence_penalty=-2, |
| 329 | response_format={"type": "json_object"}, |
| 330 | seed=-9223372036854776000, |
| 331 | stop="string", |
| 332 | temperature=1, |
| 333 | tool_choice="none", |
| 334 | tools=[ |
| 335 | { |
| 336 | "type": "function", |
| 337 | "function": { |
| 338 | "description": "string", |
| 339 | "name": "string", |
| 340 | "parameters": {"foo": "bar"}, |
| 341 | }, |
| 342 | }, |
| 343 | { |
| 344 | "type": "function", |
| 345 | "function": { |
| 346 | "description": "string", |
| 347 | "name": "string", |
| 348 | "parameters": {"foo": "bar"}, |
| 349 | }, |
| 350 | }, |
| 351 | { |
| 352 | "type": "function", |
| 353 | "function": { |
| 354 | "description": "string", |
| 355 | "name": "string", |
| 356 | "parameters": {"foo": "bar"}, |
| 357 | }, |
| 358 | }, |
| 359 | ], |
| 360 | top_p=1, |
| 361 | user="user-1234", |
| 362 | ) |
| 363 | |
| 364 | @parametrize |
| 365 | async def test_raw_response_create_overload_2(self, client: AsyncOpenAI) -> None: |
| 366 | response = await client.chat.completions.with_raw_response.create( |
| 367 | messages=[ |
| 368 | { |
| 369 | "content": "string", |
| 370 | "role": "system", |
| 371 | } |
| 372 | ], |
| 373 | model="gpt-3.5-turbo", |
| 374 | stream=True, |
| 375 | ) |
| 376 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 377 | response.parse() |
| 378 | |