mod accessors;
mod bm25;
mod catalog;
mod construct;
mod inference;
#[cfg(test)]
mod tests;
pub use catalog::{
list_embedding_models, resolve_embedding_model, resolve_image_model, resolve_multi_model,
};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::OnceLock;
use std::sync::{Arc, Mutex};
use fastembed::{Bgem3Embedding, ImageEmbedding, SparseTextEmbedding, TextEmbedding, TextRerank};
use qdrant_edge::bm25_embed::EdgeBm25;
use qql_core::error::QqlError;
use qql_embed::{Bm25Params, Bm25TextConfig};
pub(crate) fn err(msg: impl Into<std::borrow::Cow<'static, str>>) -> QqlError {
QqlError::execution("QQL-EDGE-EMBED", msg, None)
}
#[derive(Debug, Clone)]
pub struct EmbeddingModelInfo {
pub name: String,
pub model_code: String,
pub dim: usize,
pub description: String,
pub multi: bool,
pub image: bool,
}
#[derive(Debug, Clone, Default)]
pub struct FastEmbedderOptions {
pub model: Option<String>,
pub sparse_model: Option<String>,
pub multi_model: Option<String>,
pub image_model: Option<String>,
pub reranker_model: Option<String>,
pub cache_dir: Option<PathBuf>,
pub show_download_progress: bool,
pub bm25_k1: Option<f64>,
pub bm25_b: Option<f64>,
pub bm25_avg_len: Option<f64>,
pub bm25_language: Option<String>,
pub bm25_tokenizer: Option<String>,
pub bm25_lowercase: Option<bool>,
pub bm25_ascii_folding: Option<bool>,
pub bm25_min_token_len: Option<usize>,
pub bm25_max_token_len: Option<usize>,
pub bm25_stopwords: Option<Vec<String>>,
pub bm25_stemmer: Option<String>,
pub bm25_stopwords_languages: Option<Vec<String>>,
}
pub(crate) struct DenseSlot {
pub(crate) model: Arc<Mutex<TextEmbedding>>,
pub(crate) model_name: String,
pub(crate) model_code: String,
pub(crate) dim: usize,
}
pub(crate) struct MultiSlot {
pub(crate) model: Arc<Mutex<Bgem3Embedding>>,
pub(crate) model_name: String,
pub(crate) model_code: String,
pub(crate) dim: usize,
}
pub(crate) struct ImageSlot {
pub(crate) model: Arc<Mutex<ImageEmbedding>>,
pub(crate) model_name: String,
pub(crate) model_code: String,
pub(crate) dim: usize,
}
pub(crate) struct RerankSlot {
pub(crate) model: Arc<Mutex<TextRerank>>,
pub(crate) model_name: String,
pub(crate) model_code: String,
}
pub(crate) struct SparseSlot {
pub(crate) model: Arc<Mutex<SparseTextEmbedding>>,
pub(crate) model_name: String,
pub(crate) model_code: String,
}
pub struct FastEmbedder {
dense: DenseSlot,
sparse: Option<SparseSlot>,
multi: Option<MultiSlot>,
image: Option<ImageSlot>,
reranker: Option<RerankSlot>,
bm25: EdgeBm25,
bm25_params: Bm25Params,
bm25_text: Bm25TextConfig,
}
pub(crate) type CacheKey = (String, String);
pub(crate) type CachedModel<T> = Arc<Mutex<T>>;
pub(crate) type ModelCache<T> = Mutex<HashMap<CacheKey, CachedModel<T>>>;
static DENSE_CACHE: OnceLock<ModelCache<TextEmbedding>> = OnceLock::new();
static SPARSE_CACHE: OnceLock<ModelCache<SparseTextEmbedding>> = OnceLock::new();
static MULTI_CACHE: OnceLock<ModelCache<Bgem3Embedding>> = OnceLock::new();
static IMAGE_CACHE: OnceLock<ModelCache<ImageEmbedding>> = OnceLock::new();
static RERANK_CACHE: OnceLock<ModelCache<TextRerank>> = OnceLock::new();
pub(crate) fn cache_dir_key(dir: Option<&PathBuf>) -> String {
dir.map(|p| p.display().to_string()).unwrap_or_default()
}
pub(crate) fn dense_cache() -> &'static ModelCache<TextEmbedding> {
DENSE_CACHE.get_or_init(|| Mutex::new(HashMap::new()))
}
pub(crate) fn sparse_cache() -> &'static ModelCache<SparseTextEmbedding> {
SPARSE_CACHE.get_or_init(|| Mutex::new(HashMap::new()))
}
pub(crate) fn multi_cache() -> &'static ModelCache<Bgem3Embedding> {
MULTI_CACHE.get_or_init(|| Mutex::new(HashMap::new()))
}
pub(crate) fn image_cache() -> &'static ModelCache<ImageEmbedding> {
IMAGE_CACHE.get_or_init(|| Mutex::new(HashMap::new()))
}
pub(crate) fn rerank_cache() -> &'static ModelCache<TextRerank> {
RERANK_CACHE.get_or_init(|| Mutex::new(HashMap::new()))
}