async_openai/
fine_tuning.rs

1use serde::Serialize;
2
3use crate::{
4    config::Config,
5    error::OpenAIError,
6    types::finetuning::{
7        CreateFineTuningCheckpointPermissionRequest, CreateFineTuningJobRequest,
8        DeleteFineTuningCheckpointPermissionResponse, FineTuningJob,
9        ListFineTuningCheckpointPermissionResponse, ListFineTuningJobCheckpointsResponse,
10        ListFineTuningJobEventsResponse, ListPaginatedFineTuningJobsResponse,
11    },
12    Client,
13};
14
15/// Manage fine-tuning jobs to tailor a model to your specific training data.
16///
17/// Related guide: [Fine-tune models](https://platform.openai.com/docs/guides/fine-tuning)
18pub struct FineTuning<'c, C: Config> {
19    client: &'c Client<C>,
20}
21
22impl<'c, C: Config> FineTuning<'c, C> {
23    pub fn new(client: &'c Client<C>) -> Self {
24        Self { client }
25    }
26
27    /// Creates a fine-tuning job which begins the process of creating a new model from a given dataset.
28    ///
29    /// Response includes details of the enqueued job including job status and the name of the fine-tuned
30    /// models once complete.
31    ///
32    /// [Learn more about fine-tuning](https://platform.openai.com/docs/guides/model-optimization)
33    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
34    pub async fn create(
35        &self,
36        request: CreateFineTuningJobRequest,
37    ) -> Result<FineTuningJob, OpenAIError> {
38        self.client.post("/fine_tuning/jobs", request).await
39    }
40
41    /// List your organization's fine-tuning jobs
42    #[crate::byot(T0 = serde::Serialize, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
43    pub async fn list_paginated<Q>(
44        &self,
45        query: &Q,
46    ) -> Result<ListPaginatedFineTuningJobsResponse, OpenAIError>
47    where
48        Q: Serialize + ?Sized,
49    {
50        self.client
51            .get_with_query("/fine_tuning/jobs", &query)
52            .await
53    }
54
55    /// Get info about a fine-tuning job.
56    ///
57    /// [Learn more about fine-tuning](https://platform.openai.com/docs/guides/model-optimization)
58    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
59    pub async fn retrieve(&self, fine_tuning_job_id: &str) -> Result<FineTuningJob, OpenAIError> {
60        self.client
61            .get(format!("/fine_tuning/jobs/{fine_tuning_job_id}").as_str())
62            .await
63    }
64
65    /// Immediately cancel a fine-tune job.
66    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
67    pub async fn cancel(&self, fine_tuning_job_id: &str) -> Result<FineTuningJob, OpenAIError> {
68        self.client
69            .post(
70                format!("/fine_tuning/jobs/{fine_tuning_job_id}/cancel").as_str(),
71                (),
72            )
73            .await
74    }
75
76    /// Pause a fine-tune job.
77    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
78    pub async fn pause(&self, fine_tuning_job_id: &str) -> Result<FineTuningJob, OpenAIError> {
79        self.client
80            .post(
81                format!("/fine_tuning/jobs/{fine_tuning_job_id}/pause").as_str(),
82                (),
83            )
84            .await
85    }
86
87    /// Resume a fine-tune job.
88    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
89    pub async fn resume(&self, fine_tuning_job_id: &str) -> Result<FineTuningJob, OpenAIError> {
90        self.client
91            .post(
92                format!("/fine_tuning/jobs/{fine_tuning_job_id}/resume").as_str(),
93                (),
94            )
95            .await
96    }
97
98    /// Get status updates for a fine-tuning job.
99    #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
100    pub async fn list_events<Q>(
101        &self,
102        fine_tuning_job_id: &str,
103        query: &Q,
104    ) -> Result<ListFineTuningJobEventsResponse, OpenAIError>
105    where
106        Q: Serialize + ?Sized,
107    {
108        self.client
109            .get_with_query(
110                format!("/fine_tuning/jobs/{fine_tuning_job_id}/events").as_str(),
111                &query,
112            )
113            .await
114    }
115
116    /// List checkpoints for a fine-tuning job.
117    #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
118    pub async fn list_checkpoints<Q>(
119        &self,
120        fine_tuning_job_id: &str,
121        query: &Q,
122    ) -> Result<ListFineTuningJobCheckpointsResponse, OpenAIError>
123    where
124        Q: Serialize + ?Sized,
125    {
126        self.client
127            .get_with_query(
128                format!("/fine_tuning/jobs/{fine_tuning_job_id}/checkpoints").as_str(),
129                &query,
130            )
131            .await
132    }
133
134    #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
135    pub async fn create_checkpoint_permissions(
136        &self,
137        fine_tuned_model_checkpoint: &str,
138        request: CreateFineTuningCheckpointPermissionRequest,
139    ) -> Result<ListFineTuningCheckpointPermissionResponse, OpenAIError> {
140        self.client
141            .post(
142                format!("/fine_tuning/checkpoints/{fine_tuned_model_checkpoint}/permissions")
143                    .as_str(),
144                request,
145            )
146            .await
147    }
148
149    #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
150    pub async fn list_checkpoint_permissions<Q>(
151        &self,
152        fine_tuned_model_checkpoint: &str,
153        query: &Q,
154    ) -> Result<ListFineTuningCheckpointPermissionResponse, OpenAIError>
155    where
156        Q: Serialize + ?Sized,
157    {
158        self.client
159            .get_with_query(
160                format!("/fine_tuning/checkpoints/{fine_tuned_model_checkpoint}/permissions")
161                    .as_str(),
162                &query,
163            )
164            .await
165    }
166
167    #[crate::byot(T0 = std::fmt::Display, T1 = std::fmt::Display, R = serde::de::DeserializeOwned)]
168    pub async fn delete_checkpoint_permissions(
169        &self,
170        fine_tuned_model_checkpoint: &str,
171        permission_id: &str,
172    ) -> Result<DeleteFineTuningCheckpointPermissionResponse, OpenAIError> {
173        self.client
174            .delete(
175                format!("/fine_tuning/checkpoints/{fine_tuned_model_checkpoint}/permissions/{permission_id}")
176                    .as_str(),
177            )
178            .await
179    }
180}