openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.60.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/test_completions.py

258lines · modeblame

5cfb125aStainless Bot2 years ago1# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
08b8179aDavid Schnurr2 years ago2
3from __future__ import annotations
4
5import os
86379b44Stainless Bot2 years ago6from typing import Any, cast
08b8179aDavid Schnurr2 years ago7
8import pytest
9
10from openai import OpenAI, AsyncOpenAI
11from tests.utils import assert_matches_type
4a0f0fa0Stainless Bot2 years ago12from openai.types import Completion
08b8179aDavid Schnurr2 years ago13
14base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
15
16
17class TestCompletions:
98d779fbStainless Bot2 years ago18parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
08b8179aDavid Schnurr2 years ago19
20@parametrize
21def test_method_create_overload_1(self, client: OpenAI) -> None:
22completion = client.completions.create(
156d13e1Stainless Bot2 years ago23model="string",
08b8179aDavid Schnurr2 years ago24prompt="This is a test.",
25)
26assert_matches_type(Completion, completion, path=["response"])
27
28@parametrize
29def test_method_create_with_all_params_overload_1(self, client: OpenAI) -> None:
30completion = client.completions.create(
156d13e1Stainless Bot2 years ago31model="string",
08b8179aDavid Schnurr2 years ago32prompt="This is a test.",
33best_of=0,
34echo=True,
35frequency_penalty=-2,
36logit_bias={"foo": 0},
37logprobs=0,
38max_tokens=16,
39n=1,
40presence_penalty=-2,
339d3151stainless-app[bot]1 years ago41seed=0,
08b8179aDavid Schnurr2 years ago42stop="\n",
43stream=False,
6cc51587Stainless Bot2 years ago44stream_options={"include_usage": True},
08b8179aDavid Schnurr2 years ago45suffix="test.",
46temperature=1,
47top_p=1,
48user="user-1234",
49)
50assert_matches_type(Completion, completion, path=["response"])
51
52@parametrize
53def test_raw_response_create_overload_1(self, client: OpenAI) -> None:
54response = client.completions.with_raw_response.create(
156d13e1Stainless Bot2 years ago55model="string",
08b8179aDavid Schnurr2 years ago56prompt="This is a test.",
57)
86379b44Stainless Bot2 years ago58
59assert response.is_closed is True
08b8179aDavid Schnurr2 years ago60assert response.http_request.headers.get("X-Stainless-Lang") == "python"
61completion = response.parse()
62assert_matches_type(Completion, completion, path=["response"])
63
86379b44Stainless Bot2 years ago64@parametrize
65def test_streaming_response_create_overload_1(self, client: OpenAI) -> None:
66with client.completions.with_streaming_response.create(
156d13e1Stainless Bot2 years ago67model="string",
86379b44Stainless Bot2 years ago68prompt="This is a test.",
69) as response:
70assert not response.is_closed
71assert response.http_request.headers.get("X-Stainless-Lang") == "python"
72
73completion = response.parse()
74assert_matches_type(Completion, completion, path=["response"])
75
76assert cast(Any, response.is_closed) is True
77
08b8179aDavid Schnurr2 years ago78@parametrize
79def test_method_create_overload_2(self, client: OpenAI) -> None:
86379b44Stainless Bot2 years ago80completion_stream = client.completions.create(
156d13e1Stainless Bot2 years ago81model="string",
08b8179aDavid Schnurr2 years ago82prompt="This is a test.",
83stream=True,
84)
86379b44Stainless Bot2 years ago85completion_stream.response.close()
08b8179aDavid Schnurr2 years ago86
87@parametrize
88def test_method_create_with_all_params_overload_2(self, client: OpenAI) -> None:
86379b44Stainless Bot2 years ago89completion_stream = client.completions.create(
156d13e1Stainless Bot2 years ago90model="string",
08b8179aDavid Schnurr2 years ago91prompt="This is a test.",
92stream=True,
93best_of=0,
94echo=True,
95frequency_penalty=-2,
96logit_bias={"foo": 0},
97logprobs=0,
98max_tokens=16,
99n=1,
100presence_penalty=-2,
339d3151stainless-app[bot]1 years ago101seed=0,
08b8179aDavid Schnurr2 years ago102stop="\n",
6cc51587Stainless Bot2 years ago103stream_options={"include_usage": True},
08b8179aDavid Schnurr2 years ago104suffix="test.",
105temperature=1,
106top_p=1,
107user="user-1234",
108)
86379b44Stainless Bot2 years ago109completion_stream.response.close()
08b8179aDavid Schnurr2 years ago110
111@parametrize
112def test_raw_response_create_overload_2(self, client: OpenAI) -> None:
113response = client.completions.with_raw_response.create(
156d13e1Stainless Bot2 years ago114model="string",
08b8179aDavid Schnurr2 years ago115prompt="This is a test.",
116stream=True,
117)
86379b44Stainless Bot2 years ago118
08b8179aDavid Schnurr2 years ago119assert response.http_request.headers.get("X-Stainless-Lang") == "python"
86379b44Stainless Bot2 years ago120stream = response.parse()
121stream.close()
122
123@parametrize
124def test_streaming_response_create_overload_2(self, client: OpenAI) -> None:
125with client.completions.with_streaming_response.create(
156d13e1Stainless Bot2 years ago126model="string",
86379b44Stainless Bot2 years ago127prompt="This is a test.",
128stream=True,
129) as response:
130assert not response.is_closed
131assert response.http_request.headers.get("X-Stainless-Lang") == "python"
132
133stream = response.parse()
134stream.close()
135
136assert cast(Any, response.is_closed) is True
08b8179aDavid Schnurr2 years ago137
138
139class TestAsyncCompletions:
98d779fbStainless Bot2 years ago140parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
08b8179aDavid Schnurr2 years ago141
142@parametrize
98d779fbStainless Bot2 years ago143async def test_method_create_overload_1(self, async_client: AsyncOpenAI) -> None:
144completion = await async_client.completions.create(
156d13e1Stainless Bot2 years ago145model="string",
08b8179aDavid Schnurr2 years ago146prompt="This is a test.",
147)
148assert_matches_type(Completion, completion, path=["response"])
149
150@parametrize
98d779fbStainless Bot2 years ago151async def test_method_create_with_all_params_overload_1(self, async_client: AsyncOpenAI) -> None:
152completion = await async_client.completions.create(
156d13e1Stainless Bot2 years ago153model="string",
08b8179aDavid Schnurr2 years ago154prompt="This is a test.",
155best_of=0,
156echo=True,
157frequency_penalty=-2,
158logit_bias={"foo": 0},
159logprobs=0,
160max_tokens=16,
161n=1,
162presence_penalty=-2,
339d3151stainless-app[bot]1 years ago163seed=0,
08b8179aDavid Schnurr2 years ago164stop="\n",
165stream=False,
6cc51587Stainless Bot2 years ago166stream_options={"include_usage": True},
08b8179aDavid Schnurr2 years ago167suffix="test.",
168temperature=1,
169top_p=1,
170user="user-1234",
171)
172assert_matches_type(Completion, completion, path=["response"])
173
174@parametrize
98d779fbStainless Bot2 years ago175async def test_raw_response_create_overload_1(self, async_client: AsyncOpenAI) -> None:
176response = await async_client.completions.with_raw_response.create(
156d13e1Stainless Bot2 years ago177model="string",
08b8179aDavid Schnurr2 years ago178prompt="This is a test.",
179)
86379b44Stainless Bot2 years ago180
181assert response.is_closed is True
08b8179aDavid Schnurr2 years ago182assert response.http_request.headers.get("X-Stainless-Lang") == "python"
183completion = response.parse()
184assert_matches_type(Completion, completion, path=["response"])
185
86379b44Stainless Bot2 years ago186@parametrize
98d779fbStainless Bot2 years ago187async def test_streaming_response_create_overload_1(self, async_client: AsyncOpenAI) -> None:
188async with async_client.completions.with_streaming_response.create(
156d13e1Stainless Bot2 years ago189model="string",
86379b44Stainless Bot2 years ago190prompt="This is a test.",
191) as response:
192assert not response.is_closed
193assert response.http_request.headers.get("X-Stainless-Lang") == "python"
194
195completion = await response.parse()
196assert_matches_type(Completion, completion, path=["response"])
197
198assert cast(Any, response.is_closed) is True
199
08b8179aDavid Schnurr2 years ago200@parametrize
98d779fbStainless Bot2 years ago201async def test_method_create_overload_2(self, async_client: AsyncOpenAI) -> None:
202completion_stream = await async_client.completions.create(
156d13e1Stainless Bot2 years ago203model="string",
08b8179aDavid Schnurr2 years ago204prompt="This is a test.",
205stream=True,
206)
86379b44Stainless Bot2 years ago207await completion_stream.response.aclose()
08b8179aDavid Schnurr2 years ago208
209@parametrize
98d779fbStainless Bot2 years ago210async def test_method_create_with_all_params_overload_2(self, async_client: AsyncOpenAI) -> None:
211completion_stream = await async_client.completions.create(
156d13e1Stainless Bot2 years ago212model="string",
08b8179aDavid Schnurr2 years ago213prompt="This is a test.",
214stream=True,
215best_of=0,
216echo=True,
217frequency_penalty=-2,
218logit_bias={"foo": 0},
219logprobs=0,
220max_tokens=16,
221n=1,
222presence_penalty=-2,
339d3151stainless-app[bot]1 years ago223seed=0,
08b8179aDavid Schnurr2 years ago224stop="\n",
6cc51587Stainless Bot2 years ago225stream_options={"include_usage": True},
08b8179aDavid Schnurr2 years ago226suffix="test.",
227temperature=1,
228top_p=1,
229user="user-1234",
230)
86379b44Stainless Bot2 years ago231await completion_stream.response.aclose()
08b8179aDavid Schnurr2 years ago232
233@parametrize
98d779fbStainless Bot2 years ago234async def test_raw_response_create_overload_2(self, async_client: AsyncOpenAI) -> None:
235response = await async_client.completions.with_raw_response.create(
156d13e1Stainless Bot2 years ago236model="string",
08b8179aDavid Schnurr2 years ago237prompt="This is a test.",
238stream=True,
239)
86379b44Stainless Bot2 years ago240
08b8179aDavid Schnurr2 years ago241assert response.http_request.headers.get("X-Stainless-Lang") == "python"
86379b44Stainless Bot2 years ago242stream = response.parse()
243await stream.close()
244
245@parametrize
98d779fbStainless Bot2 years ago246async def test_streaming_response_create_overload_2(self, async_client: AsyncOpenAI) -> None:
247async with async_client.completions.with_streaming_response.create(
156d13e1Stainless Bot2 years ago248model="string",
86379b44Stainless Bot2 years ago249prompt="This is a test.",
250stream=True,
251) as response:
252assert not response.is_closed
253assert response.http_request.headers.get("X-Stainless-Lang") == "python"
254
255stream = await response.parse()
256await stream.close()
257
258assert cast(Any, response.is_closed) is True