use serde::{Deserialize, Serialize}; use crate::v1::{common, constants}; // ----------------------------------------------------------------------------- // Definitions #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ChatMessage { pub role: ChatMessageRole, pub content: String, } #[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)] #[allow(non_camel_case_types)] pub enum ChatMessageRole { assistant, user, } // ----------------------------------------------------------------------------- // Request #[derive(Debug)] pub struct ChatCompletionParams { pub tools: Option, pub temperature: Option, pub max_tokens: Option, pub top_p: Option, pub random_seed: Option, pub safe_prompt: Option, } impl Default for ChatCompletionParams { fn default() -> Self { Self { tools: None, temperature: None, max_tokens: None, top_p: None, random_seed: None, safe_prompt: None, } } } #[derive(Debug, Serialize, Deserialize)] pub struct ChatCompletionRequest { pub messages: Vec, pub model: constants::Model, #[serde(skip_serializing_if = "Option::is_none")] pub tools: Option, #[serde(skip_serializing_if = "Option::is_none")] pub temperature: Option, #[serde(skip_serializing_if = "Option::is_none")] pub max_tokens: Option, #[serde(skip_serializing_if = "Option::is_none")] pub top_p: Option, #[serde(skip_serializing_if = "Option::is_none")] pub random_seed: Option, pub stream: bool, #[serde(skip_serializing_if = "Option::is_none")] pub safe_prompt: 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 ChatCompletionRequest { pub fn new( model: constants::Model, messages: Vec, stream: bool, options: Option, ) -> Self { let ChatCompletionParams { tools, temperature, max_tokens, top_p, random_seed, safe_prompt, } = options.unwrap_or_default(); Self { messages, model, tools, temperature, max_tokens, top_p, random_seed, stream, safe_prompt, } } } // ----------------------------------------------------------------------------- // Response #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ChatCompletionResponse { 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 ChatCompletionResponseChoice { pub index: u32, pub message: ChatMessage, pub finish_reason: String, // TODO Check this prop (seen in API responses but undocumented). // pub logprobs: ??? } // ----------------------------------------------------------------------------- // Stream #[derive(Debug, Deserialize)] pub struct ChatCompletionStreamChunk { pub id: String, pub object: String, /// Unix timestamp (in seconds). pub created: u32, pub model: constants::Model, pub choices: Vec, // TODO Check this prop (seen in API responses but undocumented). // pub usage: ???, } #[derive(Debug, Deserialize)] pub struct ChatCompletionStreamChunkChoice { pub index: u32, pub delta: ChatCompletionStreamChunkChoiceDelta, pub finish_reason: Option, // TODO Check this prop (seen in API responses but undocumented). // pub logprobs: ???, } #[derive(Debug, Deserialize)] pub struct ChatCompletionStreamChunkChoiceDelta { pub role: Option, pub content: String, }