openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
d9e9d7adbf2f3c615a9ba2125974cf8b8ee6a91d

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/test_models.py

231lines · modecode

1# File generated from our OpenAPI spec by Stainless.
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 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:
21 strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
22 loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
23 parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
24
25 @parametrize
26 def test_method_retrieve(self, client: OpenAI) -> None:
27 model = client.models.retrieve(
28 "gpt-3.5-turbo",
29 )
30 assert_matches_type(Model, model, path=["response"])
31
32 @parametrize
33 def test_raw_response_retrieve(self, client: OpenAI) -> None:
34 response = client.models.with_raw_response.retrieve(
35 "gpt-3.5-turbo",
36 )
37
38 assert response.is_closed is True
39 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
40 model = response.parse()
41 assert_matches_type(Model, model, path=["response"])
42
43 @parametrize
44 def test_streaming_response_retrieve(self, client: OpenAI) -> None:
45 with client.models.with_streaming_response.retrieve(
46 "gpt-3.5-turbo",
47 ) as response:
48 assert not response.is_closed
49 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
50
51 model = response.parse()
52 assert_matches_type(Model, model, path=["response"])
53
54 assert cast(Any, response.is_closed) is True
55
56 @parametrize
57 def test_path_params_retrieve(self, client: OpenAI) -> None:
58 with pytest.raises(ValueError, match=r"Expected a non-empty value for `model` but received ''"):
59 client.models.with_raw_response.retrieve(
60 "",
61 )
62
63 @parametrize
64 def test_method_list(self, client: OpenAI) -> None:
65 model = client.models.list()
66 assert_matches_type(SyncPage[Model], model, path=["response"])
67
68 @parametrize
69 def test_raw_response_list(self, client: OpenAI) -> None:
70 response = client.models.with_raw_response.list()
71
72 assert response.is_closed is True
73 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
74 model = response.parse()
75 assert_matches_type(SyncPage[Model], model, path=["response"])
76
77 @parametrize
78 def test_streaming_response_list(self, client: OpenAI) -> None:
79 with client.models.with_streaming_response.list() as response:
80 assert not response.is_closed
81 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
82
83 model = response.parse()
84 assert_matches_type(SyncPage[Model], model, path=["response"])
85
86 assert cast(Any, response.is_closed) is True
87
88 @parametrize
89 def test_method_delete(self, client: OpenAI) -> None:
90 model = client.models.delete(
91 "ft:gpt-3.5-turbo:acemeco:suffix:abc123",
92 )
93 assert_matches_type(ModelDeleted, model, path=["response"])
94
95 @parametrize
96 def test_raw_response_delete(self, client: OpenAI) -> None:
97 response = client.models.with_raw_response.delete(
98 "ft:gpt-3.5-turbo:acemeco:suffix:abc123",
99 )
100
101 assert response.is_closed is True
102 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
103 model = response.parse()
104 assert_matches_type(ModelDeleted, model, path=["response"])
105
106 @parametrize
107 def test_streaming_response_delete(self, client: OpenAI) -> None:
108 with client.models.with_streaming_response.delete(
109 "ft:gpt-3.5-turbo:acemeco:suffix:abc123",
110 ) as response:
111 assert not response.is_closed
112 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
113
114 model = response.parse()
115 assert_matches_type(ModelDeleted, model, path=["response"])
116
117 assert cast(Any, response.is_closed) is True
118
119 @parametrize
120 def test_path_params_delete(self, client: OpenAI) -> None:
121 with pytest.raises(ValueError, match=r"Expected a non-empty value for `model` but received ''"):
122 client.models.with_raw_response.delete(
123 "",
124 )
125
126
127class TestAsyncModels:
128 strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
129 loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
130 parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
131
132 @parametrize
133 async def test_method_retrieve(self, client: AsyncOpenAI) -> None:
134 model = await client.models.retrieve(
135 "gpt-3.5-turbo",
136 )
137 assert_matches_type(Model, model, path=["response"])
138
139 @parametrize
140 async def test_raw_response_retrieve(self, client: AsyncOpenAI) -> None:
141 response = await client.models.with_raw_response.retrieve(
142 "gpt-3.5-turbo",
143 )
144
145 assert response.is_closed is True
146 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
147 model = response.parse()
148 assert_matches_type(Model, model, path=["response"])
149
150 @parametrize
151 async def test_streaming_response_retrieve(self, client: AsyncOpenAI) -> None:
152 async with client.models.with_streaming_response.retrieve(
153 "gpt-3.5-turbo",
154 ) as response:
155 assert not response.is_closed
156 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
157
158 model = await response.parse()
159 assert_matches_type(Model, model, path=["response"])
160
161 assert cast(Any, response.is_closed) is True
162
163 @parametrize
164 async def test_path_params_retrieve(self, client: AsyncOpenAI) -> None:
165 with pytest.raises(ValueError, match=r"Expected a non-empty value for `model` but received ''"):
166 await client.models.with_raw_response.retrieve(
167 "",
168 )
169
170 @parametrize
171 async def test_method_list(self, client: AsyncOpenAI) -> None:
172 model = await client.models.list()
173 assert_matches_type(AsyncPage[Model], model, path=["response"])
174
175 @parametrize
176 async def test_raw_response_list(self, client: AsyncOpenAI) -> None:
177 response = await client.models.with_raw_response.list()
178
179 assert response.is_closed is True
180 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
181 model = response.parse()
182 assert_matches_type(AsyncPage[Model], model, path=["response"])
183
184 @parametrize
185 async def test_streaming_response_list(self, client: AsyncOpenAI) -> None:
186 async with client.models.with_streaming_response.list() as response:
187 assert not response.is_closed
188 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
189
190 model = await response.parse()
191 assert_matches_type(AsyncPage[Model], model, path=["response"])
192
193 assert cast(Any, response.is_closed) is True
194
195 @parametrize
196 async def test_method_delete(self, client: AsyncOpenAI) -> None:
197 model = await client.models.delete(
198 "ft:gpt-3.5-turbo:acemeco:suffix:abc123",
199 )
200 assert_matches_type(ModelDeleted, model, path=["response"])
201
202 @parametrize
203 async def test_raw_response_delete(self, client: AsyncOpenAI) -> None:
204 response = await client.models.with_raw_response.delete(
205 "ft:gpt-3.5-turbo:acemeco:suffix:abc123",
206 )
207
208 assert response.is_closed is True
209 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
210 model = response.parse()
211 assert_matches_type(ModelDeleted, model, path=["response"])
212
213 @parametrize
214 async def test_streaming_response_delete(self, client: AsyncOpenAI) -> None:
215 async with client.models.with_streaming_response.delete(
216 "ft:gpt-3.5-turbo:acemeco:suffix:abc123",
217 ) as response:
218 assert not response.is_closed
219 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
220
221 model = await response.parse()
222 assert_matches_type(ModelDeleted, model, path=["response"])
223
224 assert cast(Any, response.is_closed) is True
225
226 @parametrize
227 async def test_path_params_delete(self, client: AsyncOpenAI) -> None:
228 with pytest.raises(ValueError, match=r"Expected a non-empty value for `model` but received ''"):
229 await client.models.with_raw_response.delete(
230 "",
231 )
232