Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Use each similar clause's own number of candidates when batching (#1523)

Aditya Gupta1ebe2a5e5daf
diff --git a/src/python/txtai/embeddings/search/scan.py b/src/python/txtai/embeddings/search/scan.py
index 36369154..72745709 100644
--- a/src/python/txtai/embeddings/search/scan.py
+++ b/src/python/txtai/embeddings/search/scan.py
@@ -54,12 +54,11 @@ class Scan:
# Group by index and run
for index, iqueries in self.parse(queries, parameters).items():
- # Query limit to pass to batch search
- candidates = [query.candidates for query in iqueries if query.candidates]
- if not candidates and not default:
+ # Number of candidates per query. Queries without a candidates argument use the default.
+ if not default and any(not query.candidates for query in iqueries):
default = self.default(queries)
- candidates = max(candidates) if candidates else default
+ candidates = [query.candidates if query.candidates else default for query in iqueries]
# Query weights to pass to batch search. Each query keeps its own weights, queries without weights use the default.
weights = [query.weights if query.weights is not None else self.weights for query in iqueries]
@@ -68,10 +67,10 @@ class Scan:
# Index to run query against
index = index if index else self.index
- # Run index searches
- for x, result in enumerate(self.search([query.text for query in iqueries], candidates, weights, index)):
- # Save query id and results to later join to original query
- results[iqueries[x].uid] = (iqueries[x].qid, result)
+ # Run index searches with the largest number of candidates
+ for x, result in enumerate(self.search([query.text for query in iqueries], max(candidates), weights, index)):
+ # Save query id and results to later join to original query. Each query keeps its own number of candidates.
+ results[iqueries[x].uid] = (iqueries[x].qid, result[: candidates[x]])
# Sort by query uid and return results
return [result for _, result in sorted(results.items())]
diff --git a/test/python/testembeddings.py b/test/python/testembeddings.py
index 6669f845..ac210667 100644
--- a/test/python/testembeddings.py
+++ b/test/python/testembeddings.py
@@ -725,6 +725,32 @@ class TestEmbeddings(unittest.TestCase):
for attr in checks:
self.assertIsNotNone(getattr(embeddings, attr))
+ def testSimilarCandidates(self):
+ """
+ Test that each similar clause keeps its own number of candidates when clauses on the same index are batched
+ """
+
+ data = ["red apple fruit", "green pear fruit", "blue car vehicle", "yellow banana fruit", "fast red sports car vehicle"]
+
+ embeddings = Embeddings({"keyword": True, "content": True})
+ embeddings.index(data)
+
+ # A query without a candidates argument and a query that sets 1 candidate
+ queries = ["select id from txtai where similar('fruit')", "select id from txtai where similar('vehicle', 1)"]
+ alone = [sorted(x["id"] for x in embeddings.search(query, 3)) for query in queries]
+ self.assertEqual(alone, [["0", "1", "3"], ["2"]])
+
+ # Batching the queries must not change the results of either query
+ batch = [sorted(x["id"] for x in result) for result in embeddings.batchsearch(queries, 3)]
+ self.assertEqual(batch, alone)
+
+ # Same with both clauses in one query
+ results = embeddings.search("select id from txtai where similar('fruit') or similar('vehicle', 1)", 10)
+ self.assertEqual(sorted(x["id"] for x in results), ["0", "1", "2", "3"])
+
+ results = embeddings.search("select id from txtai where similar('fruit', 1) or similar('vehicle')", 10)
+ self.assertEqual(len([x for x in results if "fruit" in data[int(x["id"])]]), 1)
+
def testSimilarity(self):
"""
Test similarity