memoir_core/embedding/
mod.rs1mod error;
7pub mod onnx;
8
9pub use error::EmbeddingError;
10pub use onnx::OnnxEmbedding;
11
12pub trait EmbeddingModel: Send + Sync + 'static {
18 fn embed(&self, text: &str) -> impl std::future::Future<Output = Result<Vec<f32>, EmbeddingError>> + Send;
25
26 fn dimensions(&self) -> usize;
28}
29
30impl<T: EmbeddingModel> EmbeddingModel for std::sync::Arc<T> {
31 fn embed(&self, text: &str) -> impl std::future::Future<Output = Result<Vec<f32>, EmbeddingError>> + Send {
32 (**self).embed(text)
33 }
34
35 fn dimensions(&self) -> usize {
36 (**self).dimensions()
37 }
38}
39
40#[cfg(test)]
41mod tests {
42 use super::*;
43
44 struct StubEmbedding {
45 dim: usize,
46 }
47
48 impl EmbeddingModel for StubEmbedding {
49 async fn embed(&self, _text: &str) -> Result<Vec<f32>, EmbeddingError> {
50 Ok(vec![0.1; self.dim])
51 }
52
53 fn dimensions(&self) -> usize {
54 self.dim
55 }
56 }
57
58 #[tokio::test(flavor = "current_thread")]
59 async fn should_implement_trait_with_in_test_stub() {
60 let model = StubEmbedding { dim: 4 };
61
62 let vector = model.embed("hello").await.unwrap();
63
64 assert_eq!(vector.len(), 4);
65 assert_eq!(model.dimensions(), 4);
66 }
67}