Skip to main content

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(OapiError::UrlError)?;
30            url.path_segments_mut()
31                .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
32                .push("chat")
33                .push("completions")
34                .push(&self.completion_id);
35
36            for (key, value) in &self.extra_query {
37                url.query_pairs_mut().append_pair(key, value);
38            }
39
40            Ok(url.to_string())
41        }
42    }
43
44    impl GetNoStream for ChatRetrieveRequest {
45        type Response = crate::chat::ChatCompletion;
46    }
47}