openai_dive/v1/endpoints/
fine_tuning.rs

1use crate::v1::api::Client;
2use crate::v1::error::APIError;
3use crate::v1::helpers::format_response;
4use crate::v1::resources::fine_tuning::CreateFineTuningJobParameters;
5use crate::v1::resources::fine_tuning::FineTuningJob;
6use crate::v1::resources::fine_tuning::FineTuningJobCheckpoint;
7use crate::v1::resources::fine_tuning::FineTuningJobEvent;
8use crate::v1::resources::shared::ListResponse;
9use crate::v1::resources::shared::SimpleListParameters;
10use serde_json::Value;
11
12pub struct FineTuning<'a> {
13    pub client: &'a Client,
14}
15
16impl Client {
17    /// Manage fine-tuning jobs to tailor a model to your specific training data.
18    pub fn fine_tuning(&self) -> FineTuning<'_> {
19        FineTuning { client: self }
20    }
21}
22
23impl FineTuning<'_> {
24    /// Creates a job that fine-tunes a specified model from a given dataset.
25    pub async fn create(
26        &self,
27        parameters: CreateFineTuningJobParameters,
28    ) -> Result<FineTuningJob, APIError> {
29        let response = self
30            .client
31            .post("/fine_tuning/jobs", &parameters, None)
32            .await?;
33
34        let response: FineTuningJob = format_response(response.data)?;
35
36        Ok(response)
37    }
38
39    /// List your organization's fine-tuning jobs.
40    pub async fn list(
41        &self,
42        query: Option<SimpleListParameters>,
43    ) -> Result<ListResponse<FineTuningJob>, APIError> {
44        let response = self
45            .client
46            .get_with_query("/fine_tuning/jobs", &query)
47            .await?;
48
49        let response: ListResponse<FineTuningJob> = format_response(response)?;
50
51        Ok(response)
52    }
53
54    /// Get info about a fine-tuning job.
55    pub async fn retrieve(&self, id: &str) -> Result<FineTuningJob, APIError> {
56        let response = self.client.get(&format!("/fine_tuning/jobs/{id}")).await?;
57
58        let response: FineTuningJob = format_response(response)?;
59
60        Ok(response)
61    }
62
63    /// Immediately cancel a fine-tune job.
64    pub async fn cancel(&self, id: &str) -> Result<FineTuningJob, APIError> {
65        let response = self
66            .client
67            .post(
68                &format!("/fine_tuning/jobs/{id}/cancel"),
69                &Value::Null,
70                None,
71            )
72            .await?;
73
74        let response: FineTuningJob = format_response(response.data)?;
75
76        Ok(response)
77    }
78
79    /// Get status updates for a fine-tuning job.
80    pub async fn list_job_events(
81        &self,
82        id: &str,
83        query: Option<SimpleListParameters>,
84    ) -> Result<ListResponse<FineTuningJobEvent>, APIError> {
85        let response = self
86            .client
87            .get_with_query(&format!("/fine_tuning/jobs/{id}/events"), &query)
88            .await?;
89
90        let response: ListResponse<FineTuningJobEvent> = format_response(response)?;
91
92        Ok(response)
93    }
94
95    /// List checkpoints for a fine-tuning job.
96    pub async fn list_checkpoints(
97        &self,
98        id: &str,
99        query: Option<SimpleListParameters>,
100    ) -> Result<ListResponse<FineTuningJobCheckpoint>, APIError> {
101        let response = self
102            .client
103            .get_with_query(&format!("/fine_tuning/jobs/{id}/checkpoints"), &query)
104            .await?;
105
106        let response: ListResponse<FineTuningJobCheckpoint> = format_response(response)?;
107
108        Ok(response)
109    }
110}