Skip to main content

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(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            Ok(url.to_string())
36        }
37    }
38
39    impl DeleteNoStream for ChatDeleteRequest<'_> {
40        type Response = ChatCompletionDeleted;
41    }
42}
43
44pub mod response {
45    use serde::Deserialize;
46
47    /// The response of a chat completion deletion, e.g.
48    /// `{"object": "chat.completion.deleted", "id": "...", "deleted": true}`.
49    #[derive(Debug, Clone, Deserialize)]
50    pub struct ChatCompletionDeleted {
51        /// The ID of the chat completion that was deleted.
52        pub id: String,
53        /// Whether the chat completion was deleted.
54        pub deleted: bool,
55        /// The type of the object, which is always `chat.completion.deleted`.
56        pub object: ChatCompletionDeletedObject,
57    }
58
59    #[derive(Debug, Clone, Deserialize)]
60    pub enum ChatCompletionDeletedObject {
61        #[serde(rename = "chat.completion.deleted")]
62        ChatCompletionDeleted,
63    }
64
65    crate::impl_from_str!(ChatCompletionDeleted);
66}