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