use std::process::{Command, Child};
use std::collections::HashMap;
#[derive(Debug, Clone, Default)]
pub struct LocalOllamaRunner {
env_vars: HashMap<String, String>,
}
impl LocalOllamaRunner {
pub fn new() -> Self {
Self::default()
}
pub fn host(mut self, val: &str) -> Self {
self.env_vars.insert("OLLAMA_HOST".to_string(), val.to_string());
self
}
pub fn models_dir(mut self, val: &str) -> Self {
self.env_vars.insert("OLLAMA_MODELS".to_string(), val.to_string());
self
}
pub fn keep_alive(mut self, val: &str) -> Self {
self.env_vars.insert("OLLAMA_KEEP_ALIVE".to_string(), val.to_string());
self
}
pub fn max_queue(mut self, val: u32) -> Self {
self.env_vars.insert("OLLAMA_MAX_QUEUE".to_string(), val.to_string());
self
}
pub fn num_parallel(mut self, val: u32) -> Self {
self.env_vars.insert("OLLAMA_NUM_PARALLEL".to_string(), val.to_string());
self
}
pub fn max_loaded_models(mut self, val: u32) -> Self {
self.env_vars.insert("OLLAMA_MAX_LOADED_MODELS".to_string(), val.to_string());
self
}
pub fn context_length(mut self, val: u32) -> Self {
self.env_vars.insert("OLLAMA_CONTEXT_LENGTH".to_string(), val.to_string());
self
}
pub fn no_history(mut self, val: bool) -> Self {
if val {
self.env_vars.insert("OLLAMA_NOHISTORY".to_string(), "1".to_string());
}
self
}
pub fn disable_cloud(mut self, val: bool) -> Self {
if val {
self.env_vars.insert("OLLAMA_NO_CLOUD".to_string(), "1".to_string());
}
self
}
pub fn env(mut self, key: &str, val: &str) -> Self {
self.env_vars.insert(key.to_string(), val.to_string());
self
}
pub fn spawn(self) -> std::io::Result<Child> {
let mut cmd = Command::new("ollama");
cmd.arg("serve");
for (k, v) in self.env_vars {
cmd.env(k, v);
}
cmd.spawn()
}
}