Skip to main content

Module provider

Module provider 

Source
Expand description

Abstract embedding provider interface.

This module defines the EmbeddingProvider trait, which abstracts over different embedding API providers (OpenAI, Ollama, Google, etc.). The trait is object-safe and designed for use with dynamic dispatch (Box<dyn EmbeddingProvider>).

§Design Goals

  • Provider Flexibility: Support multiple embedding providers with different models and dimensions (768-dim for Ollama/Google, 1536-dim for OpenAI)
  • Object Safety: Enable runtime provider selection via trait objects
  • Async Support: All embedding operations are async for non-blocking I/O
  • Thread Safety: Providers must be Send + Sync for use in async contexts
  • Batch Optimization: Providers can override default batching with native batch APIs

§Examples

use maproom::embedding::provider::{EmbeddingProvider, Vector};
use maproom::embedding::error::EmbeddingError;
use async_trait::async_trait;

// Define a custom provider
struct MyProvider {
    dimension: usize,
}

#[async_trait]
impl EmbeddingProvider for MyProvider {
    async fn embed(&self, text: String) -> Result<Vector, EmbeddingError> {
        // Implementation here
        Ok(vec![0.0; self.dimension])
    }

    fn dimension(&self) -> usize {
        self.dimension
    }

    fn provider_name(&self) -> &'static str {
        "my-provider"
    }
}

// Use with dynamic dispatch
async fn process_with_provider(provider: Box<dyn EmbeddingProvider>) -> Result<(), EmbeddingError> {
    let embedding = provider.embed("Hello, world!".to_string()).await?;
    assert_eq!(embedding.len(), provider.dimension());
    Ok(())
}

Structs§

ProviderMetrics
Metrics about provider performance and cost.

Traits§

EmbeddingProvider
Abstract embedding provider interface.

Type Aliases§

Vector
Vector type representing an embedding.