use std::str::FromStr;
use fastembed::EmbeddingModel;
pub const DEFAULT_EMBED_MODEL: &str = "AllMiniLML6V2";
pub const BGE_QUERY_PREFIX: &str = "Represent this sentence for searching relevant passages: ";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EmbedModelChoice {
pub model: EmbeddingModel,
pub dim: u64,
pub query_prefix: &'static str,
pub passage_prefix: &'static str,
pub collection_suffix: Option<&'static str>,
}
impl EmbedModelChoice {
pub fn supported() -> &'static [(&'static str, Self)] {
&[
(
"AllMiniLML6V2",
Self {
model: EmbeddingModel::AllMiniLML6V2,
dim: 384,
query_prefix: "",
passage_prefix: "",
collection_suffix: None,
},
),
(
"BGEBaseENV15",
Self {
model: EmbeddingModel::BGEBaseENV15,
dim: 768,
query_prefix: BGE_QUERY_PREFIX,
passage_prefix: "",
collection_suffix: Some("bge768"),
},
),
]
}
pub fn accepted_names() -> String {
Self::supported()
.iter()
.map(|(name, _)| *name)
.collect::<Vec<_>>()
.join(", ")
}
pub fn default_collection(&self, base: &str) -> String {
match self.collection_suffix {
None => base.to_owned(),
Some(suffix) => format!("{base}_{suffix}"),
}
}
pub fn from_env() -> Result<Self, String> {
match std::env::var("KNOT_EMBED_MODEL") {
Ok(name) => Self::from_str(&name),
Err(_) => Self::from_str(DEFAULT_EMBED_MODEL),
}
}
}
impl FromStr for EmbedModelChoice {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::supported()
.iter()
.find(|(name, _)| name.eq_ignore_ascii_case(s))
.map(|(_, choice)| choice.clone())
.ok_or_else(|| {
format!(
"Unknown embedding model '{s}'. Accepted KNOT_EMBED_MODEL values: {}",
Self::accepted_names()
)
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_known_names_case_insensitively() {
for (name, choice) in EmbedModelChoice::supported() {
let lower =
EmbedModelChoice::from_str(&name.to_lowercase()).expect("lowercase name parses");
assert_eq!(&lower, choice, "case-insensitive parse failed for {name}");
}
}
#[test]
fn rejects_unknown_name_listing_accepts() {
let err = EmbedModelChoice::from_str("GPT5").unwrap_err();
assert!(err.contains("Unknown embedding model 'GPT5'"), "{err}");
assert!(
err.contains("AllMiniLML6V2"),
"error must list accepts: {err}"
);
assert!(
err.contains("BGEBaseENV15"),
"error must list accepts: {err}"
);
assert!(EmbedModelChoice::from_str("BGEBaseENv15").is_ok());
assert!(EmbedModelChoice::from_str("allminilmL6v2").is_ok());
assert!(EmbedModelChoice::from_str("BGESmallENV15").is_err());
assert!(EmbedModelChoice::from_str("JinaEmbeddingsV2BaseCode").is_err());
assert!(EmbedModelChoice::from_str("MultilingualE5Small").is_err());
assert!(EmbedModelChoice::from_str("NomicEmbedTextV15").is_err());
}
#[test]
fn bge_uses_query_prefix_and_no_passage_prefix() {
let bge = EmbedModelChoice::from_str("BGEBaseENV15").unwrap();
assert_eq!(bge.dim, 768);
assert_eq!(bge.query_prefix, BGE_QUERY_PREFIX);
assert_eq!(bge.passage_prefix, "");
assert!(
bge.query_prefix.ends_with(' '),
"prefix must be space-separated"
);
}
#[test]
fn minilm_is_symmetric() {
let choice = EmbedModelChoice::from_str("AllMiniLML6V2").unwrap();
assert_eq!(choice.query_prefix, "", "MiniLM must be symmetric");
assert_eq!(choice.passage_prefix, "", "MiniLM must be symmetric");
}
#[test]
fn every_choice_dim_matches_fastembed_model_info() {
for (name, choice) in EmbedModelChoice::supported() {
let info = fastembed::TextEmbedding::get_model_info(&choice.model)
.unwrap_or_else(|e| panic!("fastembed metadata for {name}: {e}"));
assert_eq!(
u64::try_from(info.dim).expect("non-negative dim"),
choice.dim,
"declared dim for {name} drifted from fastembed"
);
}
}
#[test]
fn default_model_is_minilm_384_symmetric() {
let choice = EmbedModelChoice::from_str(DEFAULT_EMBED_MODEL).expect("default known");
assert_eq!(choice.model, EmbeddingModel::AllMiniLML6V2);
assert_eq!(choice.dim, 384);
assert_eq!(choice.query_prefix, "");
assert_eq!(choice.passage_prefix, "");
}
#[test]
fn exactly_one_model_has_no_collection_suffix_and_it_is_the_default() {
let baseless: Vec<&str> = EmbedModelChoice::supported()
.iter()
.filter(|(_, choice)| choice.collection_suffix.is_none())
.map(|(name, _)| *name)
.collect();
assert_eq!(baseless, vec![DEFAULT_EMBED_MODEL]);
}
#[test]
fn collection_suffixes_are_unique() {
let mut seen = std::collections::HashSet::new();
for (name, choice) in EmbedModelChoice::supported() {
if let Some(suffix) = choice.collection_suffix {
assert!(
seen.insert(suffix),
"collection suffix '{suffix}' duplicated (first on {name})"
);
}
}
}
#[test]
fn default_collection_maps_base_and_suffix() {
let minilm = EmbedModelChoice::from_str("AllMiniLML6V2").unwrap();
assert_eq!(minilm.default_collection("knot_entities"), "knot_entities");
let bge = EmbedModelChoice::from_str("BGEBaseENV15").unwrap();
assert_eq!(
bge.default_collection("knot_entities"),
"knot_entities_bge768"
);
}
}