Skip to main content

openai_interface/fine_tuning/jobs/
list.rs

1//! List fine-tuning jobs via `GET /fine_tuning/jobs`.
2//!
3//! > ![warn] This module is untested!
4//! > If you encounter any issues, please report them on the repository.
5
6use url::Url;
7
8use crate::{
9    errors::OapiError,
10    fine_tuning::FineTuningJob,
11    rest::get::{Get, GetNoStream},
12};
13
14/// Lists fine-tuning jobs.
15#[derive(Debug, Clone, Default)]
16pub struct ListFineTuningJobsRequest<'a> {
17    /// A cursor for pagination: return jobs created *after* this job
18    /// ID.
19    pub after: Option<&'a str>,
20    /// A limit on the number of jobs to be returned (1-100, default
21    /// 20).
22    pub limit: Option<u32>,
23    /// Additional query parameters appended verbatim to the URL.
24    pub extra_query: Option<std::collections::HashMap<String, String>>,
25}
26
27impl Get for ListFineTuningJobsRequest<'_> {
28    /// Builds the URL for the request.
29    ///
30    /// `base_url` should be like <https://api.openai.com/v1>
31    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
32        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
33        url.path_segments_mut()
34            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
35            .push("fine_tuning")
36            .push("jobs");
37
38        let mut touched = false;
39        {
40            let mut pairs = url.query_pairs_mut();
41            if let Some(after) = self.after {
42                pairs.append_pair("after", after);
43                touched = true;
44            }
45            if let Some(limit) = self.limit {
46                pairs.append_pair("limit", &limit.to_string());
47                touched = true;
48            }
49            if let Some(extra_query) = &self.extra_query {
50                for (key, value) in extra_query {
51                    pairs.append_pair(key, value);
52                }
53                touched = true;
54            }
55        }
56        if !touched {
57            url.set_query(None);
58        }
59
60        Ok(url.to_string())
61    }
62}
63
64impl GetNoStream for ListFineTuningJobsRequest<'_> {
65    type Response = ListFineTuningJobsResponse;
66}
67
68/// The response of listing fine-tuning jobs.
69#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
70pub struct ListFineTuningJobsResponse {
71    /// The jobs on this page.
72    #[serde(default)]
73    pub data: Vec<FineTuningJob>,
74    /// Whether more jobs exist after this page.
75    #[serde(default)]
76    pub has_more: Option<bool>,
77    /// The ID of the first job on the page, for cursor pagination.
78    #[serde(default)]
79    pub first_id: Option<String>,
80    /// The ID of the last job on the page, for cursor pagination.
81    #[serde(default)]
82    pub last_id: Option<String>,
83    /// The object type (`list`), if the provider sends it.
84    #[serde(default)]
85    pub object: Option<String>,
86}
87
88crate::impl_from_str!(ListFineTuningJobsResponse);