openai_req/model/
mod.rs

1use std::fmt::{Display, Formatter};
2use derive_more::Constructor;
3use reqwest::RequestBuilder;
4use crate::{ByUrlRequest, DeleteResponse, GetRequest, OpenAiClient};
5use with_id::WithRefId;
6use serde::{Serialize,Deserialize};
7use crate::fine_tunes::{FineTune, FineTuneListEntry};
8
9/// allows to get info about single model from its name.
10/// More details at https://platform.openai.com/docs/api-reference/models/retrieve
11/// # Usage example
12/// ```
13/// use openai_req::ByUrlRequest;
14/// use openai_req::model::ModelRequest;
15///
16/// let model_request = ModelRequest::new("model_name".to_string());
17/// let model_info = model_request.run(&lclient).await?;
18/// ```
19#[derive(Clone, Debug, Deserialize,Serialize,WithRefId,Constructor)]
20pub struct ModelRequest {
21    #[id]
22    model_name: String
23}
24
25
26impl TryFrom<FineTuneListEntry> for ModelRequest {
27    type Error = ConversionError;
28
29    fn try_from(value: FineTuneListEntry) -> Result<Self, Self::Error> {
30        Ok(ModelRequest {
31            model_name: value
32                .fine_tuned_model
33                .ok_or(ConversionError{
34                    message:"can only convert finished fine tune, that has fine tune model set"
35                })?
36        })
37    }
38}
39
40impl TryFrom<FineTune> for ModelRequest {
41
42    type Error = ConversionError;
43
44    fn try_from(value: FineTune) -> Result<Self, Self::Error> {
45        Ok(ModelRequest {
46            model_name: value
47                .fine_tuned_model
48                .ok_or(ConversionError{
49                    message:"can only convert finished fine tune, that has fine tune model set"
50                })?
51        })
52    }
53}
54
55
56impl ByUrlRequest<Model> for ModelRequest{
57    const ENDPOINT: &'static str = "/models/";
58    const SUFFIX: &'static str = "";
59}
60
61
62#[derive(Clone, Debug, Serialize, Deserialize)]
63pub struct Model {
64    pub id: String,
65    pub object: String,
66    pub created: i64,
67    pub owned_by: String,
68    pub permission: Vec<ModelPermission>,
69    pub root: String,
70    pub parent: Option<String>,
71}
72
73#[derive(Clone, Debug,Serialize, Deserialize)]
74pub struct ModelPermission {
75    pub  id: String,
76    pub object: String,
77    pub created: i64,
78    pub allow_create_engine: bool,
79    pub allow_sampling: bool,
80    pub allow_logprobs: bool,
81    pub allow_search_indices: bool,
82    pub allow_view: bool,
83    pub allow_fine_tuning: bool,
84    pub organization: String,
85    pub group: Option<String>,
86    pub is_blocking: bool,
87}
88
89/// List of all available models.
90/// More details at https://platform.openai.com/docs/api-reference/models/list
91/// # Usage example
92/// ```
93/// use openai_req::GetRequest;
94/// use openai_req::model::ModelListResponse;
95///
96/// let response = ModelListResponse::get(&client).await?;
97/// ```
98#[derive(Clone, Debug, Deserialize,Serialize)]
99pub struct ModelListResponse {
100    pub object: String,
101    pub data: Vec<Model>
102}
103
104impl GetRequest for ModelListResponse {
105    const ENDPOINT: &'static str = "/models";
106}
107
108/// allows deleting owned models. Part of the fine-tune API.
109/// More details at https://platform.openai.com/docs/api-reference/fine-tunes/delete-model
110/// # Usage example
111/// ```
112/// use openai_req::ByUrlRequest;
113/// use openai_req::model::ModelDeleteRequest;
114///
115/// let req = ModelDeleteRequest::new("model_name".to_string());
116/// let res = req.run(&client).await?;
117/// ```
118///
119#[derive(Serialize, Deserialize, Debug, Clone,WithRefId,Constructor)]
120pub struct ModelDeleteRequest {
121    #[id]
122    model_name: String
123}
124
125#[derive(Serialize, Deserialize, Debug, Clone)]
126pub struct ConversionError{
127    message:&'static str
128}
129
130impl Display for ConversionError {
131    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
132        write!(f,"{}",self.message)
133    }
134}
135
136impl std::error::Error for ConversionError {}
137
138
139impl TryFrom<FineTuneListEntry> for ModelDeleteRequest {
140    type Error = ConversionError;
141
142    fn try_from(value: FineTuneListEntry) -> Result<Self, Self::Error> {
143        Ok(ModelDeleteRequest {
144            model_name: value
145                .fine_tuned_model
146                .ok_or(ConversionError{
147                    message:"can only convert finished fine tune, that has fine tune model set"
148                })?
149        })
150    }
151}
152
153impl TryFrom<FineTune> for ModelDeleteRequest {
154
155    type Error = ConversionError;
156
157    fn try_from(value: FineTune) -> Result<Self, Self::Error> {
158        Ok(ModelDeleteRequest {
159            model_name: value
160                .fine_tuned_model
161                .ok_or(ConversionError{
162                    message:"can only convert finished fine tune, that has fine tune model set"
163                })?
164        })
165    }
166}
167
168
169impl ByUrlRequest<DeleteResponse> for ModelDeleteRequest {
170    const ENDPOINT: &'static str = "/models/";
171    const SUFFIX: &'static str = "";
172
173    fn builder(client:&OpenAiClient, final_url: String) -> RequestBuilder {
174        client.client.delete(final_url)
175    }
176}