openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.23.6

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/fine_tuning/test_jobs.py

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