openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/fine_tuning/test_jobs.py
251lines · modecode
| 1 | # File generated from our OpenAPI spec by Stainless. |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import os |
| 6 | |
| 7 | import pytest |
| 8 | |
| 9 | from openai import OpenAI, AsyncOpenAI |
| 10 | from tests.utils import assert_matches_type |
| 11 | from openai._client import OpenAI, AsyncOpenAI |
| 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 | api_key = "My API Key" |
| 20 | |
| 21 | |
| 22 | class TestJobs: |
| 23 | strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 24 | loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 25 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 26 | |
| 27 | @parametrize |
| 28 | def test_method_create(self, client: OpenAI) -> None: |
| 29 | job = client.fine_tuning.jobs.create( |
| 30 | model="gpt-3.5-turbo", |
| 31 | training_file="file-abc123", |
| 32 | ) |
| 33 | assert_matches_type(FineTuningJob, job, path=["response"]) |
| 34 | |
| 35 | @parametrize |
| 36 | def test_method_create_with_all_params(self, client: OpenAI) -> None: |
| 37 | job = client.fine_tuning.jobs.create( |
| 38 | model="gpt-3.5-turbo", |
| 39 | training_file="file-abc123", |
| 40 | hyperparameters={ |
| 41 | "batch_size": "auto", |
| 42 | "learning_rate_multiplier": "auto", |
| 43 | "n_epochs": "auto", |
| 44 | }, |
| 45 | suffix="x", |
| 46 | validation_file="file-abc123", |
| 47 | ) |
| 48 | assert_matches_type(FineTuningJob, job, path=["response"]) |
| 49 | |
| 50 | @parametrize |
| 51 | def test_raw_response_create(self, client: OpenAI) -> None: |
| 52 | response = client.fine_tuning.jobs.with_raw_response.create( |
| 53 | model="gpt-3.5-turbo", |
| 54 | training_file="file-abc123", |
| 55 | ) |
| 56 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 57 | job = response.parse() |
| 58 | assert_matches_type(FineTuningJob, job, path=["response"]) |
| 59 | |
| 60 | @parametrize |
| 61 | def test_method_retrieve(self, client: OpenAI) -> None: |
| 62 | job = client.fine_tuning.jobs.retrieve( |
| 63 | "ft-AF1WoRqd3aJAHsqc9NY7iL8F", |
| 64 | ) |
| 65 | assert_matches_type(FineTuningJob, job, path=["response"]) |
| 66 | |
| 67 | @parametrize |
| 68 | def test_raw_response_retrieve(self, client: OpenAI) -> None: |
| 69 | response = client.fine_tuning.jobs.with_raw_response.retrieve( |
| 70 | "ft-AF1WoRqd3aJAHsqc9NY7iL8F", |
| 71 | ) |
| 72 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 73 | job = response.parse() |
| 74 | assert_matches_type(FineTuningJob, job, path=["response"]) |
| 75 | |
| 76 | @parametrize |
| 77 | def test_method_list(self, client: OpenAI) -> None: |
| 78 | job = client.fine_tuning.jobs.list() |
| 79 | assert_matches_type(SyncCursorPage[FineTuningJob], job, path=["response"]) |
| 80 | |
| 81 | @parametrize |
| 82 | def test_method_list_with_all_params(self, client: OpenAI) -> None: |
| 83 | job = client.fine_tuning.jobs.list( |
| 84 | after="string", |
| 85 | limit=0, |
| 86 | ) |
| 87 | assert_matches_type(SyncCursorPage[FineTuningJob], job, path=["response"]) |
| 88 | |
| 89 | @parametrize |
| 90 | def test_raw_response_list(self, client: OpenAI) -> None: |
| 91 | response = client.fine_tuning.jobs.with_raw_response.list() |
| 92 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 93 | job = response.parse() |
| 94 | assert_matches_type(SyncCursorPage[FineTuningJob], job, path=["response"]) |
| 95 | |
| 96 | @parametrize |
| 97 | def test_method_cancel(self, client: OpenAI) -> None: |
| 98 | job = client.fine_tuning.jobs.cancel( |
| 99 | "ft-AF1WoRqd3aJAHsqc9NY7iL8F", |
| 100 | ) |
| 101 | assert_matches_type(FineTuningJob, job, path=["response"]) |
| 102 | |
| 103 | @parametrize |
| 104 | def test_raw_response_cancel(self, client: OpenAI) -> None: |
| 105 | response = client.fine_tuning.jobs.with_raw_response.cancel( |
| 106 | "ft-AF1WoRqd3aJAHsqc9NY7iL8F", |
| 107 | ) |
| 108 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 109 | job = response.parse() |
| 110 | assert_matches_type(FineTuningJob, job, path=["response"]) |
| 111 | |
| 112 | @parametrize |
| 113 | def test_method_list_events(self, client: OpenAI) -> None: |
| 114 | job = client.fine_tuning.jobs.list_events( |
| 115 | "ft-AF1WoRqd3aJAHsqc9NY7iL8F", |
| 116 | ) |
| 117 | assert_matches_type(SyncCursorPage[FineTuningJobEvent], job, path=["response"]) |
| 118 | |
| 119 | @parametrize |
| 120 | def test_method_list_events_with_all_params(self, client: OpenAI) -> None: |
| 121 | job = client.fine_tuning.jobs.list_events( |
| 122 | "ft-AF1WoRqd3aJAHsqc9NY7iL8F", |
| 123 | after="string", |
| 124 | limit=0, |
| 125 | ) |
| 126 | assert_matches_type(SyncCursorPage[FineTuningJobEvent], job, path=["response"]) |
| 127 | |
| 128 | @parametrize |
| 129 | def test_raw_response_list_events(self, client: OpenAI) -> None: |
| 130 | response = client.fine_tuning.jobs.with_raw_response.list_events( |
| 131 | "ft-AF1WoRqd3aJAHsqc9NY7iL8F", |
| 132 | ) |
| 133 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 134 | job = response.parse() |
| 135 | assert_matches_type(SyncCursorPage[FineTuningJobEvent], job, path=["response"]) |
| 136 | |
| 137 | |
| 138 | class TestAsyncJobs: |
| 139 | strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 140 | loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 141 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 142 | |
| 143 | @parametrize |
| 144 | async def test_method_create(self, client: AsyncOpenAI) -> None: |
| 145 | job = await client.fine_tuning.jobs.create( |
| 146 | model="gpt-3.5-turbo", |
| 147 | training_file="file-abc123", |
| 148 | ) |
| 149 | assert_matches_type(FineTuningJob, job, path=["response"]) |
| 150 | |
| 151 | @parametrize |
| 152 | async def test_method_create_with_all_params(self, client: AsyncOpenAI) -> None: |
| 153 | job = await client.fine_tuning.jobs.create( |
| 154 | model="gpt-3.5-turbo", |
| 155 | training_file="file-abc123", |
| 156 | hyperparameters={ |
| 157 | "batch_size": "auto", |
| 158 | "learning_rate_multiplier": "auto", |
| 159 | "n_epochs": "auto", |
| 160 | }, |
| 161 | suffix="x", |
| 162 | validation_file="file-abc123", |
| 163 | ) |
| 164 | assert_matches_type(FineTuningJob, job, path=["response"]) |
| 165 | |
| 166 | @parametrize |
| 167 | async def test_raw_response_create(self, client: AsyncOpenAI) -> None: |
| 168 | response = await client.fine_tuning.jobs.with_raw_response.create( |
| 169 | model="gpt-3.5-turbo", |
| 170 | training_file="file-abc123", |
| 171 | ) |
| 172 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 173 | job = response.parse() |
| 174 | assert_matches_type(FineTuningJob, job, path=["response"]) |
| 175 | |
| 176 | @parametrize |
| 177 | async def test_method_retrieve(self, client: AsyncOpenAI) -> None: |
| 178 | job = await client.fine_tuning.jobs.retrieve( |
| 179 | "ft-AF1WoRqd3aJAHsqc9NY7iL8F", |
| 180 | ) |
| 181 | assert_matches_type(FineTuningJob, job, path=["response"]) |
| 182 | |
| 183 | @parametrize |
| 184 | async def test_raw_response_retrieve(self, client: AsyncOpenAI) -> None: |
| 185 | response = await client.fine_tuning.jobs.with_raw_response.retrieve( |
| 186 | "ft-AF1WoRqd3aJAHsqc9NY7iL8F", |
| 187 | ) |
| 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 | async def test_method_list(self, client: AsyncOpenAI) -> None: |
| 194 | job = await client.fine_tuning.jobs.list() |
| 195 | assert_matches_type(AsyncCursorPage[FineTuningJob], job, path=["response"]) |
| 196 | |
| 197 | @parametrize |
| 198 | async def test_method_list_with_all_params(self, client: AsyncOpenAI) -> None: |
| 199 | job = await client.fine_tuning.jobs.list( |
| 200 | after="string", |
| 201 | limit=0, |
| 202 | ) |
| 203 | assert_matches_type(AsyncCursorPage[FineTuningJob], job, path=["response"]) |
| 204 | |
| 205 | @parametrize |
| 206 | async def test_raw_response_list(self, client: AsyncOpenAI) -> None: |
| 207 | response = await client.fine_tuning.jobs.with_raw_response.list() |
| 208 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 209 | job = response.parse() |
| 210 | assert_matches_type(AsyncCursorPage[FineTuningJob], job, path=["response"]) |
| 211 | |
| 212 | @parametrize |
| 213 | async def test_method_cancel(self, client: AsyncOpenAI) -> None: |
| 214 | job = await client.fine_tuning.jobs.cancel( |
| 215 | "ft-AF1WoRqd3aJAHsqc9NY7iL8F", |
| 216 | ) |
| 217 | assert_matches_type(FineTuningJob, job, path=["response"]) |
| 218 | |
| 219 | @parametrize |
| 220 | async def test_raw_response_cancel(self, client: AsyncOpenAI) -> None: |
| 221 | response = await client.fine_tuning.jobs.with_raw_response.cancel( |
| 222 | "ft-AF1WoRqd3aJAHsqc9NY7iL8F", |
| 223 | ) |
| 224 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 225 | job = response.parse() |
| 226 | assert_matches_type(FineTuningJob, job, path=["response"]) |
| 227 | |
| 228 | @parametrize |
| 229 | async def test_method_list_events(self, client: AsyncOpenAI) -> None: |
| 230 | job = await client.fine_tuning.jobs.list_events( |
| 231 | "ft-AF1WoRqd3aJAHsqc9NY7iL8F", |
| 232 | ) |
| 233 | assert_matches_type(AsyncCursorPage[FineTuningJobEvent], job, path=["response"]) |
| 234 | |
| 235 | @parametrize |
| 236 | async def test_method_list_events_with_all_params(self, client: AsyncOpenAI) -> None: |
| 237 | job = await client.fine_tuning.jobs.list_events( |
| 238 | "ft-AF1WoRqd3aJAHsqc9NY7iL8F", |
| 239 | after="string", |
| 240 | limit=0, |
| 241 | ) |
| 242 | assert_matches_type(AsyncCursorPage[FineTuningJobEvent], job, path=["response"]) |
| 243 | |
| 244 | @parametrize |
| 245 | async def test_raw_response_list_events(self, client: AsyncOpenAI) -> None: |
| 246 | response = await client.fine_tuning.jobs.with_raw_response.list_events( |
| 247 | "ft-AF1WoRqd3aJAHsqc9NY7iL8F", |
| 248 | ) |
| 249 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 250 | job = response.parse() |
| 251 | assert_matches_type(AsyncCursorPage[FineTuningJobEvent], job, path=["response"]) |
| 252 | |