async_openai_alt/
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    pub async fn create(
31        &self,
32        request: CreateFineTuningJobRequest,
33    ) -> Result<FineTuningJob, OpenAIError> {
34        self.client.post("/fine_tuning/jobs", request).await
35    }
36
37    /// List your organization's fine-tuning jobs
38    pub async fn list_paginated<Q>(
39        &self,
40        query: &Q,
41    ) -> Result<ListPaginatedFineTuningJobsResponse, OpenAIError>
42    where
43        Q: Serialize + ?Sized,
44    {
45        self.client.get_with_query("/fine_tuning/jobs", query).await
46    }
47
48    /// Gets info about the fine-tune job.
49    ///
50    /// [Learn more about Fine-tuning](https://platform.openai.com/docs/guides/fine-tuning)
51    pub async fn retrieve(&self, fine_tuning_job_id: &str) -> Result<FineTuningJob, OpenAIError> {
52        self.client
53            .get(format!("/fine_tuning/jobs/{fine_tuning_job_id}").as_str())
54            .await
55    }
56
57    /// Immediately cancel a fine-tune job.
58    pub async fn cancel(&self, fine_tuning_job_id: &str) -> Result<FineTuningJob, OpenAIError> {
59        self.client
60            .post(
61                format!("/fine_tuning/jobs/{fine_tuning_job_id}/cancel").as_str(),
62                (),
63            )
64            .await
65    }
66
67    /// Get fine-grained status updates for a fine-tune job.
68    pub async fn list_events<Q>(
69        &self,
70        fine_tuning_job_id: &str,
71        query: &Q,
72    ) -> Result<ListFineTuningJobEventsResponse, OpenAIError>
73    where
74        Q: Serialize + ?Sized,
75    {
76        self.client
77            .get_with_query(
78                format!("/fine_tuning/jobs/{fine_tuning_job_id}/events").as_str(),
79                query,
80            )
81            .await
82    }
83
84    pub async fn list_checkpoints<Q>(
85        &self,
86        fine_tuning_job_id: &str,
87        query: &Q,
88    ) -> Result<ListFineTuningJobCheckpointsResponse, OpenAIError>
89    where
90        Q: Serialize + ?Sized,
91    {
92        self.client
93            .get_with_query(
94                format!("/fine_tuning/jobs/{fine_tuning_job_id}/checkpoints").as_str(),
95                query,
96            )
97            .await
98    }
99}