use serde::{Deserialize, Serialize};
use std::time::{SystemTime, UNIX_EPOCH};
#[derive(Debug, Clone, Serialize)]
pub struct ChatMessage {
pub role: String,
pub content: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub images: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_calls: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_call_id: Option<String>,
}
impl<'de> Deserialize<'de> for ChatMessage {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
struct Raw {
role: String,
#[serde(default)]
content: Option<serde_json::Value>,
#[serde(default)]
images: Option<Vec<String>>,
#[serde(default)]
name: Option<String>,
#[serde(default)]
tool_calls: Option<serde_json::Value>,
#[serde(default)]
tool_call_id: Option<String>,
}
let raw = Raw::deserialize(deserializer)?;
let mut images = raw.images.unwrap_or_default();
let content = match raw.content {
None | Some(serde_json::Value::Null) => String::new(),
Some(serde_json::Value::String(s)) => s,
Some(serde_json::Value::Array(parts)) => {
let mut text = String::new();
for part in parts {
match part.get("type").and_then(|t| t.as_str()) {
Some("text") => {
if let Some(t) = part.get("text").and_then(|t| t.as_str()) {
if !text.is_empty() {
text.push('\n');
}
text.push_str(t);
}
}
Some("image_url") => {
if let Some(url) = part
.get("image_url")
.and_then(|i| i.get("url"))
.and_then(|u| u.as_str())
{
images.push(url.to_string());
}
}
_ => {}
}
}
text
}
Some(other) => {
return Err(serde::de::Error::custom(format!(
"message content must be a string or an array of parts, got: {other}"
)))
}
};
Ok(ChatMessage {
role: raw.role,
content,
images: if images.is_empty() { None } else { Some(images) },
name: raw.name,
tool_calls: raw.tool_calls,
tool_call_id: raw.tool_call_id,
})
}
}
impl ChatMessage {
pub fn text(role: impl Into<String>, content: impl Into<String>) -> Self {
Self {
role: role.into(),
content: content.into(),
images: None,
name: None,
tool_calls: None,
tool_call_id: None,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenerationOptions {
#[serde(default = "GenerationOptions::default_max_tokens")]
pub max_tokens: u32,
#[serde(default = "GenerationOptions::default_temperature")]
pub temperature: f32,
#[serde(default = "GenerationOptions::default_top_p")]
pub top_p: f32,
#[serde(default = "GenerationOptions::default_top_k")]
pub top_k: i32,
#[serde(default = "GenerationOptions::default_min_p")]
pub min_p: f32,
#[serde(default = "GenerationOptions::default_repetition_penalty")]
pub repetition_penalty: f32,
#[serde(default)]
pub stop_sequences: Vec<String>,
}
impl GenerationOptions {
fn default_max_tokens() -> u32 {
256
}
fn default_temperature() -> f32 {
0.7
}
fn default_top_p() -> f32 {
0.9
}
fn default_top_k() -> i32 {
40
}
fn default_min_p() -> f32 {
0.05
}
fn default_repetition_penalty() -> f32 {
1.1
}
}
impl Default for GenerationOptions {
fn default() -> Self {
Self {
max_tokens: Self::default_max_tokens(),
temperature: Self::default_temperature(),
top_p: Self::default_top_p(),
top_k: Self::default_top_k(),
min_p: Self::default_min_p(),
repetition_penalty: Self::default_repetition_penalty(),
stop_sequences: vec![],
}
}
}
#[derive(Debug, Serialize, Deserialize, Default, Clone)]
pub struct UsageInfo {
pub prompt_tokens: u32,
pub completion_tokens: u32,
pub total_tokens: u32,
}
#[derive(Debug, Deserialize)]
pub struct ChatCompletionRequest {
pub model: String,
pub messages: Vec<ChatMessage>,
#[serde(default)]
pub max_tokens: Option<u32>,
#[serde(default)]
pub temperature: Option<f32>,
#[serde(default)]
pub top_p: Option<f32>,
#[serde(default)]
pub top_k: Option<i32>,
#[serde(default)]
pub min_p: Option<f32>,
#[serde(default)]
pub repetition_penalty: Option<f32>,
#[serde(default)]
pub stop: Option<serde_json::Value>,
#[serde(default)]
pub stream: Option<bool>,
#[serde(default)]
pub tools: Option<Vec<Tool>>,
}
impl ChatCompletionRequest {
pub fn to_generation_options(&self) -> GenerationOptions {
let defaults = GenerationOptions::default();
let stop_sequences = match &self.stop {
Some(serde_json::Value::String(s)) => vec![s.clone()],
Some(serde_json::Value::Array(arr)) => arr
.iter()
.filter_map(|v| v.as_str().map(String::from))
.collect(),
_ => vec![],
};
GenerationOptions {
max_tokens: self.max_tokens.unwrap_or(defaults.max_tokens),
temperature: self.temperature.unwrap_or(defaults.temperature),
top_p: self.top_p.unwrap_or(defaults.top_p),
top_k: self.top_k.unwrap_or(defaults.top_k),
min_p: self.min_p.unwrap_or(defaults.min_p),
repetition_penalty: self.repetition_penalty.unwrap_or(defaults.repetition_penalty),
stop_sequences,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Tool {
#[serde(rename = "type")]
pub tool_type: String,
pub function: FunctionDef,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FunctionDef {
pub name: String,
pub description: Option<String>,
pub parameters: Option<serde_json::Value>,
}
#[derive(Debug, Serialize)]
pub struct ChatCompletionResponse {
pub id: String,
pub object: String,
pub created: u64,
pub model: String,
pub choices: Vec<ChatChoice>,
pub usage: UsageInfo,
}
impl ChatCompletionResponse {
pub fn new(id: String, model: String, choices: Vec<ChatChoice>, usage: UsageInfo) -> Self {
let created = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
Self {
id,
object: "chat.completion".to_string(),
created,
model,
choices,
usage,
}
}
}
#[derive(Debug, Serialize)]
pub struct ChatChoice {
pub index: u32,
pub message: AssistantMessage,
pub finish_reason: String,
}
#[derive(Debug, Serialize)]
pub struct AssistantMessage {
pub role: String,
pub content: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_calls: Option<Vec<ToolCall>>,
}
#[derive(Debug, Serialize)]
pub struct ToolCall {
pub id: String,
#[serde(rename = "type")]
pub call_type: String,
pub function: FunctionCallResult,
}
#[derive(Debug, Serialize)]
pub struct FunctionCallResult {
pub name: String,
pub arguments: String,
}
#[derive(Debug, Serialize)]
pub struct ChatCompletionChunk {
pub id: String,
pub object: String,
pub created: u64,
pub model: String,
pub choices: Vec<StreamChoice>,
}
impl ChatCompletionChunk {
pub fn new(
id: String,
model: String,
delta: DeltaContent,
finish_reason: Option<String>,
) -> Self {
let created = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
Self {
id,
object: "chat.completion.chunk".to_string(),
created,
model,
choices: vec![StreamChoice {
index: 0,
delta,
finish_reason,
}],
}
}
}
#[derive(Debug, Serialize)]
pub struct StreamChoice {
pub index: u32,
pub delta: DeltaContent,
pub finish_reason: Option<String>,
}
#[derive(Debug, Serialize, Default)]
pub struct DeltaContent {
#[serde(skip_serializing_if = "Option::is_none")]
pub role: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_calls: Option<serde_json::Value>,
}
#[derive(Debug, Deserialize)]
pub struct EmbeddingRequest {
pub model: String,
pub input: EmbeddingInput,
#[serde(default)]
pub encoding_format: Option<String>,
}
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum EmbeddingInput {
Single(String),
Multiple(Vec<String>),
}
impl EmbeddingInput {
pub fn into_vec(self) -> Vec<String> {
match self {
Self::Single(s) => vec![s],
Self::Multiple(v) => v,
}
}
}
#[derive(Debug, Serialize)]
pub struct EmbeddingResponse {
pub object: String,
pub data: Vec<EmbeddingData>,
pub model: String,
pub usage: UsageInfo,
}
#[derive(Debug, Serialize)]
pub struct EmbeddingData {
pub object: String,
pub embedding: Vec<f32>,
pub index: u32,
}
#[derive(Debug, Serialize)]
pub struct ModelListResponse {
pub object: String,
pub data: Vec<ModelInfo>,
}
#[derive(Debug, Serialize)]
pub struct ModelInfo {
pub id: String,
pub object: String,
pub created: u64,
pub owned_by: String,
}
#[derive(Debug, Serialize)]
pub struct ErrorResponse {
pub error: ErrorDetail,
}
#[derive(Debug, Serialize)]
pub struct ErrorDetail {
pub message: String,
#[serde(rename = "type")]
pub error_type: String,
pub param: Option<String>,
pub code: Option<String>,
}
impl ErrorResponse {
pub fn new(message: impl Into<String>, error_type: impl Into<String>) -> Self {
Self {
error: ErrorDetail {
message: message.into(),
error_type: error_type.into(),
param: None,
code: None,
},
}
}
pub fn invalid_request(message: impl Into<String>) -> Self {
Self::new(message, "invalid_request_error")
}
pub fn server_error(message: impl Into<String>) -> Self {
Self::new(message, "server_error")
}
}