Skip to main content

openai_interface/models/
retrieve.rs

1//! Module for retrieving a model instance, providing basic information about
2//! the model such as the owner and permissioning.
3//!
4//! > ![warn] This module is untested!
5//! > OpenAI-compatible providers are not known to implement this endpoint.
6//! > If you encounter any issues, please report them on the repository.
7
8pub mod request {
9    use url::Url;
10
11    use crate::{
12        errors::OapiError,
13        rest::get::{Get, GetNoStream},
14    };
15
16    /// Retrieve a model instance by its identifier.
17    #[derive(Debug, Clone)]
18    pub struct RetrieveModelRequest<'a> {
19        /// The ID of the model to use for this request.
20        pub model: &'a str,
21    }
22
23    impl Get for RetrieveModelRequest<'_> {
24        /// base_url should look like <https://api.openai.com/v1>
25        fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
26            let mut url =
27                Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
28            url.path_segments_mut()
29                .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
30                .push("models")
31                .push(self.model);
32
33            Ok(url.to_string())
34        }
35    }
36
37    impl GetNoStream for RetrieveModelRequest<'_> {
38        type Response = crate::models::Model;
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::request::RetrieveModelRequest;
45    use crate::rest::get::Get;
46
47    #[test]
48    fn test_build_url() {
49        let request = RetrieveModelRequest { model: "gpt-4o" };
50        let url = request.build_url("https://api.openai.com/v1").unwrap();
51        assert_eq!(url, "https://api.openai.com/v1/models/gpt-4o");
52    }
53}