ai-provider-sdk 0.0.2

Async-first Rust SDK for OpenAI APIs
Documentation
//! Models 资源封装。提供模型列表与模型详情查询。

use std::sync::Arc;

use crate::error::{Error, Result};
use crate::path::encode_path_segment;
use crate::request_options::RequestOptions;
use crate::transport::Transport;
use crate::types::{Model, ModelList};

#[derive(Clone)]
/// Models 资源入口。
pub struct Models {
    transport: Arc<Transport>,
}

impl Models {
    pub(crate) fn new(transport: Arc<Transport>) -> Self {
        Self { transport }
    }

    pub async fn list(&self) -> Result<ModelList> {
        self.list_with_options(RequestOptions::default()).await
    }

    pub async fn list_with_options(&self, options: RequestOptions) -> Result<ModelList> {
        self.transport.get_json("/models", options).await
    }

    pub async fn retrieve(&self, model: impl AsRef<str>) -> Result<Model> {
        self.retrieve_with_options(model, RequestOptions::default())
            .await
    }

    pub async fn retrieve_with_options(
        &self,
        model: impl AsRef<str>,
        options: RequestOptions,
    ) -> Result<Model> {
        let model = model.as_ref();
        if model.is_empty() {
            return Err(Error::Config("model must not be empty".to_string()));
        }

        let model = encode_path_segment(model);
        self.transport
            .get_json(&format!("/models/{model}"), options)
            .await
    }
}