openai-interface 0.6.0

A low-level Rust interface for the OpenAI API
Documentation
//! Module for retrieving a model instance, providing basic information about
//! the model such as the owner and permissioning.
//!
//! > ![warn] This module is untested!
//! > OpenAI-compatible providers are not known to implement this endpoint.
//! > If you encounter any issues, please report them on the repository.

pub mod request {
    use url::Url;

    use crate::{
        errors::OapiError,
        rest::get::{Get, GetNoStream},
    };

    /// Retrieve a model instance by its identifier.
    #[derive(Debug, Clone)]
    pub struct RetrieveModelRequest<'a> {
        /// The ID of the model to use for this request.
        pub model: &'a str,
    }

    impl Get for RetrieveModelRequest<'_> {
        /// base_url should look like <https://api.openai.com/v1>
        fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
            let mut url =
                Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
            url.path_segments_mut()
                .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
                .push("models")
                .push(self.model);

            Ok(url.to_string())
        }
    }

    impl GetNoStream for RetrieveModelRequest<'_> {
        type Response = crate::models::Model;
    }
}

#[cfg(test)]
mod tests {
    use super::request::RetrieveModelRequest;
    use crate::rest::get::Get;

    #[test]
    fn test_build_url() {
        let request = RetrieveModelRequest { model: "gpt-4o" };
        let url = request.build_url("https://api.openai.com/v1").unwrap();
        assert_eq!(url, "https://api.openai.com/v1/models/gpt-4o");
    }
}