use crate::core::tools::ToolDefinition;
use std::env;
#[derive(Debug, Clone)]
pub struct OllamaConfig {
pub base_url: String,
pub model: String,
pub temperature: Option<f32>,
pub max_tokens: Option<usize>,
pub top_p: Option<f32>,
pub streaming: bool,
pub tools: Option<Vec<ToolDefinition>>,
pub tool_choice: Option<String>,
}
impl Default for OllamaConfig {
fn default() -> Self {
Self {
base_url: "http://localhost:11434/v1".to_string(),
model: String::new(),
temperature: None,
max_tokens: None,
top_p: None,
streaming: false,
tools: None,
tool_choice: None,
}
}
}
impl OllamaConfig {
pub fn new(model: impl Into<String>) -> Self {
Self {
model: model.into(),
..Default::default()
}
}
#[deprecated(
since = "0.7.0",
note = "Use from_env_result() which returns Result<Self, String>"
)]
#[allow(deprecated)]
pub fn from_env() -> Self {
Self::from_env_result().unwrap_or_else(|_| Self::default())
}
pub fn from_env_result() -> Result<Self, String> {
let base_url = env::var("OLLAMA_BASE_URL")
.unwrap_or_else(|_| "http://localhost:11434/v1".to_string());
let model = env::var("OLLAMA_MODEL").unwrap_or_else(|_| String::new());
Ok(Self {
base_url,
model,
..Default::default()
})
}
pub fn with_base_url(mut self, url: impl Into<String>) -> Self {
self.base_url = url.into();
self
}
pub fn with_model(mut self, model: impl Into<String>) -> Self {
self.model = model.into();
self
}
pub fn with_temperature(mut self, temp: f32) -> Self {
self.temperature = Some(temp);
self
}
pub fn with_max_tokens(mut self, max: usize) -> Self {
self.max_tokens = Some(max);
self
}
pub fn with_streaming(mut self, streaming: bool) -> Self {
self.streaming = streaming;
self
}
pub fn with_tools(mut self, tools: Vec<ToolDefinition>) -> Self {
self.tools = Some(tools);
self
}
pub fn with_tool_choice(mut self, choice: impl Into<String>) -> Self {
self.tool_choice = Some(choice.into());
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ENV_TEST_LOCK;
use std::env;
fn save_and_set(key: &str, value: &str) -> Option<String> {
let old = env::var(key).ok();
env::set_var(key, value);
old
}
fn restore(key: &str, old: Option<String>) {
match old {
Some(v) => env::set_var(key, v),
None => env::remove_var(key),
}
}
#[test]
fn test_from_env_result_ok_when_vars_set() {
let _lock = crate::ENV_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let old_url = save_and_set("OLLAMA_BASE_URL", "http://custom:11434/v1");
let old_model = save_and_set("OLLAMA_MODEL", "llama3.2");
let config = OllamaConfig::from_env_result().unwrap();
assert_eq!(config.base_url, "http://custom:11434/v1");
assert_eq!(config.model, "llama3.2");
restore("OLLAMA_BASE_URL", old_url);
restore("OLLAMA_MODEL", old_model);
}
#[test]
fn test_from_env_result_uses_defaults_when_vars_missing() {
let _lock = crate::ENV_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let old_url = env::var("OLLAMA_BASE_URL").ok();
let old_model = env::var("OLLAMA_MODEL").ok();
env::remove_var("OLLAMA_BASE_URL");
env::remove_var("OLLAMA_MODEL");
let config = OllamaConfig::from_env_result().unwrap();
assert_eq!(config.base_url, "http://localhost:11434/v1");
assert_eq!(config.model, "");
restore("OLLAMA_BASE_URL", old_url);
restore("OLLAMA_MODEL", old_model);
}
}