use super::error::Result;
use crate::{error::FallibleResponse, Client};
use chrono::{DateTime, Utc};
use serde::Deserialize;
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct Model {
pub id: String,
#[serde(rename = "created", with = "chrono::serde::ts_seconds")]
pub created_at: DateTime<Utc>,
pub owned_by: String,
pub permission: Vec<Permission>,
#[serde(default)]
pub root: Option<String>,
#[serde(default)]
pub parent: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct Permission {
#[serde(rename = "created", with = "chrono::serde::ts_seconds")]
pub created_at: DateTime<Utc>,
pub id: String,
pub allow_create_engine: bool,
pub allow_sampling: bool,
pub allow_logprobs: bool,
pub allow_search_indices: bool,
pub allow_view: bool,
pub allow_fine_tuning: bool,
pub organization: String,
pub is_blocking: bool,
}
impl Model {
pub async fn get(model: impl AsRef<str>, client: impl AsRef<Client>) -> Result<Model> {
let models = client
.as_ref()
.get(format!(
"https://api.openai.com/v1/models/{}",
model.as_ref()
))
.send()
.await?
.json::<FallibleResponse<Model>>()
.await?
.into_result()?;
return Ok(models);
}
}
pub async fn models(client: impl AsRef<Client>) -> Result<Vec<Model>> {
#[derive(Debug, Clone, Deserialize)]
pub struct Models {
data: Vec<Model>,
}
let models = client
.as_ref()
.get("https://api.openai.com/v1/models")
.send()
.await?
.json::<FallibleResponse<Models>>()
.await?
.into_result()?;
return Ok(models.data);
}