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