Skip to main content

openai_interface/fine_tuning/jobs/
checkpoints.rs

1//! List the checkpoints of a fine-tuning job via
2//! `GET /fine_tuning/jobs/{job_id}/checkpoints`.
3//!
4//! > ![warn] This module is untested!
5//! > If you encounter any issues, please report them on the repository.
6//!
7//! Checkpoints are only created for jobs with `integrations` or when the
8//! official service decides to snapshot the run; the list may be empty.
9
10use url::Url;
11
12use crate::{
13    errors::OapiError,
14    fine_tuning::FineTuneMetrics,
15    rest::get::{Get, GetNoStream},
16};
17
18/// Lists the checkpoints of a fine-tuning job.
19#[derive(Debug, Clone, Default)]
20pub struct ListFineTuningCheckpointsRequest<'a> {
21    /// The ID of the job whose checkpoints to list, e.g. `ftjob_...`.
22    pub job_id: &'a str,
23    /// A cursor for pagination: return checkpoints created *after*
24    /// this checkpoint ID.
25    pub after: Option<&'a str>,
26    /// A limit on the number of checkpoints to be returned (1-100,
27    /// default 20).
28    pub limit: Option<u32>,
29    /// Additional query parameters appended verbatim to the URL.
30    pub extra_query: Option<std::collections::HashMap<String, String>>,
31}
32
33impl Get for ListFineTuningCheckpointsRequest<'_> {
34    /// Builds the URL for the request.
35    ///
36    /// `base_url` should be like <https://api.openai.com/v1>
37    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
38        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
39        url.path_segments_mut()
40            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
41            .push("fine_tuning")
42            .push("jobs")
43            .push(self.job_id)
44            .push("checkpoints");
45
46        let mut touched = false;
47        {
48            let mut pairs = url.query_pairs_mut();
49            if let Some(after) = self.after {
50                pairs.append_pair("after", after);
51                touched = true;
52            }
53            if let Some(limit) = self.limit {
54                pairs.append_pair("limit", &limit.to_string());
55                touched = true;
56            }
57            if let Some(extra_query) = &self.extra_query {
58                for (key, value) in extra_query {
59                    pairs.append_pair(key, value);
60                }
61                touched = true;
62            }
63        }
64        if !touched {
65            url.set_query(None);
66        }
67
68        Ok(url.to_string())
69    }
70}
71
72impl GetNoStream for ListFineTuningCheckpointsRequest<'_> {
73    type Response = ListFineTuningCheckpointsResponse;
74}
75
76/// A checkpoint of a fine-tuning job.
77#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
78pub struct FineTuningCheckpoint {
79    /// The checkpoint ID, e.g. `ftckpt_...`.
80    pub id: String,
81    /// The object type, always `fine_tuning.job.checkpoint`.
82    #[serde(default)]
83    pub object: Option<String>,
84    /// Unix timestamp (seconds) of when the checkpoint was created.
85    pub created_at: u64,
86    /// The name of the fine-tuned model at this checkpoint.
87    pub fine_tuned_model_checkpoint: String,
88    /// The step number the checkpoint was taken at.
89    pub step_number: u64,
90    /// The metrics at this checkpoint.
91    #[serde(default)]
92    pub metrics: Option<FineTuneMetrics>,
93    /// The ID of the job the checkpoint belongs to.
94    pub fine_tuning_job_id: String,
95}
96
97/// The response of listing a job's checkpoints.
98#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
99pub struct ListFineTuningCheckpointsResponse {
100    /// The checkpoints on this page.
101    #[serde(default)]
102    pub data: Vec<FineTuningCheckpoint>,
103    /// Whether more checkpoints exist after this page.
104    #[serde(default)]
105    pub has_more: Option<bool>,
106    /// The ID of the first checkpoint on the page, for cursor
107    /// pagination.
108    #[serde(default)]
109    pub first_id: Option<String>,
110    /// The ID of the last checkpoint on the page, for cursor
111    /// pagination.
112    #[serde(default)]
113    pub last_id: Option<String>,
114    /// The object type (`list`), if the provider sends it.
115    #[serde(default)]
116    pub object: Option<String>,
117}
118
119crate::impl_from_str!(ListFineTuningCheckpointsResponse);