async_openai/types/
fine_tuning.rs

1use derive_builder::Builder;
2use serde::{Deserialize, Serialize};
3
4use crate::error::OpenAIError;
5
6#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
7#[serde(untagged)]
8pub enum NEpochs {
9    NEpochs(u8),
10    #[default]
11    #[serde(rename = "auto")]
12    Auto,
13}
14
15#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
16#[serde(untagged)]
17pub enum BatchSize {
18    BatchSize(u16),
19    #[default]
20    #[serde(rename = "auto")]
21    Auto,
22}
23
24#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
25#[serde(untagged)]
26pub enum LearningRateMultiplier {
27    LearningRateMultiplier(f32),
28    #[default]
29    #[serde(rename = "auto")]
30    Auto,
31}
32
33#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
34pub struct Hyperparameters {
35    /// Number of examples in each batch. A larger batch size means that model parameters
36    /// are updated less frequently, but with lower variance.
37    pub batch_size: BatchSize,
38    /// Scaling factor for the learning rate. A smaller learning rate may be useful to avoid
39    /// overfitting.
40    pub learning_rate_multiplier: LearningRateMultiplier,
41    /// The number of epochs to train the model for. An epoch refers to one full cycle through the training dataset.
42    pub n_epochs: NEpochs,
43}
44
45#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
46#[serde(untagged)]
47pub enum Beta {
48    Beta(f32),
49    #[default]
50    #[serde(rename = "auto")]
51    Auto,
52}
53
54#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
55pub struct DPOHyperparameters {
56    /// The beta value for the DPO method. A higher beta value will increase the weight of the penalty between the policy and reference model.
57    pub beta: Beta,
58    /// Number of examples in each batch. A larger batch size means that model parameters
59    /// are updated less frequently, but with lower variance.
60    pub batch_size: BatchSize,
61    /// Scaling factor for the learning rate. A smaller learning rate may be useful to avoid
62    /// overfitting.
63    pub learning_rate_multiplier: LearningRateMultiplier,
64    /// The number of epochs to train the model for. An epoch refers to one full cycle through the training dataset.
65    pub n_epochs: NEpochs,
66}
67
68#[derive(Debug, Serialize, Deserialize, Clone, Default, Builder, PartialEq)]
69#[builder(name = "CreateFineTuningJobRequestArgs")]
70#[builder(pattern = "mutable")]
71#[builder(setter(into, strip_option), default)]
72#[builder(derive(Debug))]
73#[builder(build_fn(error = "OpenAIError"))]
74pub struct CreateFineTuningJobRequest {
75    /// The name of the model to fine-tune. You can select one of the
76    /// [supported models](https://platform.openai.com/docs/guides/fine-tuning#which-models-can-be-fine-tuned).
77    pub model: String,
78
79    /// The ID of an uploaded file that contains training data.
80    ///
81    /// See [upload file](https://platform.openai.com/docs/api-reference/files/create) for how to upload a file.
82    ///
83    /// Your dataset must be formatted as a JSONL file. Additionally, you must upload your file with the purpose `fine-tune`.
84    ///
85    /// The contents of the file should differ depending on if the model uses the [chat](https://platform.openai.com/docs/api-reference/fine-tuning/chat-input), [completions](https://platform.openai.com/docs/api-reference/fine-tuning/completions-input) format, or if the fine-tuning method uses the [preference](https://platform.openai.com/docs/api-reference/fine-tuning/preference-input) format.
86    ///
87    /// See the [fine-tuning guide](https://platform.openai.com/docs/guides/fine-tuning) for more details.
88    pub training_file: String,
89
90    /// The hyperparameters used for the fine-tuning job.
91    /// This value is now deprecated in favor of `method`, and should be passed in under the `method` parameter.
92    #[deprecated]
93    pub hyperparameters: Option<Hyperparameters>,
94
95    /// A string of up to 64 characters that will be added to your fine-tuned model name.
96    ///
97    /// For example, a `suffix` of "custom-model-name" would produce a model name like `ft:gpt-4o-mini:openai:custom-model-name:7p4lURel`.
98    #[serde(skip_serializing_if = "Option::is_none")]
99    pub suffix: Option<String>, // default: null, minLength:1, maxLength:40
100
101    /// The ID of an uploaded file that contains validation data.
102    ///
103    /// If you provide this file, the data is used to generate validation
104    /// metrics periodically during fine-tuning. These metrics can be viewed in
105    /// the fine-tuning results file.
106    /// The same data should not be present in both train and validation files.
107    ///
108    /// Your dataset must be formatted as a JSONL file. You must upload your file with the purpose `fine-tune`.
109    ///
110    /// See the [fine-tuning guide](https://platform.openai.com/docs/guides/fine-tuning) for more details.
111    #[serde(skip_serializing_if = "Option::is_none")]
112    pub validation_file: Option<String>,
113
114    /// A list of integrations to enable for your fine-tuning job.
115    #[serde(skip_serializing_if = "Option::is_none")]
116    pub integrations: Option<Vec<FineTuningIntegration>>,
117
118    /// The seed controls the reproducibility of the job. Passing in the same seed and job parameters should produce the same results, but may differ in rare cases.
119    /// If a seed is not specified, one will be generated for you.
120    #[serde(skip_serializing_if = "Option::is_none")]
121    pub seed: Option<u32>, // min:0, max: 2147483647
122
123    #[serde(skip_serializing_if = "Option::is_none")]
124    pub method: Option<FineTuneMethod>,
125}
126
127/// The method used for fine-tuning.
128#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
129#[serde(tag = "type", rename_all = "lowercase")]
130pub enum FineTuneMethod {
131    Supervised {
132        supervised: FineTuneSupervisedMethod,
133    },
134    DPO {
135        dpo: FineTuneDPOMethod,
136    },
137}
138
139#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
140pub struct FineTuneSupervisedMethod {
141    pub hyperparameters: Hyperparameters,
142}
143
144#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
145pub struct FineTuneDPOMethod {
146    pub hyperparameters: DPOHyperparameters,
147}
148
149#[derive(Debug, Deserialize, Clone, PartialEq, Serialize, Default)]
150#[serde(rename_all = "lowercase")]
151pub enum FineTuningJobIntegrationType {
152    #[default]
153    Wandb,
154}
155
156#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
157pub struct FineTuningIntegration {
158    /// The type of integration to enable. Currently, only "wandb" (Weights and Biases) is supported.
159    pub r#type: FineTuningJobIntegrationType,
160
161    /// The settings for your integration with Weights and Biases. This payload specifies the project that
162    /// metrics will be sent to. Optionally, you can set an explicit display name for your run, add tags
163    /// to your run, and set a default entity (team, username, etc) to be associated with your run.
164    pub wandb: WandB,
165}
166
167#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
168pub struct WandB {
169    /// The name of the project that the new run will be created under.
170    pub project: String,
171    /// A display name to set for the run. If not set, we will use the Job ID as the name.
172    #[serde(skip_serializing_if = "Option::is_none")]
173    pub name: Option<String>,
174    /// The entity to use for the run. This allows you to set the team or username of the WandB user that you would
175    /// like associated with the run. If not set, the default entity for the registered WandB API key is used.
176    #[serde(skip_serializing_if = "Option::is_none")]
177    pub entity: Option<String>,
178    /// A list of tags to be attached to the newly created run. These tags are passed through directly to WandB. Some
179    /// default tags are generated by OpenAI: "openai/finetune", "openai/{base-model}", "openai/{ftjob-abcdef}".
180    #[serde(skip_serializing_if = "Option::is_none")]
181    pub tags: Option<Vec<String>>,
182}
183
184/// For fine-tuning jobs that have `failed`, this will contain more information on the cause of the failure.
185#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
186pub struct FineTuneJobError {
187    ///  A machine-readable error code.
188    pub code: String,
189    ///  A human-readable error message.
190    pub message: String,
191    /// The parameter that was invalid, usually `training_file` or `validation_file`.
192    /// This field will be null if the failure was not parameter-specific.
193    pub param: Option<String>, // nullable true
194}
195
196#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
197#[serde(rename_all = "snake_case")]
198pub enum FineTuningJobStatus {
199    ValidatingFiles,
200    Queued,
201    Running,
202    Succeeded,
203    Failed,
204    Cancelled,
205}
206
207/// The `fine_tuning.job` object represents a fine-tuning job that has been created through the API.
208#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
209pub struct FineTuningJob {
210    /// The object identifier, which can be referenced in the API endpoints.
211    pub id: String,
212    /// The Unix timestamp (in seconds) for when the fine-tuning job was created.
213    pub created_at: u32,
214    /// For fine-tuning jobs that have `failed`, this will contain more information on the cause of the failure.
215    pub error: Option<FineTuneJobError>,
216    /// The name of the fine-tuned model that is being created.
217    /// The value will be null if the fine-tuning job is still running.
218    pub fine_tuned_model: Option<String>, // nullable: true
219    /// The Unix timestamp (in seconds) for when the fine-tuning job was finished.
220    /// The value will be null if the fine-tuning job is still running.
221    pub finished_at: Option<u32>, // nullable true
222
223    /// The hyperparameters used for the fine-tuning job.
224    /// See the [fine-tuning guide](/docs/guides/fine-tuning) for more details.
225    pub hyperparameters: Hyperparameters,
226
227    ///  The base model that is being fine-tuned.
228    pub model: String,
229
230    /// The object type, which is always "fine_tuning.job".
231    pub object: String,
232    /// The organization that owns the fine-tuning job.
233    pub organization_id: String,
234
235    /// The compiled results file ID(s) for the fine-tuning job.
236    /// You can retrieve the results with the [Files API](https://platform.openai.com/docs/api-reference/files/retrieve-contents).
237    pub result_files: Vec<String>,
238
239    /// The current status of the fine-tuning job, which can be either
240    /// `validating_files`, `queued`, `running`, `succeeded`, `failed`, or `cancelled`.
241    pub status: FineTuningJobStatus,
242
243    /// The total number of billable tokens processed by this fine-tuning job. The value will be null if the fine-tuning job is still running.
244    pub trained_tokens: Option<u32>,
245
246    /// The file ID used for training. You can retrieve the training data with the [Files API](https://platform.openai.com/docs/api-reference/files/retrieve-contents).
247    pub training_file: String,
248
249    ///  The file ID used for validation. You can retrieve the validation results with the [Files API](https://platform.openai.com/docs/api-reference/files/retrieve-contents).
250    pub validation_file: Option<String>,
251
252    /// A list of integrations to enable for this fine-tuning job.
253    pub integrations: Option<Vec<FineTuningIntegration>>, // maxItems: 5
254
255    /// The seed used for the fine-tuning job.
256    pub seed: u32,
257
258    /// The Unix timestamp (in seconds) for when the fine-tuning job is estimated to finish. The value will be null if the fine-tuning job is not running.
259    pub estimated_finish: Option<u32>,
260
261    pub method: Option<FineTuneMethod>,
262}
263
264#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
265pub struct ListPaginatedFineTuningJobsResponse {
266    pub data: Vec<FineTuningJob>,
267    pub has_more: bool,
268    pub object: String,
269}
270
271#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
272pub struct ListFineTuningJobEventsResponse {
273    pub data: Vec<FineTuningJobEvent>,
274    pub object: String,
275}
276
277#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
278pub struct ListFineTuningJobCheckpointsResponse {
279    pub data: Vec<FineTuningJobCheckpoint>,
280    pub object: String,
281    pub first_id: Option<String>,
282    pub last_id: Option<String>,
283    pub has_more: bool,
284}
285
286#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
287#[serde(rename_all = "lowercase")]
288pub enum Level {
289    Info,
290    Warn,
291    Error,
292}
293
294///Fine-tuning job event object
295#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
296pub struct FineTuningJobEvent {
297    /// The object identifier.
298    pub id: String,
299    /// The Unix timestamp (in seconds) for when the fine-tuning job event was created.
300    pub created_at: u32,
301    /// The log level of the event.
302    pub level: Level,
303    /// The message of the event.
304    pub message: String,
305    /// The object type, which is always "fine_tuning.job.event".
306    pub object: String,
307    /// The type of event.
308    pub r#type: Option<FineTuningJobEventType>,
309    /// The data associated with the event.
310    pub data: Option<serde_json::Value>,
311}
312
313#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
314#[serde(rename_all = "lowercase")]
315pub enum FineTuningJobEventType {
316    Message,
317    Metrics,
318}
319
320/// The `fine_tuning.job.checkpoint` object represents a model checkpoint for a fine-tuning job that is ready to use.
321#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
322pub struct FineTuningJobCheckpoint {
323    /// The checkpoint identifier, which can be referenced in the API endpoints.
324    pub id: String,
325    /// The Unix timestamp (in seconds) for when the checkpoint was created.
326    pub created_at: u32,
327    /// The name of the fine-tuned checkpoint model that is created.
328    pub fine_tuned_model_checkpoint: String,
329    /// The step number that the checkpoint was created at.
330    pub step_number: u32,
331    /// Metrics at the step number during the fine-tuning job.
332    pub metrics: FineTuningJobCheckpointMetrics,
333    /// The name of the fine-tuning job that this checkpoint was created from.
334    pub fine_tuning_job_id: String,
335    /// The object type, which is always "fine_tuning.job.checkpoint".
336    pub object: String,
337}
338
339#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
340pub struct FineTuningJobCheckpointMetrics {
341    pub step: u32,
342    pub train_loss: f32,
343    pub train_mean_token_accuracy: f32,
344    pub valid_loss: f32,
345    pub valid_mean_token_accuracy: f32,
346    pub full_valid_loss: f32,
347    pub full_valid_mean_token_accuracy: f32,
348}