use async_trait::async_trait;
use futures::stream::BoxStream;
use crate::embeddings::{EmbeddingsRequest, EmbeddingsResponse};
use crate::error::Error;
use crate::provider::Provider;
use crate::providers::openai::OpenAiProvider;
use crate::stream_event::StreamEvent;
use crate::text::{Step, TextRequest};
const DEFAULT_BASE_URL: &str = "http://localhost:11434/v1";
pub struct OllamaProvider {
inner: OpenAiProvider,
}
impl OllamaProvider {
pub fn new() -> Self {
Self::with_base_url(DEFAULT_BASE_URL)
}
pub fn with_base_url(base_url: impl Into<String>) -> Self {
Self::with_api_key_and_base_url("", base_url)
}
pub fn with_api_key_and_base_url(
api_key: impl Into<String>,
base_url: impl Into<String>,
) -> Self {
Self {
inner: OpenAiProvider::with_name_and_base_url("ollama", api_key, base_url),
}
}
pub fn with_client(mut self, client: reqwest_middleware::ClientWithMiddleware) -> Self {
self.inner = self.inner.with_client(client);
self
}
}
impl Default for OllamaProvider {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl Provider for OllamaProvider {
fn name(&self) -> &str {
"ollama"
}
async fn text_step(&self, request: TextRequest) -> Result<Step, Error> {
self.inner.text_step(request).await
}
async fn stream_text_once(
&self,
request: TextRequest,
) -> Result<BoxStream<'static, Result<StreamEvent, Error>>, Error> {
self.inner.stream_text_once(request).await
}
async fn embeddings(&self, request: EmbeddingsRequest) -> Result<EmbeddingsResponse, Error> {
self.inner.embeddings(request).await
}
}