Skip to main content

openai_interface/conversations/
create.rs

1//! Create a conversation via `POST /conversations`.
2//!
3//! > ![warn] This module is untested!
4//! > If you encounter any issues, please report them on the repository.
5
6use serde::{Deserialize, Serialize};
7use url::Url;
8
9use crate::{
10    conversations::Conversation,
11    errors::OapiError,
12    rest::post::{Post, PostNoStream},
13};
14
15crate::request_body! {
16    /// Creates a conversation.
17    #[derive(Debug, Clone, Serialize, Deserialize, Default)]
18    pub struct CreateConversationRequest {
19        /// Initial items to seed the conversation with, as raw JSON
20        /// (Responses-API input item shapes).
21        #[serde(skip_serializing_if = "Option::is_none")]
22        pub items: Option<Vec<serde_json::Value>>,
23        /// Arbitrary key-value metadata (up to 16 pairs) attached to the
24        /// conversation.
25        #[serde(skip_serializing_if = "Option::is_none")]
26        pub metadata: Option<std::collections::HashMap<String, String>>,
27    }
28}
29
30impl Post for CreateConversationRequest {
31    fn is_streaming(&self) -> bool {
32        false
33    }
34
35    /// Builds the URL for the request.
36    ///
37    /// `base_url` should be like <https://api.openai.com/v1>
38    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
39        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
40        url.path_segments_mut()
41            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
42            .push("conversations");
43        Ok(url.to_string())
44    }
45}
46
47impl PostNoStream for CreateConversationRequest {
48    type Response = Conversation;
49}