Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Keep batchterms output aligned when a query has no similar() clause (#1440)

Mohammad Hijjawiacdef49f3816
diff --git a/src/python/txtai/embeddings/search/terms.py b/src/python/txtai/embeddings/search/terms.py
index 42f183b2..e36ae9c5 100644
--- a/src/python/txtai/embeddings/search/terms.py
+++ b/src/python/txtai/embeddings/search/terms.py
@@ -37,10 +37,10 @@ class Terms:
# Parse query
parse = self.database.parse(query)
- # Join query text from similar clauses (absent when the query has no similar() clause).
- # Only the first argument is query text, the rest are candidates, index and weights.
- if "similar" in parse:
- terms.append(" ".join(s[0] for s in parse["similar"]))
+ # Join query text from similar clauses. Only the first argument is query text, the rest are
+ # candidates, index and weights. A query without a similar() clause has no keyword terms,
+ # add None to keep the output aligned with the input queries.
+ terms.append(" ".join(s[0] for s in parse["similar"]) if "similar" in parse else None)
return terms
diff --git a/test/python/testembeddings.py b/test/python/testembeddings.py
index 555027c6..de00783b 100644
--- a/test/python/testembeddings.py
+++ b/test/python/testembeddings.py
@@ -804,6 +804,12 @@ class TestEmbeddings(unittest.TestCase):
# SQL query without a similar() clause has no keyword terms (previously raised KeyError)
self.assertEqual(embeddings.terms("select id, text from txtai where id = 1"), None)
+ # Batch results stay aligned with the input queries when a query has no similar() clause
+ self.assertEqual(
+ embeddings.batchterms(["select id, text from txtai where id = 1", "select id, text from txtai where similar('feel good story')"]),
+ [None, "feel good story"],
+ )
+
def testTruncate(self):
"""
Test dimensionality truncation