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: ";
pub const E5_QUERY_PREFIX: &str = "query: ";
pub const E5_PASSAGE_PREFIX: &str = "passage: ";
pub const NOMIC_QUERY_PREFIX: &str = "search_query: ";
pub const NOMIC_PASSAGE_PREFIX: &str = "search_document: ";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EmbedModelChoice {
pub model: EmbeddingModel,
pub dim: u64,
pub query_prefix: &'static str,
pub passage_prefix: &'static str,
}
impl EmbedModelChoice {
pub fn supported() -> &'static [(&'static str, Self)] {
&[
(
"AllMiniLML6V2",
Self {
model: EmbeddingModel::AllMiniLML6V2,
dim: 384,
query_prefix: "",
passage_prefix: "",
},
),
(
"BGESmallENV15",
Self {
model: EmbeddingModel::BGESmallENV15,
dim: 384,
query_prefix: BGE_QUERY_PREFIX,
passage_prefix: "",
},
),
(
"BGEBaseENV15",
Self {
model: EmbeddingModel::BGEBaseENV15,
dim: 768,
query_prefix: BGE_QUERY_PREFIX,
passage_prefix: "",
},
),
(
"MultilingualE5Small",
Self {
model: EmbeddingModel::MultilingualE5Small,
dim: 384,
query_prefix: E5_QUERY_PREFIX,
passage_prefix: E5_PASSAGE_PREFIX,
},
),
(
"JinaEmbeddingsV2BaseCode",
Self {
model: EmbeddingModel::JinaEmbeddingsV2BaseCode,
dim: 768,
query_prefix: "",
passage_prefix: "",
},
),
(
"NomicEmbedTextV15",
Self {
model: EmbeddingModel::NomicEmbedTextV15,
dim: 768,
query_prefix: NOMIC_QUERY_PREFIX,
passage_prefix: NOMIC_PASSAGE_PREFIX,
},
),
]
}
pub fn accepted_names() -> String {
Self::supported()
.iter()
.map(|(name, _)| *name)
.collect::<Vec<_>>()
.join(", ")
}
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 std::str::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("BGESmallENV15"),
"error must list accepts: {err}"
);
assert!(EmbedModelChoice::from_str("BGESmallENv15").is_ok());
assert!(EmbedModelChoice::from_str("BgEsmallenv15").is_ok());
assert!(EmbedModelChoice::from_str("BGESmallEnv18").is_err());
}
#[test]
fn bge_uses_query_prefix_and_no_passage_prefix() {
let bge = EmbedModelChoice::from_str("BGESmallENV15").unwrap();
assert_eq!(bge.dim, 384);
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 e5_uses_distinct_query_and_passage_prefixes() {
let e5 = EmbedModelChoice::from_str("MultilingualE5Small").unwrap();
assert_eq!(e5.dim, 384);
assert_ne!(e5.query_prefix, e5.passage_prefix);
assert_eq!(e5.query_prefix, E5_QUERY_PREFIX);
assert_eq!(e5.passage_prefix, E5_PASSAGE_PREFIX);
}
#[test]
fn nomic_uses_distinct_query_and_passage_prefixes() {
let nomic = EmbedModelChoice::from_str("NomicEmbedTextV15").unwrap();
assert_eq!(nomic.dim, 768);
assert_ne!(nomic.query_prefix, nomic.passage_prefix);
assert_eq!(nomic.query_prefix, NOMIC_QUERY_PREFIX);
assert_eq!(nomic.passage_prefix, NOMIC_PASSAGE_PREFIX);
}
#[test]
fn minilm_and_jina_are_symmetric() {
for name in ["AllMiniLML6V2", "JinaEmbeddingsV2BaseCode"] {
let choice = EmbedModelChoice::from_str(name).unwrap();
assert_eq!(choice.query_prefix, "", "{name} must be symmetric");
assert_eq!(choice.passage_prefix, "", "{name} 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_parseable_and_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, "");
}
}