pub trait EmbeddingService: Send + Sync {
// Required methods
fn embed<'life0, 'life1, 'async_trait>(
&'life0 self,
texts: &'life1 [String],
model: EmbeddingModel,
) -> Pin<Box<dyn Future<Output = Result<Vec<Vec<f32>>>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
fn supports_model(&self, model: EmbeddingModel) -> bool;
fn name(&self) -> &'static str;
// Provided methods
fn embed_one<'life0, 'life1, 'async_trait>(
&'life0 self,
text: &'life1 str,
model: EmbeddingModel,
) -> Pin<Box<dyn Future<Output = Result<Vec<f32>>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn model_config(&self, model: EmbeddingModel) -> ModelConfig { ... }
}Expand description
Stable: external consumers may depend on this; breaking changes require a SemVer bump.
Trait for embedding generation services.
This trait defines the interface for services that can convert text into vector embeddings. Implementations may use local models (native Rust) or remote APIs.
§Example
use lattice_embed::{EmbeddingService, EmbeddingModel, NativeEmbeddingService};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let service = NativeEmbeddingService::default();
let embedding = service.embed_one("Hello, world!", EmbeddingModel::default()).await?;
assert_eq!(embedding.len(), 384);
Ok(())
}Required Methods§
Sourcefn embed<'life0, 'life1, 'async_trait>(
&'life0 self,
texts: &'life1 [String],
model: EmbeddingModel,
) -> Pin<Box<dyn Future<Output = Result<Vec<Vec<f32>>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn embed<'life0, 'life1, 'async_trait>(
&'life0 self,
texts: &'life1 [String],
model: EmbeddingModel,
) -> Pin<Box<dyn Future<Output = Result<Vec<Vec<f32>>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Stable: generate embeddings for multiple texts.
Returns a vector of embeddings, one for each input text, in the same order.
Sourcefn supports_model(&self, model: EmbeddingModel) -> bool
fn supports_model(&self, model: EmbeddingModel) -> bool
Stable: check if the service supports a given model.
Provided Methods§
Sourcefn embed_one<'life0, 'life1, 'async_trait>(
&'life0 self,
text: &'life1 str,
model: EmbeddingModel,
) -> Pin<Box<dyn Future<Output = Result<Vec<f32>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn embed_one<'life0, 'life1, 'async_trait>(
&'life0 self,
text: &'life1 str,
model: EmbeddingModel,
) -> Pin<Box<dyn Future<Output = Result<Vec<f32>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Stable: generate an embedding for a single text.
This is a convenience method that calls embed with a single-element slice.
Sourcefn model_config(&self, model: EmbeddingModel) -> ModelConfig
fn model_config(&self, model: EmbeddingModel) -> ModelConfig
Unstable: returns the effective ModelConfig for a given model on this service.
The default returns a config with no MRL truncation. NativeEmbeddingService
overrides this to expose the configured output dimension so CachedEmbeddingService
can include the actual dimension in cache keys.