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