Skip to main content

llm/providers/local/
ollama.rs

1#![doc = include_str!(concat!(env!("OUT_DIR"), "/docs/ollama.md"))]
2
3use super::util::get_local_config;
4use crate::providers::openai::OpenAiChatProvider;
5use crate::{ProviderConnectionConfig, ProviderFactory, Result};
6use async_openai::{Client, config::OpenAIConfig};
7
8pub struct OllamaProvider {
9    model: String,
10    client: Client<OpenAIConfig>,
11}
12
13impl OllamaProvider {
14    pub fn new(model: &str, base_url: &str) -> Self {
15        Self { model: model.to_string(), client: Client::with_config(get_local_config(base_url)) }
16    }
17
18    pub fn default(model: &str) -> Self {
19        Self { model: model.to_string(), client: Client::with_config(get_local_config("http://localhost:11434/v1")) }
20    }
21}
22
23impl ProviderFactory for OllamaProvider {
24    async fn from_env() -> Result<Self> {
25        Self::from_env_with_connection(ProviderConnectionConfig::default()).await
26    }
27
28    async fn from_env_with_connection(connection: ProviderConnectionConfig) -> Result<Self> {
29        let base_url = connection.base_url.as_deref().unwrap_or("http://localhost:11434/v1");
30        Ok(Self { model: String::new(), client: Client::with_config(get_local_config(base_url)) })
31    }
32
33    fn with_model(mut self, model: &str) -> Self {
34        self.model = model.to_string();
35        self
36    }
37}
38
39impl OpenAiChatProvider for OllamaProvider {
40    type Config = OpenAIConfig;
41
42    fn client(&self) -> &Client<Self::Config> {
43        &self.client
44    }
45
46    fn model(&self) -> &str {
47        &self.model
48    }
49
50    fn provider_name(&self) -> &'static str {
51        "Ollama"
52    }
53}