use async_trait::async_trait;
use qql_core::error::QqlError;
use crate::sparse::{self, SparseVector};
#[cfg(not(target_arch = "wasm32"))]
pub trait EmbedderBound: Send + Sync {}
#[cfg(not(target_arch = "wasm32"))]
impl<T: Send + Sync> EmbedderBound for T {}
#[cfg(target_arch = "wasm32")]
pub trait EmbedderBound {}
#[cfg(target_arch = "wasm32")]
impl<T> EmbedderBound for T {}
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait Embedder: EmbedderBound {
async fn embed_dense(&self, text: &str, model: &str) -> Result<Vec<f32>, QqlError>;
async fn embed_sparse(&self, text: &str) -> Result<SparseVector, QqlError>;
fn dimension(&self) -> Option<usize> {
None
}
fn accepts_model(&self, _model: &str) -> bool {
true
}
async fn embed_dense_batch(
&self,
texts: &[String],
model: &str,
) -> Result<Vec<Vec<f32>>, QqlError> {
let mut results = Vec::with_capacity(texts.len());
for text in texts {
results.push(self.embed_dense(text, model).await?);
}
Ok(results)
}
}
pub struct SparseEmbedder;
impl SparseEmbedder {
pub fn embed_sparse(text: &str) -> SparseVector {
sparse::build_query_default(text)
}
}