Skip to main content

openai_compat/resources/
fine_tuning.rs

1//! `/fine_tuning` resource, mirroring `resources/fine_tuning/jobs/`:
2//! create, retrieve, list, cancel, pause, resume, list events, and list
3//! checkpoints for fine-tuning jobs.
4
5use reqwest::Method;
6
7use crate::client::Client;
8use crate::error::OpenAIError;
9use crate::pagination::List;
10use crate::request::RequestOptions;
11use crate::types::fine_tuning::{
12    FineTuningJob, FineTuningJobCheckpoint, FineTuningJobEvent, FineTuningJobListParams,
13    FineTuningJobRequest, FineTuningPageParams,
14};
15
16/// Accessor for fine-tuning endpoints: `client.fine_tuning().jobs()`.
17#[derive(Debug, Clone)]
18pub struct FineTuning {
19    client: Client,
20}
21
22impl FineTuning {
23    pub(crate) fn new(client: Client) -> Self {
24        Self { client }
25    }
26
27    /// The fine-tuning jobs resource.
28    pub fn jobs(&self) -> FineTuningJobs {
29        FineTuningJobs {
30            client: self.client.clone(),
31        }
32    }
33}
34
35/// The `/fine_tuning/jobs` resource.
36#[derive(Debug, Clone)]
37pub struct FineTuningJobs {
38    client: Client,
39}
40
41impl FineTuningJobs {
42    /// Create a fine-tuning job.
43    pub async fn create(
44        &self,
45        request: FineTuningJobRequest,
46    ) -> Result<FineTuningJob, OpenAIError> {
47        let body = serde_json::to_value(&request)?;
48        self.client
49            .execute(
50                Method::POST,
51                "/fine_tuning/jobs",
52                RequestOptions::json(body),
53            )
54            .await
55    }
56
57    /// Retrieve a fine-tuning job.
58    pub async fn retrieve(&self, job_id: &str) -> Result<FineTuningJob, OpenAIError> {
59        self.client
60            .execute(
61                Method::GET,
62                &format!("/fine_tuning/jobs/{job_id}"),
63                RequestOptions::default(),
64            )
65            .await
66    }
67
68    /// List fine-tuning jobs (a single page).
69    pub async fn list(
70        &self,
71        params: Option<FineTuningJobListParams>,
72    ) -> Result<List<FineTuningJob>, OpenAIError> {
73        let query = params.unwrap_or_default().to_query();
74        self.client
75            .execute(
76                Method::GET,
77                "/fine_tuning/jobs",
78                RequestOptions::query(query),
79            )
80            .await
81    }
82
83    /// List all fine-tuning jobs, following pagination cursors.
84    pub async fn list_all(
85        &self,
86        params: Option<FineTuningJobListParams>,
87    ) -> Result<Vec<FineTuningJob>, OpenAIError> {
88        let query = params.unwrap_or_default().to_query();
89        self.client.paginate_all("/fine_tuning/jobs", query).await
90    }
91
92    /// Cancel a fine-tuning job.
93    pub async fn cancel(&self, job_id: &str) -> Result<FineTuningJob, OpenAIError> {
94        self.client
95            .execute(
96                Method::POST,
97                &format!("/fine_tuning/jobs/{job_id}/cancel"),
98                RequestOptions::default(),
99            )
100            .await
101    }
102
103    /// Pause a fine-tuning job.
104    pub async fn pause(&self, job_id: &str) -> Result<FineTuningJob, OpenAIError> {
105        self.client
106            .execute(
107                Method::POST,
108                &format!("/fine_tuning/jobs/{job_id}/pause"),
109                RequestOptions::default(),
110            )
111            .await
112    }
113
114    /// Resume a paused fine-tuning job.
115    pub async fn resume(&self, job_id: &str) -> Result<FineTuningJob, OpenAIError> {
116        self.client
117            .execute(
118                Method::POST,
119                &format!("/fine_tuning/jobs/{job_id}/resume"),
120                RequestOptions::default(),
121            )
122            .await
123    }
124
125    /// List status events for a fine-tuning job (a single page).
126    pub async fn list_events(
127        &self,
128        job_id: &str,
129        params: Option<FineTuningPageParams>,
130    ) -> Result<List<FineTuningJobEvent>, OpenAIError> {
131        let query = params.unwrap_or_default().to_query();
132        self.client
133            .execute(
134                Method::GET,
135                &format!("/fine_tuning/jobs/{job_id}/events"),
136                RequestOptions::query(query),
137            )
138            .await
139    }
140
141    /// List checkpoints for a fine-tuning job (a single page).
142    pub async fn list_checkpoints(
143        &self,
144        job_id: &str,
145        params: Option<FineTuningPageParams>,
146    ) -> Result<List<FineTuningJobCheckpoint>, OpenAIError> {
147        let query = params.unwrap_or_default().to_query();
148        self.client
149            .execute(
150                Method::GET,
151                &format!("/fine_tuning/jobs/{job_id}/checkpoints"),
152                RequestOptions::query(query),
153            )
154            .await
155    }
156}