Skip to main content

EmbeddingService

Trait EmbeddingService 

Source
pub trait EmbeddingService: Send + Sync {
    // Required methods
    fn embed(&self, text: &str) -> Result<Embedding>;
    fn embed_batch(&self, texts: &[&str]) -> Result<Vec<Embedding>>;
    fn dimension(&self) -> usize;

    // Provided method
    fn validate_embedding(&self, embedding: &Embedding) -> Result<()> { ... }
}
Expand description

Embedding service trait for generating vector representations of text.

This trait defines the contract for any embedding provider. Implementations must be thread-safe (Send + Sync) to allow concurrent embedding operations.

§Implementing a Custom Provider

use pulsedb::embedding::EmbeddingService;
use pulsedb::{Embedding, Result};

struct MyEmbeddingService {
    client: MyApiClient,
    dimension: usize,
}

impl EmbeddingService for MyEmbeddingService {
    fn embed(&self, text: &str) -> Result<Embedding> {
        Ok(self.client.get_embedding(text)?)
    }

    fn embed_batch(&self, texts: &[&str]) -> Result<Vec<Embedding>> {
        Ok(self.client.get_embeddings(texts)?)
    }

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

Required Methods§

Source

fn embed(&self, text: &str) -> Result<Embedding>

Generates an embedding for a single text.

§Arguments
  • text - The text to embed
§Returns

A vector of f32 values with length equal to dimension().

§Errors

Returns PulseDBError::Embedding if embedding generation fails.

Source

fn embed_batch(&self, texts: &[&str]) -> Result<Vec<Embedding>>

Generates embeddings for multiple texts in a batch.

Batch processing is typically more efficient than individual calls due to reduced API overhead and better GPU utilization.

§Arguments
  • texts - Slice of texts to embed
§Returns

A vector of embeddings in the same order as the input texts.

§Errors

Returns PulseDBError::Embedding if any embedding generation fails.

Source

fn dimension(&self) -> usize

Returns the dimension of embeddings produced by this service.

All embeddings from this service will have exactly this many dimensions.

Provided Methods§

Source

fn validate_embedding(&self, embedding: &Embedding) -> Result<()>

Validates that an embedding has the correct dimension.

§Errors

Returns ValidationError::DimensionMismatch if dimensions don’t match.

Implementors§

Source§

impl EmbeddingService for OnnxEmbedding

Available on crate feature builtin-embeddings only.
Source§

impl EmbeddingService for ExternalEmbedding