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};
7use std::future::ready;
8
9pub struct OllamaProvider {
10    model: String,
11    client: Client<OpenAIConfig>,
12}
13
14impl OllamaProvider {
15    pub fn new(model: &str, base_url: &str) -> Self {
16        Self { model: model.to_string(), client: Client::with_config(get_local_config(base_url)) }
17    }
18
19    pub fn default(model: &str) -> Self {
20        Self { model: model.to_string(), client: Client::with_config(get_local_config("http://localhost:11434/v1")) }
21    }
22}
23
24impl ProviderFactory for OllamaProvider {
25    async fn from_env() -> Result<Self> {
26        Self::from_env_with_connection(ProviderConnectionConfig::default()).await
27    }
28
29    fn from_env_with_connection(connection: ProviderConnectionConfig) -> impl Future<Output = Result<Self>> + Send {
30        let base_url = connection.base_url.as_deref().unwrap_or("http://localhost:11434/v1");
31        ready(Ok(Self { model: String::new(), client: Client::with_config(get_local_config(base_url)) }))
32    }
33
34    fn with_model(mut self, model: &str) -> Self {
35        self.model = model.to_string();
36        self
37    }
38}
39
40impl OpenAiChatProvider for OllamaProvider {
41    type Config = OpenAIConfig;
42
43    fn client(&self) -> &Client<Self::Config> {
44        &self.client
45    }
46
47    fn model(&self) -> &str {
48        &self.model
49    }
50
51    fn provider_name(&self) -> &'static str {
52        "Ollama"
53    }
54}