openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.57.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/fine_tuning/test_jobs.py

460lines · 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.pagination import SyncCursorPage, AsyncCursorPage
13from openai.types.fine_tuning import (
14 FineTuningJob,
15 FineTuningJobEvent,
16)
17
18base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
19
20
21class TestJobs:
22 parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
23
24 @parametrize
25 def test_method_create(self, client: OpenAI) -> None:
26 job = client.fine_tuning.jobs.create(
27 model="gpt-4o-mini",
28 training_file="file-abc123",
29 )
30 assert_matches_type(FineTuningJob, job, path=["response"])
31
32 @parametrize
33 def test_method_create_with_all_params(self, client: OpenAI) -> None:
34 job = client.fine_tuning.jobs.create(
35 model="gpt-4o-mini",
36 training_file="file-abc123",
37 hyperparameters={
38 "batch_size": "auto",
39 "learning_rate_multiplier": "auto",
40 "n_epochs": "auto",
41 },
42 integrations=[
43 {
44 "type": "wandb",
45 "wandb": {
46 "project": "my-wandb-project",
47 "entity": "entity",
48 "name": "name",
49 "tags": ["custom-tag"],
50 },
51 }
52 ],
53 seed=42,
54 suffix="x",
55 validation_file="file-abc123",
56 )
57 assert_matches_type(FineTuningJob, job, path=["response"])
58
59 @parametrize
60 def test_raw_response_create(self, client: OpenAI) -> None:
61 response = client.fine_tuning.jobs.with_raw_response.create(
62 model="gpt-4o-mini",
63 training_file="file-abc123",
64 )
65
66 assert response.is_closed is True
67 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
68 job = response.parse()
69 assert_matches_type(FineTuningJob, job, path=["response"])
70
71 @parametrize
72 def test_streaming_response_create(self, client: OpenAI) -> None:
73 with client.fine_tuning.jobs.with_streaming_response.create(
74 model="gpt-4o-mini",
75 training_file="file-abc123",
76 ) as response:
77 assert not response.is_closed
78 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
79
80 job = response.parse()
81 assert_matches_type(FineTuningJob, job, path=["response"])
82
83 assert cast(Any, response.is_closed) is True
84
85 @parametrize
86 def test_method_retrieve(self, client: OpenAI) -> None:
87 job = client.fine_tuning.jobs.retrieve(
88 "ft-AF1WoRqd3aJAHsqc9NY7iL8F",
89 )
90 assert_matches_type(FineTuningJob, job, path=["response"])
91
92 @parametrize
93 def test_raw_response_retrieve(self, client: OpenAI) -> None:
94 response = client.fine_tuning.jobs.with_raw_response.retrieve(
95 "ft-AF1WoRqd3aJAHsqc9NY7iL8F",
96 )
97
98 assert response.is_closed is True
99 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
100 job = response.parse()
101 assert_matches_type(FineTuningJob, job, path=["response"])
102
103 @parametrize
104 def test_streaming_response_retrieve(self, client: OpenAI) -> None:
105 with client.fine_tuning.jobs.with_streaming_response.retrieve(
106 "ft-AF1WoRqd3aJAHsqc9NY7iL8F",
107 ) as response:
108 assert not response.is_closed
109 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
110
111 job = response.parse()
112 assert_matches_type(FineTuningJob, job, path=["response"])
113
114 assert cast(Any, response.is_closed) is True
115
116 @parametrize
117 def test_path_params_retrieve(self, client: OpenAI) -> None:
118 with pytest.raises(ValueError, match=r"Expected a non-empty value for `fine_tuning_job_id` but received ''"):
119 client.fine_tuning.jobs.with_raw_response.retrieve(
120 "",
121 )
122
123 @parametrize
124 def test_method_list(self, client: OpenAI) -> None:
125 job = client.fine_tuning.jobs.list()
126 assert_matches_type(SyncCursorPage[FineTuningJob], job, path=["response"])
127
128 @parametrize
129 def test_method_list_with_all_params(self, client: OpenAI) -> None:
130 job = client.fine_tuning.jobs.list(
131 after="string",
132 limit=0,
133 )
134 assert_matches_type(SyncCursorPage[FineTuningJob], job, path=["response"])
135
136 @parametrize
137 def test_raw_response_list(self, client: OpenAI) -> None:
138 response = client.fine_tuning.jobs.with_raw_response.list()
139
140 assert response.is_closed is True
141 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
142 job = response.parse()
143 assert_matches_type(SyncCursorPage[FineTuningJob], job, path=["response"])
144
145 @parametrize
146 def test_streaming_response_list(self, client: OpenAI) -> None:
147 with client.fine_tuning.jobs.with_streaming_response.list() as response:
148 assert not response.is_closed
149 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
150
151 job = response.parse()
152 assert_matches_type(SyncCursorPage[FineTuningJob], job, path=["response"])
153
154 assert cast(Any, response.is_closed) is True
155
156 @parametrize
157 def test_method_cancel(self, client: OpenAI) -> None:
158 job = client.fine_tuning.jobs.cancel(
159 "ft-AF1WoRqd3aJAHsqc9NY7iL8F",
160 )
161 assert_matches_type(FineTuningJob, job, path=["response"])
162
163 @parametrize
164 def test_raw_response_cancel(self, client: OpenAI) -> None:
165 response = client.fine_tuning.jobs.with_raw_response.cancel(
166 "ft-AF1WoRqd3aJAHsqc9NY7iL8F",
167 )
168
169 assert response.is_closed is True
170 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
171 job = response.parse()
172 assert_matches_type(FineTuningJob, job, path=["response"])
173
174 @parametrize
175 def test_streaming_response_cancel(self, client: OpenAI) -> None:
176 with client.fine_tuning.jobs.with_streaming_response.cancel(
177 "ft-AF1WoRqd3aJAHsqc9NY7iL8F",
178 ) as response:
179 assert not response.is_closed
180 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
181
182 job = response.parse()
183 assert_matches_type(FineTuningJob, job, path=["response"])
184
185 assert cast(Any, response.is_closed) is True
186
187 @parametrize
188 def test_path_params_cancel(self, client: OpenAI) -> None:
189 with pytest.raises(ValueError, match=r"Expected a non-empty value for `fine_tuning_job_id` but received ''"):
190 client.fine_tuning.jobs.with_raw_response.cancel(
191 "",
192 )
193
194 @parametrize
195 def test_method_list_events(self, client: OpenAI) -> None:
196 job = client.fine_tuning.jobs.list_events(
197 "ft-AF1WoRqd3aJAHsqc9NY7iL8F",
198 )
199 assert_matches_type(SyncCursorPage[FineTuningJobEvent], job, path=["response"])
200
201 @parametrize
202 def test_method_list_events_with_all_params(self, client: OpenAI) -> None:
203 job = client.fine_tuning.jobs.list_events(
204 "ft-AF1WoRqd3aJAHsqc9NY7iL8F",
205 after="string",
206 limit=0,
207 )
208 assert_matches_type(SyncCursorPage[FineTuningJobEvent], job, path=["response"])
209
210 @parametrize
211 def test_raw_response_list_events(self, client: OpenAI) -> None:
212 response = client.fine_tuning.jobs.with_raw_response.list_events(
213 "ft-AF1WoRqd3aJAHsqc9NY7iL8F",
214 )
215
216 assert response.is_closed is True
217 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
218 job = response.parse()
219 assert_matches_type(SyncCursorPage[FineTuningJobEvent], job, path=["response"])
220
221 @parametrize
222 def test_streaming_response_list_events(self, client: OpenAI) -> None:
223 with client.fine_tuning.jobs.with_streaming_response.list_events(
224 "ft-AF1WoRqd3aJAHsqc9NY7iL8F",
225 ) as response:
226 assert not response.is_closed
227 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
228
229 job = response.parse()
230 assert_matches_type(SyncCursorPage[FineTuningJobEvent], job, path=["response"])
231
232 assert cast(Any, response.is_closed) is True
233
234 @parametrize
235 def test_path_params_list_events(self, client: OpenAI) -> None:
236 with pytest.raises(ValueError, match=r"Expected a non-empty value for `fine_tuning_job_id` but received ''"):
237 client.fine_tuning.jobs.with_raw_response.list_events(
238 "",
239 )
240
241
242class TestAsyncJobs:
243 parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
244
245 @parametrize
246 async def test_method_create(self, async_client: AsyncOpenAI) -> None:
247 job = await async_client.fine_tuning.jobs.create(
248 model="gpt-4o-mini",
249 training_file="file-abc123",
250 )
251 assert_matches_type(FineTuningJob, job, path=["response"])
252
253 @parametrize
254 async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
255 job = await async_client.fine_tuning.jobs.create(
256 model="gpt-4o-mini",
257 training_file="file-abc123",
258 hyperparameters={
259 "batch_size": "auto",
260 "learning_rate_multiplier": "auto",
261 "n_epochs": "auto",
262 },
263 integrations=[
264 {
265 "type": "wandb",
266 "wandb": {
267 "project": "my-wandb-project",
268 "entity": "entity",
269 "name": "name",
270 "tags": ["custom-tag"],
271 },
272 }
273 ],
274 seed=42,
275 suffix="x",
276 validation_file="file-abc123",
277 )
278 assert_matches_type(FineTuningJob, job, path=["response"])
279
280 @parametrize
281 async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
282 response = await async_client.fine_tuning.jobs.with_raw_response.create(
283 model="gpt-4o-mini",
284 training_file="file-abc123",
285 )
286
287 assert response.is_closed is True
288 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
289 job = response.parse()
290 assert_matches_type(FineTuningJob, job, path=["response"])
291
292 @parametrize
293 async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
294 async with async_client.fine_tuning.jobs.with_streaming_response.create(
295 model="gpt-4o-mini",
296 training_file="file-abc123",
297 ) as response:
298 assert not response.is_closed
299 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
300
301 job = await response.parse()
302 assert_matches_type(FineTuningJob, job, path=["response"])
303
304 assert cast(Any, response.is_closed) is True
305
306 @parametrize
307 async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None:
308 job = await async_client.fine_tuning.jobs.retrieve(
309 "ft-AF1WoRqd3aJAHsqc9NY7iL8F",
310 )
311 assert_matches_type(FineTuningJob, job, path=["response"])
312
313 @parametrize
314 async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None:
315 response = await async_client.fine_tuning.jobs.with_raw_response.retrieve(
316 "ft-AF1WoRqd3aJAHsqc9NY7iL8F",
317 )
318
319 assert response.is_closed is True
320 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
321 job = response.parse()
322 assert_matches_type(FineTuningJob, job, path=["response"])
323
324 @parametrize
325 async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None:
326 async with async_client.fine_tuning.jobs.with_streaming_response.retrieve(
327 "ft-AF1WoRqd3aJAHsqc9NY7iL8F",
328 ) as response:
329 assert not response.is_closed
330 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
331
332 job = await response.parse()
333 assert_matches_type(FineTuningJob, job, path=["response"])
334
335 assert cast(Any, response.is_closed) is True
336
337 @parametrize
338 async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None:
339 with pytest.raises(ValueError, match=r"Expected a non-empty value for `fine_tuning_job_id` but received ''"):
340 await async_client.fine_tuning.jobs.with_raw_response.retrieve(
341 "",
342 )
343
344 @parametrize
345 async def test_method_list(self, async_client: AsyncOpenAI) -> None:
346 job = await async_client.fine_tuning.jobs.list()
347 assert_matches_type(AsyncCursorPage[FineTuningJob], job, path=["response"])
348
349 @parametrize
350 async def test_method_list_with_all_params(self, async_client: AsyncOpenAI) -> None:
351 job = await async_client.fine_tuning.jobs.list(
352 after="string",
353 limit=0,
354 )
355 assert_matches_type(AsyncCursorPage[FineTuningJob], job, path=["response"])
356
357 @parametrize
358 async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None:
359 response = await async_client.fine_tuning.jobs.with_raw_response.list()
360
361 assert response.is_closed is True
362 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
363 job = response.parse()
364 assert_matches_type(AsyncCursorPage[FineTuningJob], job, path=["response"])
365
366 @parametrize
367 async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None:
368 async with async_client.fine_tuning.jobs.with_streaming_response.list() as response:
369 assert not response.is_closed
370 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
371
372 job = await response.parse()
373 assert_matches_type(AsyncCursorPage[FineTuningJob], job, path=["response"])
374
375 assert cast(Any, response.is_closed) is True
376
377 @parametrize
378 async def test_method_cancel(self, async_client: AsyncOpenAI) -> None:
379 job = await async_client.fine_tuning.jobs.cancel(
380 "ft-AF1WoRqd3aJAHsqc9NY7iL8F",
381 )
382 assert_matches_type(FineTuningJob, job, path=["response"])
383
384 @parametrize
385 async def test_raw_response_cancel(self, async_client: AsyncOpenAI) -> None:
386 response = await async_client.fine_tuning.jobs.with_raw_response.cancel(
387 "ft-AF1WoRqd3aJAHsqc9NY7iL8F",
388 )
389
390 assert response.is_closed is True
391 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
392 job = response.parse()
393 assert_matches_type(FineTuningJob, job, path=["response"])
394
395 @parametrize
396 async def test_streaming_response_cancel(self, async_client: AsyncOpenAI) -> None:
397 async with async_client.fine_tuning.jobs.with_streaming_response.cancel(
398 "ft-AF1WoRqd3aJAHsqc9NY7iL8F",
399 ) as response:
400 assert not response.is_closed
401 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
402
403 job = await response.parse()
404 assert_matches_type(FineTuningJob, job, path=["response"])
405
406 assert cast(Any, response.is_closed) is True
407
408 @parametrize
409 async def test_path_params_cancel(self, async_client: AsyncOpenAI) -> None:
410 with pytest.raises(ValueError, match=r"Expected a non-empty value for `fine_tuning_job_id` but received ''"):
411 await async_client.fine_tuning.jobs.with_raw_response.cancel(
412 "",
413 )
414
415 @parametrize
416 async def test_method_list_events(self, async_client: AsyncOpenAI) -> None:
417 job = await async_client.fine_tuning.jobs.list_events(
418 "ft-AF1WoRqd3aJAHsqc9NY7iL8F",
419 )
420 assert_matches_type(AsyncCursorPage[FineTuningJobEvent], job, path=["response"])
421
422 @parametrize
423 async def test_method_list_events_with_all_params(self, async_client: AsyncOpenAI) -> None:
424 job = await async_client.fine_tuning.jobs.list_events(
425 "ft-AF1WoRqd3aJAHsqc9NY7iL8F",
426 after="string",
427 limit=0,
428 )
429 assert_matches_type(AsyncCursorPage[FineTuningJobEvent], job, path=["response"])
430
431 @parametrize
432 async def test_raw_response_list_events(self, async_client: AsyncOpenAI) -> None:
433 response = await async_client.fine_tuning.jobs.with_raw_response.list_events(
434 "ft-AF1WoRqd3aJAHsqc9NY7iL8F",
435 )
436
437 assert response.is_closed is True
438 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
439 job = response.parse()
440 assert_matches_type(AsyncCursorPage[FineTuningJobEvent], job, path=["response"])
441
442 @parametrize
443 async def test_streaming_response_list_events(self, async_client: AsyncOpenAI) -> None:
444 async with async_client.fine_tuning.jobs.with_streaming_response.list_events(
445 "ft-AF1WoRqd3aJAHsqc9NY7iL8F",
446 ) as response:
447 assert not response.is_closed
448 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
449
450 job = await response.parse()
451 assert_matches_type(AsyncCursorPage[FineTuningJobEvent], job, path=["response"])
452
453 assert cast(Any, response.is_closed) is True
454
455 @parametrize
456 async def test_path_params_list_events(self, async_client: AsyncOpenAI) -> None:
457 with pytest.raises(ValueError, match=r"Expected a non-empty value for `fine_tuning_job_id` but received ''"):
458 await async_client.fine_tuning.jobs.with_raw_response.list_events(
459 "",
460 )
461