openai_fork/
models.rs

1//! List and describe the various models available in the API.
2//! You can refer to the [Models](https://beta.openai.com/docs/models)
3//! documentation to understand what models are available and the differences between them.
4
5use super::{openai_get, ApiResponseOrError};
6use serde::Deserialize;
7
8#[derive(Deserialize, Clone)]
9pub struct Model {
10    pub id: String,
11    pub object: String,
12    pub created: u32,
13    pub owned_by: String,
14}
15
16#[derive(Deserialize, Clone)]
17pub struct ModelPermission {
18    pub id: String,
19    pub created: u32,
20    pub allow_create_engine: bool,
21    pub allow_sampling: bool,
22    pub allow_logprobs: bool,
23    pub allow_search_indices: bool,
24    pub allow_view: bool,
25    pub allow_fine_tuning: bool,
26    pub organization: String,
27    pub group: Option<String>,
28    pub is_blocking: bool,
29}
30
31impl Model {
32    //! Retrieves a model instance,
33    //! providing basic information about the model such as the owner and permissioning.
34    pub async fn from(id: &str) -> ApiResponseOrError<Self> {
35        openai_get(&format!("models/{id}")).await
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42    use crate::set_key;
43    use crate::tests::DEFAULT_LEGACY_MODEL;
44    use dotenvy::dotenv;
45    use std::env;
46
47    #[tokio::test]
48    async fn model() {
49        dotenv().ok();
50        set_key(env::var("OPENAI_KEY").unwrap());
51        let model = Model::from(DEFAULT_LEGACY_MODEL).await.unwrap();
52        assert_eq!(model.id, DEFAULT_LEGACY_MODEL);
53    }
54}