use serde::{Deserialize, Serialize};
#[derive(Debug, thiserror::Error)]
pub enum EmbeddingError {
#[error("HttpError: {0}")]
HttpError(#[from] reqwest::Error),
#[error("JsonError: {0}")]
JsonError(#[from] serde_json::Error),
#[error("DocumentError: {0}")]
DocumentError(Box<dyn std::error::Error + Send + Sync + 'static>),
#[error("ResponseError: {0}")]
ResponseError(String),
#[error("ProviderError: {0}")]
ProviderError(String),
}
pub trait EmbeddingModel: Clone + Sync + Send {
const MAX_DOCUMENTS: usize;
fn ndims(&self) -> usize;
fn embed_texts(
&self,
texts: impl IntoIterator<Item = String> + Send,
) -> impl std::future::Future<Output = Result<Vec<Embedding>, EmbeddingError>> + Send;
fn embed_text(
&self,
text: &str,
) -> impl std::future::Future<Output = Result<Embedding, EmbeddingError>> + Send {
async {
Ok(self
.embed_texts(vec![text.to_string()])
.await?
.pop()
.expect("There should be at least one embedding"))
}
}
}
#[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 {}