openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.8.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/test_models.py

231lines · modeblame

08b8179aDavid Schnurr2 years ago1# File generated from our OpenAPI spec by Stainless.
2
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
12from openai.types import Model, ModelDeleted
13from openai._client import OpenAI, AsyncOpenAI
14from openai.pagination import SyncPage, AsyncPage
15
16base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
17api_key = "My API Key"
18
19
20class TestModels:
21strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
22loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
23parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
24
25@parametrize
26def test_method_retrieve(self, client: OpenAI) -> None:
27model = client.models.retrieve(
28"gpt-3.5-turbo",
29)
30assert_matches_type(Model, model, path=["response"])
31
32@parametrize
33def test_raw_response_retrieve(self, client: OpenAI) -> None:
34response = client.models.with_raw_response.retrieve(
35"gpt-3.5-turbo",
36)
86379b44Stainless Bot2 years ago37
38assert response.is_closed is True
08b8179aDavid Schnurr2 years ago39assert response.http_request.headers.get("X-Stainless-Lang") == "python"
40model = response.parse()
41assert_matches_type(Model, model, path=["response"])
42
86379b44Stainless Bot2 years ago43@parametrize
44def test_streaming_response_retrieve(self, client: OpenAI) -> None:
45with client.models.with_streaming_response.retrieve(
46"gpt-3.5-turbo",
47) as response:
48assert not response.is_closed
49assert response.http_request.headers.get("X-Stainless-Lang") == "python"
50
51model = response.parse()
52assert_matches_type(Model, model, path=["response"])
53
54assert cast(Any, response.is_closed) is True
55
023a4e66Stainless Bot2 years ago56@parametrize
57def test_path_params_retrieve(self, client: OpenAI) -> None:
58with pytest.raises(ValueError, match=r"Expected a non-empty value for `model` but received ''"):
59client.models.with_raw_response.retrieve(
60"",
61)
62
08b8179aDavid Schnurr2 years ago63@parametrize
64def test_method_list(self, client: OpenAI) -> None:
65model = client.models.list()
66assert_matches_type(SyncPage[Model], model, path=["response"])
67
68@parametrize
69def test_raw_response_list(self, client: OpenAI) -> None:
70response = client.models.with_raw_response.list()
86379b44Stainless Bot2 years ago71
72assert response.is_closed is True
08b8179aDavid Schnurr2 years ago73assert response.http_request.headers.get("X-Stainless-Lang") == "python"
74model = response.parse()
75assert_matches_type(SyncPage[Model], model, path=["response"])
76
86379b44Stainless Bot2 years ago77@parametrize
78def test_streaming_response_list(self, client: OpenAI) -> None:
79with client.models.with_streaming_response.list() as response:
80assert not response.is_closed
81assert response.http_request.headers.get("X-Stainless-Lang") == "python"
82
83model = response.parse()
84assert_matches_type(SyncPage[Model], model, path=["response"])
85
86assert cast(Any, response.is_closed) is True
87
08b8179aDavid Schnurr2 years ago88@parametrize
89def test_method_delete(self, client: OpenAI) -> None:
90model = client.models.delete(
91"ft:gpt-3.5-turbo:acemeco:suffix:abc123",
92)
93assert_matches_type(ModelDeleted, model, path=["response"])
94
95@parametrize
96def test_raw_response_delete(self, client: OpenAI) -> None:
97response = client.models.with_raw_response.delete(
98"ft:gpt-3.5-turbo:acemeco:suffix:abc123",
99)
86379b44Stainless Bot2 years ago100
101assert response.is_closed is True
08b8179aDavid Schnurr2 years ago102assert response.http_request.headers.get("X-Stainless-Lang") == "python"
103model = response.parse()
104assert_matches_type(ModelDeleted, model, path=["response"])
105
86379b44Stainless Bot2 years ago106@parametrize
107def test_streaming_response_delete(self, client: OpenAI) -> None:
108with client.models.with_streaming_response.delete(
109"ft:gpt-3.5-turbo:acemeco:suffix:abc123",
110) as response:
111assert not response.is_closed
112assert response.http_request.headers.get("X-Stainless-Lang") == "python"
113
114model = response.parse()
115assert_matches_type(ModelDeleted, model, path=["response"])
116
117assert cast(Any, response.is_closed) is True
118
023a4e66Stainless Bot2 years ago119@parametrize
120def test_path_params_delete(self, client: OpenAI) -> None:
121with pytest.raises(ValueError, match=r"Expected a non-empty value for `model` but received ''"):
122client.models.with_raw_response.delete(
123"",
124)
125
08b8179aDavid Schnurr2 years ago126
127class TestAsyncModels:
128strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
129loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
130parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
131
132@parametrize
133async def test_method_retrieve(self, client: AsyncOpenAI) -> None:
134model = await client.models.retrieve(
135"gpt-3.5-turbo",
136)
137assert_matches_type(Model, model, path=["response"])
138
139@parametrize
140async def test_raw_response_retrieve(self, client: AsyncOpenAI) -> None:
141response = await client.models.with_raw_response.retrieve(
142"gpt-3.5-turbo",
143)
86379b44Stainless Bot2 years ago144
145assert response.is_closed is True
08b8179aDavid Schnurr2 years ago146assert response.http_request.headers.get("X-Stainless-Lang") == "python"
147model = response.parse()
148assert_matches_type(Model, model, path=["response"])
149
86379b44Stainless Bot2 years ago150@parametrize
151async def test_streaming_response_retrieve(self, client: AsyncOpenAI) -> None:
152async with client.models.with_streaming_response.retrieve(
153"gpt-3.5-turbo",
154) as response:
155assert not response.is_closed
156assert response.http_request.headers.get("X-Stainless-Lang") == "python"
157
158model = await response.parse()
159assert_matches_type(Model, model, path=["response"])
160
161assert cast(Any, response.is_closed) is True
162
023a4e66Stainless Bot2 years ago163@parametrize
164async def test_path_params_retrieve(self, client: AsyncOpenAI) -> None:
165with pytest.raises(ValueError, match=r"Expected a non-empty value for `model` but received ''"):
166await client.models.with_raw_response.retrieve(
167"",
168)
169
08b8179aDavid Schnurr2 years ago170@parametrize
171async def test_method_list(self, client: AsyncOpenAI) -> None:
172model = await client.models.list()
173assert_matches_type(AsyncPage[Model], model, path=["response"])
174
175@parametrize
176async def test_raw_response_list(self, client: AsyncOpenAI) -> None:
177response = await client.models.with_raw_response.list()
86379b44Stainless Bot2 years ago178
179assert response.is_closed is True
08b8179aDavid Schnurr2 years ago180assert response.http_request.headers.get("X-Stainless-Lang") == "python"
181model = response.parse()
182assert_matches_type(AsyncPage[Model], model, path=["response"])
183
86379b44Stainless Bot2 years ago184@parametrize
185async def test_streaming_response_list(self, client: AsyncOpenAI) -> None:
186async with client.models.with_streaming_response.list() as response:
187assert not response.is_closed
188assert response.http_request.headers.get("X-Stainless-Lang") == "python"
189
190model = await response.parse()
191assert_matches_type(AsyncPage[Model], model, path=["response"])
192
193assert cast(Any, response.is_closed) is True
194
08b8179aDavid Schnurr2 years ago195@parametrize
196async def test_method_delete(self, client: AsyncOpenAI) -> None:
197model = await client.models.delete(
198"ft:gpt-3.5-turbo:acemeco:suffix:abc123",
199)
200assert_matches_type(ModelDeleted, model, path=["response"])
201
202@parametrize
203async def test_raw_response_delete(self, client: AsyncOpenAI) -> None:
204response = await client.models.with_raw_response.delete(
205"ft:gpt-3.5-turbo:acemeco:suffix:abc123",
206)
86379b44Stainless Bot2 years ago207
208assert response.is_closed is True
08b8179aDavid Schnurr2 years ago209assert response.http_request.headers.get("X-Stainless-Lang") == "python"
210model = response.parse()
211assert_matches_type(ModelDeleted, model, path=["response"])
86379b44Stainless Bot2 years ago212
213@parametrize
214async def test_streaming_response_delete(self, client: AsyncOpenAI) -> None:
215async with client.models.with_streaming_response.delete(
216"ft:gpt-3.5-turbo:acemeco:suffix:abc123",
217) as response:
218assert not response.is_closed
219assert response.http_request.headers.get("X-Stainless-Lang") == "python"
220
221model = await response.parse()
222assert_matches_type(ModelDeleted, model, path=["response"])
223
224assert cast(Any, response.is_closed) is True
023a4e66Stainless Bot2 years ago225
226@parametrize
227async def test_path_params_delete(self, client: AsyncOpenAI) -> None:
228with pytest.raises(ValueError, match=r"Expected a non-empty value for `model` but received ''"):
229await client.models.with_raw_response.delete(
230"",
231)