Skip to main content

openai_tools/fine_tuning/
response.rs

1//! OpenAI Fine-tuning API Response Types
2//!
3//! This module defines the response types for the OpenAI Fine-tuning API.
4
5use serde::{Deserialize, Serialize};
6
7/// The status of a fine-tuning job.
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9#[serde(rename_all = "snake_case")]
10pub enum FineTuningJobStatus {
11    /// Files are being validated.
12    ValidatingFiles,
13    /// Job is queued for processing.
14    Queued,
15    /// Job is currently running.
16    Running,
17    /// Job completed successfully.
18    Succeeded,
19    /// Job failed.
20    Failed,
21    /// Job was cancelled.
22    Cancelled,
23}
24
25/// Hyperparameters used for fine-tuning.
26#[derive(Debug, Clone, Serialize, Deserialize, Default)]
27pub struct Hyperparameters {
28    /// Number of epochs to train for.
29    /// Can be "auto" in API but represented as Option here.
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub n_epochs: Option<u32>,
32
33    /// Batch size for training.
34    /// Can be "auto" in API but represented as Option here.
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub batch_size: Option<u32>,
37
38    /// Learning rate multiplier.
39    /// Can be "auto" in API but represented as Option here.
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub learning_rate_multiplier: Option<f64>,
42}
43
44/// Error information for a failed fine-tuning job.
45#[derive(Debug, Clone, Serialize, Deserialize)]
46pub struct FineTuningError {
47    /// A machine-readable error code.
48    pub code: String,
49    /// A human-readable error message.
50    pub message: String,
51    /// The parameter related to the error, if any.
52    pub param: Option<String>,
53}
54
55/// Integration configuration (e.g., Weights & Biases).
56#[derive(Debug, Clone, Serialize, Deserialize)]
57pub struct Integration {
58    /// The type of integration (e.g., "wandb").
59    #[serde(rename = "type")]
60    pub integration_type: String,
61
62    /// Integration-specific settings.
63    #[serde(flatten)]
64    pub settings: serde_json::Value,
65}
66
67/// Method configuration for fine-tuning.
68#[derive(Debug, Clone, Serialize, Deserialize)]
69pub struct MethodConfig {
70    /// The type of fine-tuning method (e.g., "supervised", "dpo").
71    #[serde(rename = "type")]
72    pub method_type: String,
73
74    /// Supervised fine-tuning configuration.
75    #[serde(skip_serializing_if = "Option::is_none")]
76    pub supervised: Option<SupervisedConfig>,
77
78    /// DPO fine-tuning configuration.
79    #[serde(skip_serializing_if = "Option::is_none")]
80    pub dpo: Option<DpoConfig>,
81}
82
83/// Configuration for supervised fine-tuning.
84#[derive(Debug, Clone, Serialize, Deserialize)]
85pub struct SupervisedConfig {
86    /// Hyperparameters for supervised fine-tuning.
87    #[serde(skip_serializing_if = "Option::is_none")]
88    pub hyperparameters: Option<Hyperparameters>,
89}
90
91/// Configuration for DPO (Direct Preference Optimization) fine-tuning.
92#[derive(Debug, Clone, Serialize, Deserialize)]
93pub struct DpoConfig {
94    /// Hyperparameters for DPO fine-tuning.
95    #[serde(skip_serializing_if = "Option::is_none")]
96    pub hyperparameters: Option<Hyperparameters>,
97}
98
99/// A fine-tuning job object.
100#[derive(Debug, Clone, Serialize, Deserialize)]
101pub struct FineTuningJob {
102    /// The unique identifier for the job.
103    pub id: String,
104
105    /// The object type (always "fine_tuning.job").
106    pub object: String,
107
108    /// The base model being fine-tuned.
109    pub model: String,
110
111    /// The Unix timestamp when the job was created.
112    pub created_at: i64,
113
114    /// The Unix timestamp when the job finished.
115    pub finished_at: Option<i64>,
116
117    /// The name of the fine-tuned model (available after success).
118    pub fine_tuned_model: Option<String>,
119
120    /// The organization ID that owns the job.
121    pub organization_id: String,
122
123    /// Array of result file IDs.
124    pub result_files: Vec<String>,
125
126    /// The current status of the job.
127    pub status: FineTuningJobStatus,
128
129    /// The validation file ID, if provided.
130    pub validation_file: Option<String>,
131
132    /// The training file ID.
133    pub training_file: String,
134
135    /// The hyperparameters used for training.
136    pub hyperparameters: Hyperparameters,
137
138    /// The number of tokens trained on (null while running).
139    pub trained_tokens: Option<u64>,
140
141    /// Error information if the job failed.
142    pub error: Option<FineTuningError>,
143
144    /// The seed used for training.
145    pub seed: u64,
146
147    /// Estimated finish time (Unix timestamp).
148    pub estimated_finish: Option<i64>,
149
150    /// Configured integrations.
151    pub integrations: Option<Vec<Integration>>,
152
153    /// The fine-tuning method used.
154    pub method: Option<MethodConfig>,
155
156    /// User-provided suffix for the model name.
157    pub user_provided_suffix: Option<String>,
158}
159
160/// Response for listing fine-tuning jobs.
161#[derive(Debug, Clone, Serialize, Deserialize)]
162pub struct FineTuningJobListResponse {
163    /// The object type (always "list").
164    pub object: String,
165
166    /// The list of fine-tuning jobs.
167    pub data: Vec<FineTuningJob>,
168
169    /// Whether there are more jobs to retrieve.
170    pub has_more: bool,
171}
172
173/// A fine-tuning event object.
174#[derive(Debug, Clone, Serialize, Deserialize)]
175pub struct FineTuningEvent {
176    /// The unique identifier for the event.
177    pub id: String,
178
179    /// The object type (always "fine_tuning.job.event").
180    pub object: String,
181
182    /// The Unix timestamp when the event was created.
183    pub created_at: i64,
184
185    /// The level of the event ("info", "warn", "error").
186    pub level: String,
187
188    /// The event message.
189    pub message: String,
190
191    /// Additional data associated with the event.
192    pub data: Option<serde_json::Value>,
193
194    /// The type of event.
195    #[serde(rename = "type")]
196    pub event_type: String,
197}
198
199/// Response for listing fine-tuning events.
200#[derive(Debug, Clone, Serialize, Deserialize)]
201pub struct FineTuningEventListResponse {
202    /// The object type (always "list").
203    pub object: String,
204
205    /// The list of events.
206    pub data: Vec<FineTuningEvent>,
207
208    /// Whether there are more events to retrieve.
209    pub has_more: bool,
210}
211
212/// Metrics for a fine-tuning checkpoint.
213#[derive(Debug, Clone, Serialize, Deserialize)]
214pub struct CheckpointMetrics {
215    /// The training step number.
216    pub step: u32,
217
218    /// The training loss at this checkpoint.
219    pub train_loss: f64,
220
221    /// The mean token accuracy during training.
222    pub train_mean_token_accuracy: f64,
223
224    /// The validation loss at this checkpoint.
225    pub valid_loss: Option<f64>,
226
227    /// The mean token accuracy during validation.
228    pub valid_mean_token_accuracy: Option<f64>,
229
230    /// The full validation loss.
231    pub full_valid_loss: Option<f64>,
232
233    /// The full validation mean token accuracy.
234    pub full_valid_mean_token_accuracy: Option<f64>,
235}
236
237/// A fine-tuning checkpoint object.
238#[derive(Debug, Clone, Serialize, Deserialize)]
239pub struct FineTuningCheckpoint {
240    /// The unique identifier for the checkpoint.
241    pub id: String,
242
243    /// The object type (always "fine_tuning.job.checkpoint").
244    pub object: String,
245
246    /// The Unix timestamp when the checkpoint was created.
247    pub created_at: i64,
248
249    /// The ID of the fine-tuning job.
250    pub fine_tuning_job_id: String,
251
252    /// The name of the checkpoint model.
253    pub fine_tuned_model_checkpoint: String,
254
255    /// The step number at which this checkpoint was created.
256    pub step_number: u32,
257
258    /// Training metrics at this checkpoint.
259    pub metrics: CheckpointMetrics,
260}
261
262/// Response for listing fine-tuning checkpoints.
263#[derive(Debug, Clone, Serialize, Deserialize)]
264pub struct FineTuningCheckpointListResponse {
265    /// The object type (always "list").
266    pub object: String,
267
268    /// The list of checkpoints.
269    pub data: Vec<FineTuningCheckpoint>,
270
271    /// The ID of the first checkpoint in the list.
272    pub first_id: Option<String>,
273
274    /// The ID of the last checkpoint in the list.
275    pub last_id: Option<String>,
276
277    /// Whether there are more checkpoints to retrieve.
278    pub has_more: bool,
279}