Skip to main content

daimon_core/
embedding.rs

1//! Embedding model trait for computing vector representations of text.
2
3use std::future::Future;
4use std::pin::Pin;
5use std::sync::Arc;
6
7use crate::error::Result;
8
9/// Trait for models that produce vector embeddings from text.
10///
11/// Implement this for your embedding provider, then use it with vector stores
12/// and RAG pipelines.
13pub trait EmbeddingModel: Send + Sync {
14    /// Computes embeddings for one or more texts. Returns one vector per input.
15    fn embed(&self, texts: &[&str]) -> impl Future<Output = Result<Vec<Vec<f32>>>> + Send;
16
17    /// Returns the dimensionality of the embedding vectors this model produces.
18    fn dimensions(&self) -> usize;
19}
20
21/// Object-safe wrapper for [`EmbeddingModel`].
22pub trait ErasedEmbeddingModel: Send + Sync {
23    fn embed_erased<'a>(
24        &'a self,
25        texts: &'a [&'a str],
26    ) -> Pin<Box<dyn Future<Output = Result<Vec<Vec<f32>>>> + Send + 'a>>;
27
28    fn dimensions(&self) -> usize;
29}
30
31impl<T: EmbeddingModel> ErasedEmbeddingModel for T {
32    fn embed_erased<'a>(
33        &'a self,
34        texts: &'a [&'a str],
35    ) -> Pin<Box<dyn Future<Output = Result<Vec<Vec<f32>>>> + Send + 'a>> {
36        Box::pin(self.embed(texts))
37    }
38
39    fn dimensions(&self) -> usize {
40        EmbeddingModel::dimensions(self)
41    }
42}
43
44/// Shared ownership of an embedding model.
45pub type SharedEmbeddingModel = Arc<dyn ErasedEmbeddingModel>;