use anyhow::{bail, Context, Result};
use fastembed::{EmbeddingModel, TextEmbedding, TextInitOptions};
use std::path::PathBuf;
use std::sync::{Mutex, OnceLock};
pub const EMBED_SLICE: usize = 256;
pub const EMBED_BATCH: usize = 64;
pub const QUERY_PREFIX: &str = "query: ";
pub const PASSAGE_PREFIX: &str = "passage: ";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Model {
BgeSmall,
E5Multilingual,
E5Base,
}
impl Model {
pub fn from_env() -> Self {
match std::env::var("POWERSHELL_MCP_EMBED_MODEL").as_deref() {
Ok("e5-multilingual") | Ok("multilingual-e5-small") => Model::E5Multilingual,
Ok("e5-base") | Ok("multilingual-e5-base") => Model::E5Base,
_ => Model::BgeSmall,
}
}
fn fastembed(self) -> EmbeddingModel {
match self {
Model::BgeSmall => EmbeddingModel::BGESmallENV15,
Model::E5Multilingual => EmbeddingModel::MultilingualE5Small,
Model::E5Base => EmbeddingModel::MultilingualE5Base,
}
}
pub fn id(self) -> &'static str {
match self {
Model::BgeSmall => "bge-small-en-v1.5",
Model::E5Multilingual => "multilingual-e5-small",
Model::E5Base => "multilingual-e5-base",
}
}
pub fn dim(self) -> usize {
match self {
Model::BgeSmall => 384,
Model::E5Multilingual => 384,
Model::E5Base => 768,
}
}
pub fn uses_prefixes(self) -> bool {
matches!(self, Model::E5Multilingual | Model::E5Base)
}
}
pub fn current_model() -> Model {
Model::from_env()
}
static EMBEDDER: OnceLock<Mutex<TextEmbedding>> = OnceLock::new();
pub fn embed_texts(texts: &[String]) -> Result<Vec<Vec<f32>>> {
let emb = embedder()?;
let mut out = Vec::with_capacity(texts.len());
for slice in texts.chunks(EMBED_SLICE) {
let vectors = emb
.lock()
.map_err(|_| anyhow::anyhow!("embedder mutex poisoned"))?
.embed(slice.to_vec(), Some(EMBED_BATCH))
.context("embed texts")?;
if vectors.len() != slice.len() {
bail!(
"embedding returned {} vectors for {} texts",
vectors.len(),
slice.len()
);
}
out.extend(vectors);
}
Ok(out)
}
pub fn model_cache_dir() -> Option<PathBuf> {
std::env::var("FASTEMBED_CACHE")
.map(PathBuf::from)
.ok()
.or_else(|| {
std::env::var("HOME")
.ok()
.map(|h| PathBuf::from(h).join(".cache").join("fastembed"))
})
}
pub fn active_model_cached() -> bool {
let Some(cache) = model_cache_dir() else {
return false;
};
let Ok(entries) = std::fs::read_dir(&cache) else {
return false;
};
entries.flatten().any(|e| {
e.file_name()
.to_string_lossy()
.contains(current_model().id())
})
}
fn embedder() -> Result<&'static Mutex<TextEmbedding>> {
if let Some(e) = EMBEDDER.get() {
return Ok(e);
}
let mut init = TextInitOptions::default();
init.model_name = current_model().fastembed();
init.show_download_progress = true;
init.cache_dir = model_cache_dir().unwrap_or_else(|| PathBuf::from(".fastembed_cache"));
let emb = TextEmbedding::try_new(init)
.context("init embedding model (first run downloads the model binary)")?;
let _ = EMBEDDER.set(Mutex::new(emb));
Ok(EMBEDDER.get().expect("embedder set"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bge_small_is_the_default() {
std::env::remove_var("POWERSHELL_MCP_EMBED_MODEL");
assert_eq!(current_model(), Model::BgeSmall);
assert_eq!(Model::BgeSmall.id(), "bge-small-en-v1.5");
assert_eq!(Model::BgeSmall.dim(), 384);
assert!(!Model::BgeSmall.uses_prefixes());
}
#[test]
fn e5_models_use_prefixes() {
assert!(Model::E5Multilingual.uses_prefixes());
assert!(Model::E5Base.uses_prefixes());
}
}