use crate::{
http_client,
wasm_compat::{WasmCompatSend, WasmCompatSync},
};
use serde::{Deserialize, Serialize};
#[derive(Debug, thiserror::Error)]
pub enum EmbeddingError {
#[error("HttpError: {0}")]
HttpError(#[from] http_client::Error),
#[error("JsonError: {0}")]
JsonError(#[from] serde_json::Error),
#[error("UrlError: {0}")]
UrlError(#[from] url::ParseError),
#[cfg(not(target_family = "wasm"))]
#[error("DocumentError: {0}")]
DocumentError(Box<dyn std::error::Error + Send + Sync + 'static>),
#[cfg(target_family = "wasm")]
#[error("DocumentError: {0}")]
DocumentError(Box<dyn std::error::Error + 'static>),
#[error("ResponseError: {0}")]
ResponseError(String),
#[error("ProviderError: {0}")]
ProviderError(String),
}
pub trait EmbeddingModel: WasmCompatSend + WasmCompatSync {
const MAX_DOCUMENTS: usize;
type Client;
fn make(client: &Self::Client, model: impl Into<String>, dims: Option<usize>) -> Self;
fn ndims(&self) -> usize;
fn embed_texts(
&self,
texts: impl IntoIterator<Item = String> + WasmCompatSend,
) -> impl std::future::Future<Output = Result<Vec<Embedding>, EmbeddingError>> + WasmCompatSend;
fn embed_text(
&self,
text: &str,
) -> impl std::future::Future<Output = Result<Embedding, EmbeddingError>> + WasmCompatSend {
async {
let mut embeddings = self.embed_texts(vec![text.to_string()]).await?;
embeddings.pop().ok_or_else(|| {
EmbeddingError::ResponseError(
"embedding provider returned an empty response for embed_text".to_string(),
)
})
}
}
}
pub trait ImageEmbeddingModel: Clone + WasmCompatSend + WasmCompatSync {
const MAX_DOCUMENTS: usize;
fn ndims(&self) -> usize;
fn embed_images(
&self,
images: impl IntoIterator<Item = Vec<u8>> + WasmCompatSend,
) -> impl std::future::Future<Output = Result<Vec<Embedding>, EmbeddingError>> + Send;
fn embed_image<'a>(
&'a self,
bytes: &'a [u8],
) -> impl std::future::Future<Output = Result<Embedding, EmbeddingError>> + WasmCompatSend {
async move {
let mut embeddings = self.embed_images(vec![bytes.to_owned()]).await?;
embeddings.pop().ok_or_else(|| {
EmbeddingError::ResponseError(
"embedding provider returned an empty response for embed_image".to_string(),
)
})
}
}
}
#[derive(Clone, Default, Deserialize, Serialize, Debug)]
pub struct Embedding {
pub document: String,
pub vec: Vec<f64>,
}
impl PartialEq for Embedding {
fn eq(&self, other: &Self) -> bool {
self.document == other.document
}
}
impl Eq for Embedding {}