Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Select the top documents in LateEncoder by the document axis (#1227)

Morgan Carrf3945f9120c8
diff --git a/src/python/txtai/pipeline/text/lateencoder.py b/src/python/txtai/pipeline/text/lateencoder.py
index bcec323d..899927be 100644
--- a/src/python/txtai/pipeline/text/lateencoder.py
+++ b/src/python/txtai/pipeline/text/lateencoder.py
@@ -109,7 +109,7 @@ class LateEncoder(Pipeline):
scores = scores.cpu().numpy()
# Get top n matching indices and scores
- indices = np.argpartition(-scores, limit if limit and limit < scores.shape[0] else scores.shape[0] - 1)[:, :limit]
+ indices = np.argpartition(-scores, limit if limit and limit < scores.shape[1] else scores.shape[1] - 1)[:, :limit]
scores = np.take_along_axis(scores, indices, axis=1)
# argpartition doesn't order within the top n - sort each row by score descending
diff --git a/src/python/txtai/pipeline/text/similarity.py b/src/python/txtai/pipeline/text/similarity.py
index 0dd1566c..486f5307 100644
--- a/src/python/txtai/pipeline/text/similarity.py
+++ b/src/python/txtai/pipeline/text/similarity.py
@@ -57,7 +57,7 @@ class Similarity(Labels):
return self.crossencoder(query, texts, multilabel, labels=labels)
if self.lateencoder:
- return self.lateencoder(query, texts)
+ return self.lateencoder(query, texts, **kwargs)
# Call Labels pipeline for texts using input query as the candidate label
scores = super().__call__(texts, [query] if isinstance(query, str) else query, multilabel, **kwargs)
diff --git a/test/python/testpipeline/testtext/testsimilarity.py b/test/python/testpipeline/testtext/testsimilarity.py
index 47b881fd..8a38e536 100644
--- a/test/python/testpipeline/testtext/testsimilarity.py
+++ b/test/python/testpipeline/testtext/testsimilarity.py
@@ -130,6 +130,22 @@ class TestSimilarity(unittest.TestCase):
for uid, score in scores:
self.assertAlmostEqual(score, alone[uid], places=4)
+ def testLateEncoderLimit(self):
+ """
+ Test late-encoder similarity model with a limit returns the top scoring results
+ """
+
+ similarity = Similarity("neuml/colbert-bert-tiny", lateencode=True)
+ queries = ["Who won the lottery?", "Where did an iceberg collapse?"]
+
+ for query in queries:
+ results = similarity(query, self.data, limit=3)
+ scores = [score for _, score in results]
+
+ # Limited results must match the top results of the unlimited call, sorted by score descending
+ self.assertEqual([uid for uid, _ in results], [uid for uid, _ in similarity(query, self.data)[:3]])
+ self.assertEqual(scores, sorted(scores, reverse=True))
+
def testLateEncoderPadding(self):
"""
Test late-encoder scores don't change with data batch padding