openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.40.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/api_resources/chat/test_completions.py

500lines · modecode

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