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