use url::Url;
use super::ApiKey;
use crate::KitError;
use crate::error::ValidationViolation;
const DEFAULT_OLLAMA_LOCAL_BASE_URL: &str = "http://localhost:11434/api";
const DEFAULT_OLLAMA_CLOUD_BASE_URL: &str = "https://ollama.com/api";
#[derive(Clone)]
pub struct OllamaConfig {
base_url: Url,
api_key: Option<ApiKey>,
}
impl OllamaConfig {
#[must_use]
pub fn local() -> Self {
Self {
base_url: Url::parse(DEFAULT_OLLAMA_LOCAL_BASE_URL)
.expect("crate-owned local Ollama base URL should parse"),
api_key: None,
}
}
#[must_use]
pub fn local_with_key(api_key: ApiKey) -> Self {
Self {
base_url: Url::parse(DEFAULT_OLLAMA_LOCAL_BASE_URL)
.expect("crate-owned local Ollama base URL should parse"),
api_key: Some(api_key),
}
}
#[must_use]
pub fn cloud(api_key: ApiKey) -> Self {
Self {
base_url: Url::parse(DEFAULT_OLLAMA_CLOUD_BASE_URL)
.expect("crate-owned Ollama Cloud base URL should parse"),
api_key: Some(api_key),
}
}
pub fn with_base_url(mut self, base_url: Url) -> Result<Self, KitError> {
if base_url.path() != "/api" {
return Err(KitError::config(
ValidationViolation::InvalidValue {
field: "base_url.path",
rule: "exact /api path",
},
"Ollama base URL must point at the exact /api path",
));
}
self.base_url = base_url;
Ok(self)
}
pub(crate) fn base_url(&self) -> &Url {
&self.base_url
}
pub(crate) fn api_key(&self) -> Option<&ApiKey> {
self.api_key.as_ref()
}
}