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::Serialize;
7use url::Url;
8
9use crate::{
10    conversations::Conversation,
11    errors::OapiError,
12    rest::post::{Post, PostNoStream},
13};
14
15/// Creates a conversation.
16#[derive(Debug, Clone, Serialize, Default)]
17pub struct CreateConversationRequest {
18    /// Initial items to seed the conversation with, as raw JSON
19    /// (Responses-API input item shapes).
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub items: Option<Vec<serde_json::Value>>,
22    /// Arbitrary key-value metadata (up to 16 pairs) attached to the
23    /// conversation.
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub metadata: Option<std::collections::HashMap<String, String>>,
26    /// Additional JSON properties flattened into the request body, for
27    /// fields not covered by the typed struct.
28    #[serde(flatten, skip_serializing_if = "Option::is_none")]
29    pub extra_body: Option<serde_json::Map<String, serde_json::Value>>,
30}
31
32impl Post for CreateConversationRequest {
33    fn is_streaming(&self) -> bool {
34        false
35    }
36
37    /// Builds the URL for the request.
38    ///
39    /// `base_url` should be like <https://api.openai.com/v1>
40    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
41        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
42        url.path_segments_mut()
43            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
44            .push("conversations");
45        Ok(url.to_string())
46    }
47}
48
49impl PostNoStream for CreateConversationRequest {
50    type Response = Conversation;
51}