1use std::future::Future;
4use std::pin::Pin;
5use std::sync::Arc;
6
7use crate::error::Result;
8
9pub trait EmbeddingModel: Send + Sync {
14 fn embed(&self, texts: &[&str]) -> impl Future<Output = Result<Vec<Vec<f32>>>> + Send;
16
17 fn dimensions(&self) -> usize;
19}
20
21pub 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
44pub type SharedEmbeddingModel = Arc<dyn ErasedEmbeddingModel>;