use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use crate::error::Result;
use crate::tools::ToolDefinition;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Role {
System,
User,
Assistant,
Tool,
}
impl std::fmt::Display for Role {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Role::System => write!(f, "system"),
Role::User => write!(f, "user"),
Role::Assistant => write!(f, "assistant"),
Role::Tool => write!(f, "tool"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
pub role: Role,
pub content: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_call_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_calls: Option<Vec<ToolCall>>,
}
impl Message {
pub fn system(content: impl Into<String>) -> Self {
Self {
role: Role::System,
content: content.into(),
tool_call_id: None,
tool_calls: None,
}
}
pub fn user(content: impl Into<String>) -> Self {
Self {
role: Role::User,
content: content.into(),
tool_call_id: None,
tool_calls: None,
}
}
pub fn assistant(content: impl Into<String>) -> Self {
Self {
role: Role::Assistant,
content: content.into(),
tool_call_id: None,
tool_calls: None,
}
}
pub fn tool(tool_call_id: impl Into<String>, content: impl Into<String>) -> Self {
Self {
role: Role::Tool,
content: content.into(),
tool_call_id: Some(tool_call_id.into()),
tool_calls: None,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCall {
pub id: String,
pub name: String,
pub arguments: String,
}
#[derive(Debug, Clone)]
pub struct LlmResponse {
pub content: String,
pub tool_calls: Vec<ToolCall>,
pub finish_reason: Option<String>,
pub usage: Option<Usage>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Usage {
pub prompt_tokens: u32,
pub completion_tokens: u32,
pub total_tokens: u32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LlmConfig {
pub model: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub api_key: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub base_url: Option<String>,
#[serde(default = "default_temperature")]
pub temperature: f32,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_tokens: Option<u32>,
}
fn default_temperature() -> f32 { 0.7 }
impl Default for LlmConfig {
fn default() -> Self {
Self {
model: "gpt-4o-mini".to_string(),
api_key: None,
base_url: None,
temperature: default_temperature(),
max_tokens: None,
}
}
}
impl LlmConfig {
pub fn new(model: impl Into<String>) -> Self {
Self {
model: model.into(),
..Default::default()
}
}
pub fn api_key(mut self, key: impl Into<String>) -> Self {
self.api_key = Some(key.into());
self
}
pub fn base_url(mut self, url: impl Into<String>) -> Self {
self.base_url = Some(url.into());
self
}
pub fn temperature(mut self, temp: f32) -> Self {
self.temperature = temp;
self
}
pub fn max_tokens(mut self, max: u32) -> Self {
self.max_tokens = Some(max);
self
}
}
#[async_trait]
pub trait LlmProvider: Send + Sync {
async fn chat(
&self,
messages: &[Message],
tools: Option<&[ToolDefinition]>,
) -> Result<LlmResponse>;
async fn chat_stream(
&self,
messages: &[Message],
tools: Option<&[ToolDefinition]>,
) -> Result<Box<dyn futures::Stream<Item = Result<String>> + Send + Unpin>>;
fn model(&self) -> &str;
}
pub struct OpenAiProvider {
config: LlmConfig,
client: reqwest::Client,
}
impl OpenAiProvider {
pub fn new(config: LlmConfig) -> Self {
Self {
config,
client: reqwest::Client::new(),
}
}
pub fn default_model() -> Self {
Self::new(LlmConfig::default())
}
}
#[async_trait]
impl LlmProvider for OpenAiProvider {
async fn chat(
&self,
messages: &[Message],
tools: Option<&[ToolDefinition]>,
) -> Result<LlmResponse> {
let api_key = self.config.api_key.clone()
.or_else(|| std::env::var("OPENAI_API_KEY").ok())
.ok_or_else(|| crate::error::Error::llm("OPENAI_API_KEY not set"))?;
let base_url = self.config.base_url.as_deref()
.unwrap_or("https://api.openai.com/v1");
let mut body = serde_json::json!({
"model": self.config.model,
"messages": messages,
"temperature": self.config.temperature,
});
if let Some(max_tokens) = self.config.max_tokens {
body["max_tokens"] = serde_json::json!(max_tokens);
}
if let Some(tools) = tools {
if !tools.is_empty() {
let tool_defs: Vec<_> = tools.iter().map(|t| {
serde_json::json!({
"type": "function",
"function": {
"name": t.name,
"description": t.description,
"parameters": t.parameters,
}
})
}).collect();
body["tools"] = serde_json::json!(tool_defs);
}
}
let response = self.client
.post(format!("{}/chat/completions", base_url))
.header("Authorization", format!("Bearer {}", api_key))
.header("Content-Type", "application/json")
.json(&body)
.send()
.await?;
if !response.status().is_success() {
let error_text = response.text().await.unwrap_or_default();
return Err(crate::error::Error::llm(format!("API error: {}", error_text)));
}
let data: serde_json::Value = response.json().await?;
let choice = data["choices"].get(0)
.ok_or_else(|| crate::error::Error::llm("No choices in response"))?;
let message = &choice["message"];
let content = message["content"].as_str().unwrap_or("").to_string();
let tool_calls = if let Some(calls) = message["tool_calls"].as_array() {
calls.iter().filter_map(|call| {
Some(ToolCall {
id: call["id"].as_str()?.to_string(),
name: call["function"]["name"].as_str()?.to_string(),
arguments: call["function"]["arguments"].as_str()?.to_string(),
})
}).collect()
} else {
vec![]
};
let usage = data["usage"].as_object().map(|u| Usage {
prompt_tokens: u["prompt_tokens"].as_u64().unwrap_or(0) as u32,
completion_tokens: u["completion_tokens"].as_u64().unwrap_or(0) as u32,
total_tokens: u["total_tokens"].as_u64().unwrap_or(0) as u32,
});
Ok(LlmResponse {
content,
tool_calls,
finish_reason: choice["finish_reason"].as_str().map(|s| s.to_string()),
usage,
})
}
async fn chat_stream(
&self,
_messages: &[Message],
_tools: Option<&[ToolDefinition]>,
) -> Result<Box<dyn futures::Stream<Item = Result<String>> + Send + Unpin>> {
Err(crate::error::Error::llm("Streaming not yet implemented"))
}
fn model(&self) -> &str {
&self.config.model
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_message_creation() {
let msg = Message::user("Hello");
assert_eq!(msg.role, Role::User);
assert_eq!(msg.content, "Hello");
}
#[test]
fn test_llm_config() {
let config = LlmConfig::new("gpt-4")
.temperature(0.5)
.max_tokens(1000);
assert_eq!(config.model, "gpt-4");
assert_eq!(config.temperature, 0.5);
assert_eq!(config.max_tokens, Some(1000));
}
}