💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
Add BB25 normalization for sparse encoders (#1046)
Jaepil Jeongba0d1eaeadce
diff --git a/src/python/txtai/ann/sparse/ivfsparse.py b/src/python/txtai/ann/sparse/ivfsparse.pyindex 0824417e..b6723dc9 100644--- a/src/python/txtai/ann/sparse/ivfsparse.py+++ b/src/python/txtai/ann/sparse/ivfsparse.py@@ -269,7 +269,7 @@ class IVFSparse(ANN):scores[:, deletes] = 0# Get top n matching indices and scores- indices = np.argpartition(-scores, limit if limit < scores.shape[0] else scores.shape[0] - 1)[:, :limit]+ indices = np.argpartition(-scores, limit if limit < scores.shape[1] else scores.shape[1] - 1)[:, :limit]scores = np.take_along_axis(scores, indices, axis=1)return indices, scoresdiff --git a/src/python/txtai/scoring/sparse.py b/src/python/txtai/scoring/sparse.pyindex ce9cd626..64e17b6d 100644--- a/src/python/txtai/scoring/sparse.py+++ b/src/python/txtai/scoring/sparse.py@@ -9,6 +9,7 @@ from ..ann import SparseANNFactoryfrom ..vectors import SparseVectorsFactoryfrom .base import Scoring+from .normalize import Normalizeclass Sparse(Scoring):@@ -33,9 +34,17 @@ class Sparse(Scoring):self.model = SparseVectorsFactory.create(config, models)# Normalize search outputs if vectors are not normalized already- # A float can also be provided to set the normalization factor (defaults to 30.0)+ # Supports: True (default linear, scale=30.0), float (custom scale),+ # "bb25"/"bayes" (Bayesian sigmoid calibration), False (disabled)self.isnormalize = self.config.get("normalize", True)+ # Create Bayesian normalizer when a Bayesian method is configured+ self.normalizer = None+ if isinstance(self.isnormalize, (str, dict)):+ normalizer = Normalize(self.isnormalize)+ if normalizer.isbayes():+ self.normalizer = normalizer+# Sparse ANNself.ann = None@@ -131,7 +140,7 @@ class Sparse(Scoring):return self.isnormalize or self.model.isnormalizedef isbayes(self):- return False+ return self.normalizer is not Nonedef start(self, checkpoint):"""@@ -194,7 +203,12 @@ class Sparse(Scoring):def normalize(self, queries, scores):"""- Normalize query result using the max query score.+ Normalize query results.++ When Bayesian normalization is configured, applies sigmoid calibration+ with per-query adaptive parameters (beta=median, alpha=1/std) to produce+ calibrated probabilities in [0, 1]. Otherwise, applies linear normalization+ using the max query score.Args:queries: query vectors@@ -204,7 +218,11 @@ class Sparse(Scoring):normalized query results"""- # Get normalize scale factor+ # Bayesian sigmoid calibration+ if self.normalizer:+ return [self.normalizer.bayes(result) if result else [] for result in scores]++ # Default linear normalizationscale = 30.0 if isinstance(self.isnormalize, bool) else self.isnormalize# Normalize scores using max scoresdiff --git a/test/python/testann/testsparse.py b/test/python/testann/testsparse.pyindex 368ba790..f3c4bcc2 100644--- a/test/python/testann/testsparse.py+++ b/test/python/testann/testsparse.py@@ -82,6 +82,29 @@ class TestSparse(unittest.TestCase):self.assertLess(len(ann.blocks), 15)ann.close()+ def testIVFSparseTopnOverLimit(self):+ """+ Test IVFSparse topn when limit exceeds the number of indexed documents+ """++ # Generate a small dataset (5 documents)+ data = self.generate(5, 30522)++ ann = SparseANNFactory.create({"backend": "ivfsparse"})+ ann.index(data)++ # Search with limit (10) greater than document count (5)+ results = ann.search(data[0], 10)+ self.assertGreater(len(results[0]), 0)++ # Batch search with multiple queries exceeding document count+ results = ann.search(data, 10)+ self.assertEqual(len(results), data.shape[0])+ for result in results:+ self.assertGreater(len(result), 0)++ ann.close()+@patch("sqlalchemy.orm.Query.limit")def testPGSparse(self, query):"""diff --git a/test/python/testscoring/testsparse.py b/test/python/testscoring/testsparse.pyindex 179354e1..9d54613e 100644--- a/test/python/testscoring/testsparse.py+++ b/test/python/testscoring/testsparse.py@@ -102,6 +102,77 @@ class TestSparse(unittest.TestCase):self.assertIsNotNone(scoring)scoring.close()+ def testBayes(self):+ """+ Test BB25 Bayesian normalization for sparse scoring+ """++ config = {+ "method": "sparse",+ "path": "sparse-encoder-testing/splade-bert-tiny-nq",+ "normalize": "bb25",+ }+ scoring = ScoringFactory.create(config)+ scoring.index((uid, {"text": text}, tags) for uid, text, tags in self.data)++ # Verify Bayesian mode flags+ self.assertTrue(scoring.isbayes())+ self.assertTrue(scoring.isnormalized())++ # Search and validate scores are calibrated probabilities in [0, 1]+ results = scoring.search("lottery ticket", 3)+ self.assertGreater(len(results), 0)+ for _, score in results:+ self.assertGreaterEqual(score, 0.0)+ self.assertLessEqual(score, 1.0)++ # Batch search+ results = scoring.batchsearch(["lottery ticket", "ice shelf"], 3)+ self.assertEqual(len(results), 2)+ for query_results in results:+ for _, score in query_results:+ self.assertGreaterEqual(score, 0.0)+ self.assertLessEqual(score, 1.0)++ scoring.close()++ def testBayesDict(self):+ """+ Test BB25 normalization with dict config+ """++ config = {+ "method": "sparse",+ "path": "sparse-encoder-testing/splade-bert-tiny-nq",+ "normalize": {"method": "bb25", "alpha": 2.0},+ }+ scoring = ScoringFactory.create(config)+ scoring.index((uid, {"text": text}, tags) for uid, text, tags in self.data)++ self.assertTrue(scoring.isbayes())++ results = scoring.search("lottery ticket", 3)+ self.assertGreater(len(results), 0)+ for _, score in results:+ self.assertGreaterEqual(score, 0.0)+ self.assertLessEqual(score, 1.0)++ scoring.close()++ def testBayesNonBayes(self):+ """+ Test that non-Bayesian string normalize values do not activate Bayesian mode+ """++ config = {+ "method": "sparse",+ "path": "sparse-encoder-testing/splade-bert-tiny-nq",+ "normalize": "default",+ }+ scoring = ScoringFactory.create(config)+ self.assertFalse(scoring.isbayes())+ scoring.close()+def testIVFFlat(self):"""Test sparse vectors with IVFFlat clustering