openai_interface/chat/
delete.rs

1//! Module for deleting a stored chat completion.
2//!
3//! This module is untested! If you encounter any issues,
4//! please report them on the repository.
5
6pub mod request {
7    use url::Url;
8
9    use crate::{
10        chat::delete::response::ChatCompletionDeleted,
11        errors::OapiError,
12        rest::delete::{Delete, DeleteNoStream},
13    };
14
15    /// Delete a stored chat completion.
16    ///
17    /// Only Chat Completions that have been created
18    /// with the `store` parameter set to `true` can be deleted.
19    #[derive(Debug, Clone)]
20    pub struct ChatDeleteRequest<'a> {
21        pub completion_id: &'a str,
22    }
23
24    impl Delete for ChatDeleteRequest<'_> {
25        /// Builds the URL for the request.
26        ///
27        /// `base_url` should be like <https://api.openai.com/v1>
28        fn build_url(&self, base_url: &str) -> Result<String, crate::errors::OapiError> {
29            let mut url = Url::parse(base_url).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("delete")
34                .push(self.completion_id);
35            Ok(url.to_string())
36        }
37    }
38
39    impl DeleteNoStream for ChatDeleteRequest<'_> {
40        type Response = ChatCompletionDeleted;
41    }
42}
43
44pub mod response {
45    use std::str::FromStr;
46
47    use serde::Deserialize;
48
49    use crate::errors::OapiError;
50
51    #[derive(Debug, Clone, Deserialize)]
52    #[serde(tag = "type")]
53    pub enum ChatCompletionDeleted {
54        /// The type of object being deleted.
55        #[serde(rename = "chat.completion.deleted")]
56        ChatCompletionDeleted {
57            /// The ID of the chat completion that was deleted.
58            id: String,
59            /// Whether the chat completion was deleted.
60            deleted: bool,
61        },
62    }
63
64    impl FromStr for ChatCompletionDeleted {
65        type Err = OapiError;
66
67        fn from_str(s: &str) -> Result<Self, Self::Err> {
68            let parse_result: Result<Self, _> =
69                serde_json::from_str(s).map_err(|e| OapiError::DeserializationError(e.to_string()));
70            parse_result
71        }
72    }
73}