Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Return no results from Reranker when a search has no matches (#1350)

Moinuddin Shaikc486f4706ebc
diff --git a/src/python/txtai/pipeline/text/reranker.py b/src/python/txtai/pipeline/text/reranker.py
index 71d2253b..743253ed 100644
--- a/src/python/txtai/pipeline/text/reranker.py
+++ b/src/python/txtai/pipeline/text/reranker.py
@@ -63,20 +63,19 @@ class Reranker(Pipeline):
# Re-rank using similarity pipeline
ranked = []
for x, result in enumerate(results):
- texts = [row["text"] for row in result]
-
- if self.cache is not None and (namespace := self.modelkey()) and all("id" in row for row in result):
- with self.lock:
- texts = self.vectors(result, namespace)
- if not result:
- ranked.append([])
- continue
+ # Only score when a search has results
+ if result:
+ texts = [row["text"] for row in result]
+
+ if self.cache is not None and (namespace := self.modelkey()) and all("id" in row for row in result):
+ with self.lock:
+ texts = self.vectors(result, namespace)
+ for uid, score in self.similarity(queries[x], texts):
+ result[uid]["score"] = score
+ else:
+ # Score results and merge
for uid, score in self.similarity(queries[x], texts):
result[uid]["score"] = score
- else:
- # Score results and merge
- for uid, score in self.similarity(queries[x], texts):
- result[uid]["score"] = score
# Sort and take top n sorted results
ranked.append(sorted(result, key=lambda row: row["score"], reverse=True)[:limit])
@@ -118,8 +117,6 @@ class Reranker(Pipeline):
with self.lock:
self.reset(namespace)
- if not rows:
- return []
keys, vectors, misses = [], [], []
for row in rows:
key = (namespace, row["id"], hashlib.sha256(row["text"].encode("utf-8")).hexdigest())
diff --git a/test/python/testpipeline/testtext/testreranker.py b/test/python/testpipeline/testtext/testreranker.py
index 55ab1eb6..5f2f84ff 100644
--- a/test/python/testpipeline/testtext/testreranker.py
+++ b/test/python/testpipeline/testtext/testreranker.py
@@ -238,6 +238,26 @@ class TestReranker(unittest.TestCase):
self.assertEqual(ranker("select id, text, score from txtai where id = 'missing'"), [])
self.assertEqual(encode.call_count, 0)
+ def testEmpty(self):
+ """Return no results when a search has no matches, with or without a cache."""
+
+ empty = "select id, text, score from txtai where id = 'missing'"
+ models = [
+ self.similarity,
+ Similarity("cross-encoder/ms-marco-MiniLM-L-2-v2", crossencode=True),
+ Similarity("prajjwal1/bert-medium-mnli"),
+ ]
+
+ for similarity in models:
+ for cache in (None, True):
+ ranker = Reranker(self.embeddings, similarity, cache=cache)
+ self.assertEqual(ranker(empty), [])
+
+ # Empty and non-empty queries in the same batch
+ results = ranker([empty, "lottery winner"], 1)
+ self.assertEqual(results[0], [])
+ self.assertEqual(len(results[1]), 1)
+
def testCacheBypass(self):
"""Keep text scoring for missing ids, non-late encoders and batch centering."""