openai_interface/chat/
retrieve.rs

1//! Module for retrieving chat completions from OpenAI API.
2//!
3//! > ![warn] This module is untested!
4//! > If you encounter any issues, please report them on the GitHub repository.
5
6pub mod request {
7    use std::collections::HashMap;
8
9    use url::Url;
10
11    use crate::{
12        errors::OapiError,
13        rest::get::{Get, GetNoStream},
14    };
15
16    /// Get a stored chat completion.
17    ///
18    /// Only Chat Completions that have been created with
19    /// the `store` parameter set to `true` will be returned.
20    pub struct ChatRetrieveRequest {
21        pub completion_id: String,
22        pub extra_query: HashMap<String, String>,
23    }
24
25    impl Get for ChatRetrieveRequest {
26        /// base_url should look like <https://api.openai.com/v1>
27        fn build_url(&self, base_url: &str) -> Result<String, crate::errors::OapiError> {
28            let mut url =
29                Url::parse(base_url.trim_end_matches('/')).map_err(|e| OapiError::UrlError(e))?;
30            url.path_segments_mut()
31                .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
32                .push("chat")
33                .push(&self.completion_id);
34
35            Ok(url.to_string())
36        }
37    }
38
39    impl GetNoStream for ChatRetrieveRequest {
40        type Response = crate::chat::ChatCompletion;
41    }
42}