use serde::{Deserialize, Serialize}; use crate::v1::{common, constants, tool}; // ----------------------------------------------------------------------------- // Definitions #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ChatMessage { pub role: ChatMessageRole, pub content: String, pub tool_calls: Option>, } impl ChatMessage { pub fn new_assistant_message(content: &str, tool_calls: Option>) -> Self { Self { role: ChatMessageRole::Assistant, content: content.to_string(), tool_calls, } } pub fn new_user_message(content: &str) -> Self { Self { role: ChatMessageRole::User, content: content.to_string(), tool_calls: None, } } } #[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)] pub enum ChatMessageRole { #[serde(rename = "assistant")] Assistant, #[serde(rename = "user")] User, } // ----------------------------------------------------------------------------- // Request #[derive(Debug)] pub struct ChatParams { pub max_tokens: Option, pub random_seed: Option, pub safe_prompt: Option, pub temperature: Option, pub tool_choice: Option, pub tools: Option>, pub top_p: Option, } impl Default for ChatParams { fn default() -> Self { Self { max_tokens: None, random_seed: None, safe_prompt: None, temperature: None, tool_choice: None, tools: None, top_p: None, } } } #[derive(Debug, Serialize, Deserialize)] pub struct ChatRequest { pub messages: Vec, pub model: constants::Model, #[serde(skip_serializing_if = "Option::is_none")] pub max_tokens: Option, #[serde(skip_serializing_if = "Option::is_none")] pub random_seed: Option, #[serde(skip_serializing_if = "Option::is_none")] pub safe_prompt: Option, pub stream: bool, #[serde(skip_serializing_if = "Option::is_none")] pub temperature: Option, #[serde(skip_serializing_if = "Option::is_none")] pub tool_choice: Option, #[serde(skip_serializing_if = "Option::is_none")] pub tools: Option>, #[serde(skip_serializing_if = "Option::is_none")] pub top_p: Option, // TODO Check this prop (seen in official Python client but not in API doc). // pub tool_choice: Option, // TODO Check this prop (seen in official Python client but not in API doc). // pub response_format: Option, } impl ChatRequest { pub fn new( model: constants::Model, messages: Vec, stream: bool, options: Option, ) -> Self { let ChatParams { max_tokens, random_seed, safe_prompt, temperature, tool_choice, tools, top_p, } = options.unwrap_or_default(); Self { messages, model, max_tokens, random_seed, safe_prompt, stream, temperature, tool_choice, tools, top_p, } } } // ----------------------------------------------------------------------------- // Response #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ChatResponse { pub id: String, pub object: String, /// Unix timestamp (in seconds). pub created: u32, pub model: constants::Model, pub choices: Vec, pub usage: common::ResponseUsage, } #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ChatResponseChoice { pub index: u32, pub message: ChatMessage, pub finish_reason: ChatResponseChoiceFinishReason, // TODO Check this prop (seen in API responses but undocumented). // pub logprobs: ??? } #[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)] pub enum ChatResponseChoiceFinishReason { #[serde(rename = "stop")] Stop, #[serde(rename = "tool_calls")] ToolCalls, }