Skip to main content

openai_interface/fine_tuning/
mod.rs

1//! The Fine-tuning API: manage fine-tuning jobs via `/fine_tuning`.
2//!
3//! > ![warn] This module is untested!
4//! > If you encounter any issues, please report them on the repository.
5//!
6//! A fine-tuning job trains a model on a JSONL training file (and an
7//! optional validation file) uploaded through the
8//! [`files`](crate::files) API. See
9//! [the OpenAI Fine-tuning guide](https://platform.openai.com/docs/guides/fine-tuning).
10//!
11//! Submodules: [`jobs`] (create / list / retrieve / cancel, plus the
12//! events and checkpoints sub-resources) and [`models`] (restore a
13//! fine-tuned model after a timeout).
14//!
15//! The `POST /fine_tuning/alpha/permissions` endpoints (GRPO reward
16//! functions) are not implemented; they are reserved for the
17//! enterprise-only alpha program.
18
19pub mod jobs;
20pub mod models;
21
22/// The lifecycle status of a fine-tuning job.
23#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize)]
24#[serde(rename_all = "snake_case")]
25pub enum FineTuningJobStatus {
26    /// The job is being validated and queued.
27    ValidatingFiles,
28    /// The job is waiting in the queue.
29    Queued,
30    /// The job is running.
31    Running,
32    /// The job finished successfully.
33    Succeeded,
34    /// The job failed.
35    Failed,
36    /// The job was cancelled by the user.
37    Cancelled,
38}
39
40/// The per-step training/validation loss statistics of a fine-tuning
41/// job.
42#[derive(Debug, Clone, serde::Deserialize)]
43pub struct FineTuneMetrics {
44    /// Training loss for this step.
45    #[serde(default)]
46    pub step: Option<u64>,
47    /// Training loss.
48    #[serde(default)]
49    pub train_loss: Option<serde_json::Value>,
50    /// Training mean token accuracy.
51    #[serde(default)]
52    pub train_mean_token_accuracy: Option<serde_json::Value>,
53    /// Validation loss.
54    #[serde(default)]
55    pub valid_loss: Option<serde_json::Value>,
56    /// Validation mean token accuracy.
57    #[serde(default)]
58    pub validation_mean_token_accuracy: Option<serde_json::Value>,
59    /// Full validation loss.
60    #[serde(default)]
61    pub full_valid_loss: Option<serde_json::Value>,
62    /// Full validation mean token accuracy.
63    #[serde(default)]
64    pub full_valid_mean_token_accuracy: Option<serde_json::Value>,
65}
66
67/// The hyperparameters of a fine-tuning job.
68///
69/// All fields are optional because the server may omit values when they
70/// are set to `auto`.
71#[derive(Debug, Clone, serde::Deserialize)]
72pub struct Hyperparameters {
73    /// Number of epochs. A number, or `"auto"`.
74    #[serde(default)]
75    pub n_epochs: Option<serde_json::Value>,
76    /// Batch size (in examples). A number, or `"auto"`.
77    #[serde(default)]
78    pub batch_size: Option<serde_json::Value>,
79    /// Learning rate multiplier. A number, or `"auto"`.
80    #[serde(default)]
81    pub learning_rate_multiplier: Option<serde_json::Value>,
82}
83
84/// The result files of a completed fine-tuning job.
85#[derive(Debug, Clone, serde::Deserialize)]
86pub struct FineTuningJob {
87    /// The job ID, e.g. `ftjob_...`.
88    pub id: String,
89    /// The object type, always `fine_tuning.job`.
90    #[serde(default)]
91    pub object: Option<String>,
92    /// Unix timestamp (seconds) of when the job was created.
93    pub created_at: u64,
94    /// Unix timestamp (seconds) of when the job finished.
95    #[serde(default)]
96    pub finished_at: Option<u64>,
97    /// The base model that was fine-tuned.
98    pub model: String,
99    /// The name of the resulting fine-tuned model, once the job
100    /// succeeds.
101    #[serde(default)]
102    pub fine_tuned_model: Option<String>,
103    /// The organization that owns the job.
104    #[serde(default)]
105    pub organization_id: Option<String>,
106    /// The current lifecycle status.
107    pub status: FineTuningJobStatus,
108    /// The ID of the uploaded JSONL training file.
109    pub training_file: String,
110    /// The ID of the uploaded JSONL validation file, if provided.
111    #[serde(default)]
112    pub validation_file: Option<String>,
113    /// The hyperparameters used for the job.
114    #[serde(default)]
115    pub hyperparameters: Option<Hyperparameters>,
116    /// The seed used for the job.
117    #[serde(default)]
118    pub seed: Option<u64>,
119    /// The suffix appended to the fine-tuned model name.
120    #[serde(default)]
121    pub suffix: Option<String>,
122    /// The integrations configured for the job.
123    #[serde(default)]
124    pub integrations: Option<serde_json::Value>,
125    /// The accumulated metrics and checkpoints of the job.
126    #[serde(default)]
127    pub result_files: Vec<String>,
128    /// The computed metrics of the job, if available.
129    #[serde(default)]
130    pub metrics: Option<FineTuneMetrics>,
131    /// The user that created the job.
132    #[serde(default)]
133    pub user_provided_suffix: Option<bool>,
134    /// The error of a failed job.
135    #[serde(default)]
136    pub error: Option<crate::errors::ApiError>,
137    /// Arbitrary key-value metadata attached to the job.
138    #[serde(default)]
139    pub metadata: Option<std::collections::HashMap<String, String>>,
140    /// The method used for fine-tuning (supervised, DPO, or
141    /// reinforcement learning), as raw JSON.
142    #[serde(default)]
143    pub method: Option<serde_json::Value>,
144    /// Estimated number of billable tokens.
145    #[serde(default)]
146    pub estimated_finish: Option<u64>,
147    /// Token counts of the job, when reported.
148    #[serde(default)]
149    pub trained_tokens: Option<u64>,
150    /// Unix timestamp (seconds) of when the job is estimated to finish.
151    #[serde(rename = "estimated_finish_minutes")]
152    #[serde(default)]
153    pub estimated_finish_minutes: Option<u64>,
154}
155
156crate::impl_from_str!(FineTuningJob);