0
Create fine tune
One script reply has been approved by the moderators Verified

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

Created by hugo697 357 days ago Viewed 8967 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 357 days ago
1
type Openai = {
2
  api_key: string;
3
  organization_id: string;
4
};
5
/**
6
 * Create fine tune
7
 * Creates a job that fine-tunes a specified model from a given dataset.
8

9
Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete.
10

11
Learn more about fine-tuning
12

13
 */
14
export async function main(
15
  auth: Openai,
16
  body: {
17
    training_file: string;
18
    batch_size?: number;
19
    classification_betas?: number[];
20
    classification_n_classes?: number;
21
    classification_positive_class?: string;
22
    compute_classification_metrics?: boolean;
23
    hyperparameters?: { n_epochs?: "auto" | number; [k: string]: unknown };
24
    learning_rate_multiplier?: number;
25
    model?: string | ("ada" | "babbage" | "curie" | "davinci");
26
    prompt_loss_weight?: number;
27
    suffix?: string;
28
    validation_file?: string;
29
    [k: string]: unknown;
30
  }
31
) {
32
  const url = new URL(`https://api.openai.com/v1/fine-tunes`);
33

34
  const response = await fetch(url, {
35
    method: "POST",
36
    headers: {
37
      "OpenAI-Organization": auth.organization_id,
38
      "Content-Type": "application/json",
39
      Authorization: "Bearer " + auth.api_key,
40
    },
41
    body: JSON.stringify(body),
42
  });
43
  if (!response.ok) {
44
    const text = await response.text();
45
    throw new Error(`${response.status} ${text}`);
46
  }
47
  return await response.json();
48
}
49