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