diff --git a/crates/headroom-core/src/relevance/embedding.rs b/crates/headroom-core/src/relevance/embedding.rs index a0c7c42d2..c64fc21f4 100644 --- a/crates/headroom-core/src/relevance/embedding.rs +++ b/crates/headroom-core/src/relevance/embedding.rs @@ -511,4 +511,39 @@ mod tests { assert!((0.0..=1.0).contains(&sc.score)); } } + + #[cfg(feature = "ml")] + #[test] + fn fastembed_batches_past_the_cap_keep_each_score_with_its_item() { + if !fastembed_enabled() { + return; + } + // Three EMBED_BATCH_SIZE batches of mixed lengths, the one related item + // in the last: a misaligned batch boundary would hand its score away. + let s = EmbeddingScorer::try_new().expect("model loads"); + let mut items: Vec = (0..40) + .map(|i| { + format!( + "inventory row {i}: {}", + "pallets of garden furniture ".repeat(i % 5 + 1) + ) + }) + .collect(); + items[37] = "authentication failed for user admin: invalid password".to_string(); + let refs: Vec<&str> = items.iter().map(String::as_str).collect(); + let batch = s.score_batch(&refs, "login error"); + assert_eq!(batch.len(), 40); + let top = (0..40) + .max_by(|&a, &b| batch[a].score.total_cmp(&batch[b].score)) + .unwrap(); + assert_eq!(top, 37); + for (item, sc) in refs.iter().zip(&batch) { + let single = s.score(item, "login error").score; + assert!( + (sc.score - single).abs() < 1e-3, + "{item}: batched {} vs single {single}", + sc.score + ); + } + } } diff --git a/tests/test_relevance.py b/tests/test_relevance.py index 162b5c39d..0563eda25 100644 --- a/tests/test_relevance.py +++ b/tests/test_relevance.py @@ -186,6 +186,20 @@ class TestEmbeddingScorer: # "failed"/"error" semantically relates to "errors" assert scores[0].score > scores[1].score + def test_batches_past_the_cap_keep_each_score_with_its_item(self, scorer): + """Three EMBED_BATCH_SIZE batches of mixed lengths, the related item in + the last: a misaligned batch boundary would hand its score away.""" + items = [ + f"inventory row {i}: " + "pallets of garden furniture " * (i % 5 + 1) for i in range(40) + ] + items[37] = "authentication failed for user admin: invalid password" + + scores = [s.score for s in scorer.score_batch(items, "login error")] + assert len(scores) == 40 + assert max(range(40), key=scores.__getitem__) == 37 + for item, batched in zip(items, scores): + assert batched == pytest.approx(scorer.score(item, "login error").score, abs=1e-3) + def test_paraphrase_match(self, scorer): """Embeddings match paraphrases.""" items = [