async_openai_compat/
fine_tuning.rs

1use serde::Serialize;
2
3use crate::{
4    config::Config,
5    error::OpenAIError,
6    types::{
7        CreateFineTuningJobRequest, FineTuningJob, ListFineTuningJobCheckpointsResponse,
8        ListFineTuningJobEventsResponse, ListPaginatedFineTuningJobsResponse,
9    },
10    Client,
11};
12
13/// Manage fine-tuning jobs to tailor a model to your specific training data.
14///
15/// Related guide: [Fine-tune models](https://platform.openai.com/docs/guides/fine-tuning)
16pub struct FineTuning<'c, C: Config> {
17    client: &'c Client<C>,
18}
19
20impl<'c, C: Config> FineTuning<'c, C> {
21    pub fn new(client: &'c Client<C>) -> Self {
22        Self { client }
23    }
24
25    /// Creates a job that fine-tunes a specified model from a given dataset.
26    ///
27    /// Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete.
28    ///
29    /// [Learn more about Fine-tuning](https://platform.openai.com/docs/guides/fine-tuning)
30    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
31    pub async fn create(
32        &self,
33        request: CreateFineTuningJobRequest,
34    ) -> Result<FineTuningJob, OpenAIError> {
35        self.client.post("/fine_tuning/jobs", request).await
36    }
37
38    /// List your organization's fine-tuning jobs
39    #[crate::byot(T0 = serde::Serialize, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
40    pub async fn list_paginated<Q>(
41        &self,
42        query: &Q,
43    ) -> Result<ListPaginatedFineTuningJobsResponse, OpenAIError>
44    where
45        Q: Serialize + ?Sized,
46    {
47        self.client
48            .get_with_query("/fine_tuning/jobs", &query)
49            .await
50    }
51
52    /// Gets info about the fine-tune job.
53    ///
54    /// [Learn more about Fine-tuning](https://platform.openai.com/docs/guides/fine-tuning)
55    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
56    pub async fn retrieve(&self, fine_tuning_job_id: &str) -> Result<FineTuningJob, OpenAIError> {
57        self.client
58            .get(format!("/fine_tuning/jobs/{fine_tuning_job_id}").as_str())
59            .await
60    }
61
62    /// Immediately cancel a fine-tune job.
63    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
64    pub async fn cancel(&self, fine_tuning_job_id: &str) -> Result<FineTuningJob, OpenAIError> {
65        self.client
66            .post(
67                format!("/fine_tuning/jobs/{fine_tuning_job_id}/cancel").as_str(),
68                (),
69            )
70            .await
71    }
72
73    /// Get fine-grained status updates for a fine-tune job.
74    #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
75    pub async fn list_events<Q>(
76        &self,
77        fine_tuning_job_id: &str,
78        query: &Q,
79    ) -> Result<ListFineTuningJobEventsResponse, OpenAIError>
80    where
81        Q: Serialize + ?Sized,
82    {
83        self.client
84            .get_with_query(
85                format!("/fine_tuning/jobs/{fine_tuning_job_id}/events").as_str(),
86                &query,
87            )
88            .await
89    }
90
91    #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
92    pub async fn list_checkpoints<Q>(
93        &self,
94        fine_tuning_job_id: &str,
95        query: &Q,
96    ) -> Result<ListFineTuningJobCheckpointsResponse, OpenAIError>
97    where
98        Q: Serialize + ?Sized,
99    {
100        self.client
101            .get_with_query(
102                format!("/fine_tuning/jobs/{fine_tuning_job_id}/checkpoints").as_str(),
103                &query,
104            )
105            .await
106    }
107}