use serde::{Deserialize, Serialize};
use serde_json::json;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum LlmProvider {
LmStudio,
Ollama,
OpenAi,
Custom(String),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Llm {
pub endpoint: String,
pub model: String,
pub api_key: Option<String>,
pub temperature: f64,
pub top_p: f64,
pub max_tokens: Option<usize>,
}
impl Default for Llm {
fn default() -> Self {
Self::lm_studio()
}
}
impl Llm {
pub fn lm_studio() -> Self {
Self {
endpoint: "http://localhost:1234/v1/chat/completions".to_string(),
model: "default".to_string(),
api_key: None,
temperature: 0.7,
top_p: 0.95,
max_tokens: Some(2048),
}
}
pub fn ollama() -> Self {
Self {
endpoint: "http://localhost:11434/v1/chat/completions".to_string(),
model: "llama3".to_string(),
api_key: None,
temperature: 0.7,
top_p: 0.9,
max_tokens: Some(2048),
}
}
pub fn openai(api_key: impl Into<String>) -> Self {
Self {
endpoint: "https://api.openai.com/v1/chat/completions".to_string(),
model: "gpt-4o-mini".to_string(),
api_key: Some(api_key.into()),
temperature: 0.7,
top_p: 1.0,
max_tokens: Some(2048),
}
}
pub fn model(mut self, model: impl Into<String>) -> Self {
self.model = model.into();
self
}
pub fn endpoint(mut self, endpoint: impl Into<String>) -> Self {
self.endpoint = endpoint.into();
self
}
pub fn temperature(mut self, temp: f64) -> Self {
self.temperature = temp;
self
}
pub fn top_p(mut self, top_p: f64) -> Self {
self.top_p = top_p;
self
}
pub fn max_tokens(mut self, tokens: usize) -> Self {
self.max_tokens = Some(tokens);
self
}
pub fn build_payload(
&self,
messages: &[crate::components::ChatMessage],
stream: bool,
) -> serde_json::Value {
let msgs: Vec<serde_json::Value> = messages
.iter()
.map(|m| {
json!({
"role": m.role,
"content": m.content,
})
})
.collect();
json!({
"model": self.model,
"messages": msgs,
"temperature": self.temperature,
"top_p": self.top_p,
"stream": stream,
"max_tokens": self.max_tokens,
})
}
}