use serde_json::Value;
const DEFAULT_API_URL: &str = "http://localhost:11434/v1/chat/completions";
#[derive(Debug, Clone)]
pub struct OllamaAdapter {
api_url: String,
}
impl OllamaAdapter {
pub fn new() -> Self {
Self {
api_url: DEFAULT_API_URL.to_string(),
}
}
pub fn with_url(url: impl Into<String>) -> Self {
Self {
api_url: url.into(),
}
}
fn clean_request(payload: &mut Value) {
if let Some(obj) = payload.as_object_mut() {
obj.remove("logprobs");
obj.remove("top_logprobs");
obj.remove("n");
obj.remove("frequency_penalty");
obj.remove("presence_penalty");
obj.remove("seed");
if let Some(val) = obj.remove("max_completion_tokens") {
obj.entry("max_tokens").or_insert(val);
}
}
}
}
impl Default for OllamaAdapter {
fn default() -> Self {
Self::new()
}
}
#[async_trait::async_trait]
impl super::base::ProviderAdapter for OllamaAdapter {
fn provider_name(&self) -> &str {
"ollama"
}
fn convert_request(&self, mut payload: Value) -> Value {
Self::clean_request(&mut payload);
payload
.as_object_mut()
.map(|obj| obj.remove("_reasoning_effort"));
payload
}
fn convert_response(&self, response: Value) -> Value {
response
}
fn api_url(&self) -> &str {
&self.api_url
}
}
#[cfg(test)]
#[path = "ollama_tests.rs"]
mod tests;