forked from openai/openai-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfine-tunes.ts
423 lines (371 loc) · 14 KB
/
fine-tunes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// File generated from our OpenAPI spec by Stainless.
import * as Core from 'openai/core';
import { APIPromise } from 'openai/core';
import { APIResource } from 'openai/resource';
import * as FineTunesAPI from 'openai/resources/fine-tunes';
import * as FilesAPI from 'openai/resources/files';
import { Page } from 'openai/pagination';
import { Stream } from 'openai/streaming';
export class FineTunes extends APIResource {
/**
* Creates a job that fine-tunes a specified model from a given dataset.
*
* Response includes details of the enqueued job including job status and the name
* of the fine-tuned models once complete.
*
* [Learn more about fine-tuning](https://platform.openai.com/docs/guides/legacy-fine-tuning)
*/
create(body: FineTuneCreateParams, options?: Core.RequestOptions): Core.APIPromise<FineTune> {
return this.post('/fine-tunes', { body, ...options });
}
/**
* Gets info about the fine-tune job.
*
* [Learn more about fine-tuning](https://platform.openai.com/docs/guides/legacy-fine-tuning)
*/
retrieve(fineTuneId: string, options?: Core.RequestOptions): Core.APIPromise<FineTune> {
return this.get(`/fine-tunes/${fineTuneId}`, options);
}
/**
* List your organization's fine-tuning jobs
*/
list(options?: Core.RequestOptions): Core.PagePromise<FineTunesPage, FineTune> {
return this.getAPIList('/fine-tunes', FineTunesPage, options);
}
/**
* Immediately cancel a fine-tune job.
*/
cancel(fineTuneId: string, options?: Core.RequestOptions): Core.APIPromise<FineTune> {
return this.post(`/fine-tunes/${fineTuneId}/cancel`, options);
}
/**
* Get fine-grained status updates for a fine-tune job.
*/
listEvents(
fineTuneId: string,
query?: FineTuneListEventsParamsNonStreaming,
options?: Core.RequestOptions,
): APIPromise<FineTuneEventsListResponse>;
listEvents(
fineTuneId: string,
query: FineTuneListEventsParamsStreaming,
options?: Core.RequestOptions,
): APIPromise<Stream<FineTuneEvent>>;
listEvents(
fineTuneId: string,
query?: FineTuneListEventsParamsBase | undefined,
options?: Core.RequestOptions,
): APIPromise<Stream<FineTuneEvent> | FineTuneEventsListResponse>;
listEvents(
fineTuneId: string,
query?: FineTuneListEventsParams | undefined,
options?: Core.RequestOptions,
): APIPromise<FineTuneEventsListResponse> | APIPromise<Stream<FineTuneEvent>> {
return this.get(`/fine-tunes/${fineTuneId}/events`, {
query,
timeout: 86400000,
...options,
stream: query?.stream ?? false,
}) as APIPromise<FineTuneEventsListResponse> | APIPromise<Stream<FineTuneEvent>>;
}
}
/**
* Note: no pagination actually occurs yet, this is for forwards-compatibility.
*/
export class FineTunesPage extends Page<FineTune> {}
/**
* The `FineTune` object represents a legacy fine-tune job that has been created
* through the API.
*/
export interface FineTune {
/**
* The object identifier, which can be referenced in the API endpoints.
*/
id: string;
/**
* The Unix timestamp (in seconds) for when the fine-tuning job was created.
*/
created_at: number;
/**
* The name of the fine-tuned model that is being created.
*/
fine_tuned_model: string | null;
/**
* The hyperparameters used for the fine-tuning job. See the
* [fine-tuning guide](https://platform.openai.com/docs/guides/legacy-fine-tuning/hyperparameters)
* for more details.
*/
hyperparams: FineTune.Hyperparams;
/**
* The base model that is being fine-tuned.
*/
model: string;
/**
* The object type, which is always "fine-tune".
*/
object: 'fine-tune';
/**
* The organization that owns the fine-tuning job.
*/
organization_id: string;
/**
* The compiled results files for the fine-tuning job.
*/
result_files: Array<FilesAPI.FileObject>;
/**
* The current status of the fine-tuning job, which can be either `created`,
* `running`, `succeeded`, `failed`, or `cancelled`.
*/
status: string;
/**
* The list of files used for training.
*/
training_files: Array<FilesAPI.FileObject>;
/**
* The Unix timestamp (in seconds) for when the fine-tuning job was last updated.
*/
updated_at: number;
/**
* The list of files used for validation.
*/
validation_files: Array<FilesAPI.FileObject>;
/**
* The list of events that have been observed in the lifecycle of the FineTune job.
*/
events?: Array<FineTuneEvent>;
}
export namespace FineTune {
/**
* The hyperparameters used for the fine-tuning job. See the
* [fine-tuning guide](https://platform.openai.com/docs/guides/legacy-fine-tuning/hyperparameters)
* for more details.
*/
export interface Hyperparams {
/**
* The batch size to use for training. The batch size is the number of training
* examples used to train a single forward and backward pass.
*/
batch_size: number;
/**
* The learning rate multiplier to use for training.
*/
learning_rate_multiplier: number;
/**
* The number of epochs to train the model for. An epoch refers to one full cycle
* through the training dataset.
*/
n_epochs: number;
/**
* The weight to use for loss on the prompt tokens.
*/
prompt_loss_weight: number;
/**
* The number of classes to use for computing classification metrics.
*/
classification_n_classes?: number;
/**
* The positive class to use for computing classification metrics.
*/
classification_positive_class?: string;
/**
* The classification metrics to compute using the validation dataset at the end of
* every epoch.
*/
compute_classification_metrics?: boolean;
}
}
/**
* Fine-tune event object
*/
export interface FineTuneEvent {
created_at: number;
level: string;
message: string;
object: 'fine-tune-event';
}
export interface FineTuneEventsListResponse {
data: Array<FineTuneEvent>;
object: 'list';
}
export interface FineTuneCreateParams {
/**
* The ID of an uploaded file that contains training data.
*
* See [upload file](https://platform.openai.com/docs/api-reference/files/upload)
* for how to upload a file.
*
* Your dataset must be formatted as a JSONL file, where each training example is a
* JSON object with the keys "prompt" and "completion". Additionally, you must
* upload your file with the purpose `fine-tune`.
*
* See the
* [fine-tuning guide](https://platform.openai.com/docs/guides/legacy-fine-tuning/creating-training-data)
* for more details.
*/
training_file: string;
/**
* The batch size to use for training. The batch size is the number of training
* examples used to train a single forward and backward pass.
*
* By default, the batch size will be dynamically configured to be ~0.2% of the
* number of examples in the training set, capped at 256 - in general, we've found
* that larger batch sizes tend to work better for larger datasets.
*/
batch_size?: number | null;
/**
* If this is provided, we calculate F-beta scores at the specified beta values.
* The F-beta score is a generalization of F-1 score. This is only used for binary
* classification.
*
* With a beta of 1 (i.e. the F-1 score), precision and recall are given the same
* weight. A larger beta score puts more weight on recall and less on precision. A
* smaller beta score puts more weight on precision and less on recall.
*/
classification_betas?: Array<number> | null;
/**
* The number of classes in a classification task.
*
* This parameter is required for multiclass classification.
*/
classification_n_classes?: number | null;
/**
* The positive class in binary classification.
*
* This parameter is needed to generate precision, recall, and F1 metrics when
* doing binary classification.
*/
classification_positive_class?: string | null;
/**
* If set, we calculate classification-specific metrics such as accuracy and F-1
* score using the validation set at the end of every epoch. These metrics can be
* viewed in the
* [results file](https://platform.openai.com/docs/guides/legacy-fine-tuning/analyzing-your-fine-tuned-model).
*
* In order to compute classification metrics, you must provide a
* `validation_file`. Additionally, you must specify `classification_n_classes` for
* multiclass classification or `classification_positive_class` for binary
* classification.
*/
compute_classification_metrics?: boolean | null;
/**
* The hyperparameters used for the fine-tuning job.
*/
hyperparameters?: FineTuneCreateParams.Hyperparameters;
/**
* The learning rate multiplier to use for training. The fine-tuning learning rate
* is the original learning rate used for pretraining multiplied by this value.
*
* By default, the learning rate multiplier is the 0.05, 0.1, or 0.2 depending on
* final `batch_size` (larger learning rates tend to perform better with larger
* batch sizes). We recommend experimenting with values in the range 0.02 to 0.2 to
* see what produces the best results.
*/
learning_rate_multiplier?: number | null;
/**
* The name of the base model to fine-tune. You can select one of "ada", "babbage",
* "curie", "davinci", or a fine-tuned model created after 2022-04-21 and before
* 2023-08-22. To learn more about these models, see the
* [Models](https://platform.openai.com/docs/models) documentation.
*/
model?: (string & {}) | 'ada' | 'babbage' | 'curie' | 'davinci' | null;
/**
* The weight to use for loss on the prompt tokens. This controls how much the
* model tries to learn to generate the prompt (as compared to the completion which
* always has a weight of 1.0), and can add a stabilizing effect to training when
* completions are short.
*
* If prompts are extremely long (relative to completions), it may make sense to
* reduce this weight so as to avoid over-prioritizing learning the prompt.
*/
prompt_loss_weight?: number | null;
/**
* A string of up to 40 characters that will be added to your fine-tuned model
* name.
*
* For example, a `suffix` of "custom-model-name" would produce a model name like
* `ada:ft-your-org:custom-model-name-2022-02-15-04-21-04`.
*/
suffix?: string | null;
/**
* The ID of an uploaded file that contains validation data.
*
* If you provide this file, the data is used to generate validation metrics
* periodically during fine-tuning. These metrics can be viewed in the
* [fine-tuning results file](https://platform.openai.com/docs/guides/legacy-fine-tuning/analyzing-your-fine-tuned-model).
* Your train and validation data should be mutually exclusive.
*
* Your dataset must be formatted as a JSONL file, where each validation example is
* a JSON object with the keys "prompt" and "completion". Additionally, you must
* upload your file with the purpose `fine-tune`.
*
* See the
* [fine-tuning guide](https://platform.openai.com/docs/guides/legacy-fine-tuning/creating-training-data)
* for more details.
*/
validation_file?: string | null;
}
export namespace FineTuneCreateParams {
/**
* The hyperparameters used for the fine-tuning job.
*/
export interface Hyperparameters {
/**
* The number of epochs to train the model for. An epoch refers to one full cycle
* through the training dataset.
*/
n_epochs?: 'auto' | number;
}
}
export type FineTuneListEventsParams =
| FineTuneListEventsParamsNonStreaming
| FineTuneListEventsParamsStreaming;
export interface FineTuneListEventsParamsBase {
/**
* Whether to stream events for the fine-tune job. If set to true, events will be
* sent as data-only
* [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format)
* as they become available. The stream will terminate with a `data: [DONE]`
* message when the job is finished (succeeded, cancelled, or failed).
*
* If set to false, only events generated so far will be returned.
*/
stream?: boolean;
}
export namespace FineTuneListEventsParams {
export type FineTuneListEventsParamsNonStreaming = FineTunesAPI.FineTuneListEventsParamsNonStreaming;
export type FineTuneListEventsParamsStreaming = FineTunesAPI.FineTuneListEventsParamsStreaming;
}
export interface FineTuneListEventsParamsNonStreaming extends FineTuneListEventsParamsBase {
/**
* Whether to stream events for the fine-tune job. If set to true, events will be
* sent as data-only
* [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format)
* as they become available. The stream will terminate with a `data: [DONE]`
* message when the job is finished (succeeded, cancelled, or failed).
*
* If set to false, only events generated so far will be returned.
*/
stream?: false;
}
export interface FineTuneListEventsParamsStreaming extends FineTuneListEventsParamsBase {
/**
* Whether to stream events for the fine-tune job. If set to true, events will be
* sent as data-only
* [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format)
* as they become available. The stream will terminate with a `data: [DONE]`
* message when the job is finished (succeeded, cancelled, or failed).
*
* If set to false, only events generated so far will be returned.
*/
stream: true;
}
export namespace FineTunes {
export import FineTune = FineTunesAPI.FineTune;
export import FineTuneEvent = FineTunesAPI.FineTuneEvent;
export import FineTuneEventsListResponse = FineTunesAPI.FineTuneEventsListResponse;
export import FineTunesPage = FineTunesAPI.FineTunesPage;
export import FineTuneCreateParams = FineTunesAPI.FineTuneCreateParams;
export import FineTuneListEventsParams = FineTunesAPI.FineTuneListEventsParams;
export import FineTuneListEventsParamsNonStreaming = FineTunesAPI.FineTuneListEventsParamsNonStreaming;
export import FineTuneListEventsParamsStreaming = FineTunesAPI.FineTuneListEventsParamsStreaming;
}