use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ChatMessage {
pub id: String,
pub role: String,
pub content: String,
pub created_at: f64,
}
impl ChatMessage {
pub fn user(content: impl Into<String>) -> Self {
Self {
id: generate_id(),
role: "user".to_string(),
content: content.into(),
created_at: now(),
}
}
pub fn assistant(content: impl Into<String>) -> Self {
Self {
id: generate_id(),
role: "assistant".to_string(),
content: content.into(),
created_at: now(),
}
}
pub fn system(content: impl Into<String>) -> Self {
Self {
id: generate_id(),
role: "system".to_string(),
content: content.into(),
created_at: now(),
}
}
}
#[derive(Debug, Clone)]
pub struct ChatOptions {
pub provider: String,
pub api_key: String,
pub model: String,
pub system_prompt: Option<String>,
pub temperature: f32,
pub max_tokens: u32,
pub stream: bool,
pub initial_messages: Vec<ChatMessage>,
}
impl Default for ChatOptions {
fn default() -> Self {
Self {
provider: "openai".to_string(),
api_key: String::new(),
model: "gpt-4o-mini".to_string(),
system_prompt: None,
temperature: 0.7,
max_tokens: 4096,
stream: true,
initial_messages: Vec::new(),
}
}
}
#[derive(Debug, Clone)]
pub struct CompletionOptions {
pub provider: String,
pub api_key: String,
pub model: String,
pub system_prompt: Option<String>,
pub temperature: f32,
pub max_tokens: u32,
}
impl Default for CompletionOptions {
fn default() -> Self {
Self {
provider: "openai".to_string(),
api_key: String::new(),
model: "gpt-4o-mini".to_string(),
system_prompt: None,
temperature: 0.7,
max_tokens: 4096,
}
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TokenUsage {
pub prompt_tokens: u32,
pub completion_tokens: u32,
pub total_tokens: u32,
}
fn generate_id() -> String {
use std::time::{SystemTime, UNIX_EPOCH};
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_nanos();
format!("msg_{}", timestamp)
}
fn now() -> f64 {
use std::time::{SystemTime, UNIX_EPOCH};
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs_f64()
}