1mod hash;
8
9#[cfg(feature = "onnx-embed")]
10mod onnx;
11
12pub mod gemini;
13pub mod ollama;
14
15pub use hash::HashEmbedder;
16
17use crate::config::EmbedProvider;
18use crate::{RagConfig, RagError, Result};
19use async_trait::async_trait;
20use std::sync::Arc;
21
22#[async_trait]
25pub trait Embedder: Send + Sync {
26 async fn embed(&self, texts: &[String]) -> Result<Vec<Vec<f32>>>;
28
29 fn dim(&self) -> usize;
31
32 fn id(&self) -> &str;
34
35 async fn embed_one(&self, text: &str) -> Result<Vec<f32>> {
37 let mut v = self.embed(std::slice::from_ref(&text.to_string())).await?;
38 v.pop()
39 .ok_or_else(|| RagError::Embedding("provider returned no vector".into()))
40 }
41}
42
43pub fn from_config(cfg: &RagConfig) -> Result<Arc<dyn Embedder>> {
45 match cfg.embed_provider {
46 EmbedProvider::Ollama => Ok(Arc::new(ollama::OllamaEmbedder::from_config(cfg))),
47 EmbedProvider::Gemini => Ok(Arc::new(gemini::GeminiEmbedder::from_config(cfg)?)),
48 EmbedProvider::Hash => Ok(Arc::new(HashEmbedder::new(cfg.embed_dim))),
49 EmbedProvider::Onnx => {
50 #[cfg(feature = "onnx-embed")]
51 {
52 Ok(Arc::new(onnx::OnnxEmbedder::from_config(cfg)?))
53 }
54 #[cfg(not(feature = "onnx-embed"))]
55 {
56 Err(RagError::FeatureDisabled(
57 "onnx".into(),
58 "onnx-embed".into(),
59 ))
60 }
61 }
62 }
63}