openai-interface 0.13.0

A low-level Rust interface for the OpenAI API
Documentation
//! Create a conversation via `POST /conversations`.
//!
//! > ![warn] This module is untested!
//! > If you encounter any issues, please report them on the repository.

use serde::{Deserialize, Serialize};
use url::Url;

use crate::{
    conversations::Conversation,
    errors::OapiError,
    rest::post::{Post, PostNoStream},
};

crate::request_body! {
    /// Creates a conversation.
    #[derive(Debug, Clone, Serialize, Deserialize, Default)]
    pub struct CreateConversationRequest {
        /// Initial items to seed the conversation with, as raw JSON
        /// (Responses-API input item shapes).
        #[serde(skip_serializing_if = "Option::is_none")]
        pub items: Option<Vec<serde_json::Value>>,
        /// Arbitrary key-value metadata (up to 16 pairs) attached to the
        /// conversation.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub metadata: Option<std::collections::HashMap<String, String>>,
    }
}

impl Post for CreateConversationRequest {
    fn is_streaming(&self) -> bool {
        false
    }

    /// Builds the URL for the request.
    ///
    /// `base_url` should be like <https://api.openai.com/v1>
    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
        url.path_segments_mut()
            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
            .push("conversations");
        Ok(url.to_string())
    }
}

impl PostNoStream for CreateConversationRequest {
    type Response = Conversation;
}