openai_interface/chat/
update.rs

1//! Module for updating stored chat completions.
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 serde::Serialize;
10
11    use crate::rest::post::{Post, PostNoStream};
12
13    /// Modify a stored chat completion.
14    ///
15    /// Only Chat Completions that have been created
16    /// with the `store` parameter set to `true` can be modified. Currently, the only
17    /// supported modification is to update the `metadata` field.
18    #[derive(Debug, Serialize, Default, Clone)]
19    pub struct ChatUpdate<'a> {
20        /// The ID of the completion to update.
21        #[serde(skip_serializing)]
22        pub completion_id: &'a str,
23        /// Add additional JSON properties to the request
24        #[serde(skip_serializing_if = "Option::is_none")]
25        pub extra_body: Option<serde_json::Map<String, serde_json::Value>>,
26        /// Add additional query parameters to the request
27        #[serde(skip_serializing)]
28        pub extra_query: HashMap<&'a str, &'a str>,
29        /// Set of 16 key-value pairs that can be attached to an object. This can be useful
30        /// for storing additional information about the object in a structured format, and
31        /// querying for objects via API or the dashboard.
32        ///
33        /// Keys are strings with a maximum length of 64 characters. Values are strings with
34        /// a maximum length of 512 characters.
35        pub metadata: Option<HashMap<&'a str, &'a str>>,
36    }
37
38    impl ChatUpdate<'_> {
39        pub fn build_url(base_url: impl AsRef<str>, completion_id: impl AsRef<str>) -> String {
40            let mut url = url::Url::parse(base_url.as_ref()).expect("Invalid base URL");
41            url.path_segments_mut()
42                .expect("Cannot modify URL path")
43                .push("chat")
44                .push("completions")
45                .push(completion_id.as_ref());
46            url.to_string()
47        }
48    }
49
50    impl Post for ChatUpdate<'_> {
51        fn is_streaming(&self) -> bool {
52            false
53        }
54
55        /// Builds the URL for the request.
56        ///
57        /// `base_url` should be like <https://api.openai.com/v1>
58        fn build_url(&self, base_url: &str) -> Result<String, crate::errors::OapiError> {
59            let mut url =
60                url::Url::parse(base_url).map_err(|e| crate::errors::OapiError::UrlError(e))?;
61            url.path_segments_mut()
62                .map_err(|_| crate::errors::OapiError::UrlCannotBeBase(base_url.to_string()))?
63                .push("chat")
64                .push("completions")
65                .push(self.completion_id);
66            Ok(url.to_string())
67        }
68    }
69
70    impl PostNoStream for ChatUpdate<'_> {
71        type Response = crate::chat::ChatCompletion;
72    }
73}