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 only the query argument of similar() clauses in terms and explain (#1367)

Jasperca9563daac12
diff --git a/src/python/txtai/embeddings/search/explain.py b/src/python/txtai/embeddings/search/explain.py
index b4332d89..ee4582f6 100644
--- a/src/python/txtai/embeddings/search/explain.py
+++ b/src/python/txtai/embeddings/search/explain.py
@@ -86,8 +86,9 @@ class Explain:
# Parse query
query = self.database.parse(query)
- # Extract query from similar clause
- query = " ".join([" ".join(clause) for clause in query["similar"]]) if "similar" in query else None
+ # Extract query text from similar clauses. Only the first argument is query text, the rest are
+ # candidates, index and weights.
+ query = " ".join([clause[0] for clause in query["similar"]]) if "similar" in query else None
# Return original texts if query, text or score not present
if not query or not texts or "score" not in texts[0] or "text" not in texts[0]:
diff --git a/src/python/txtai/embeddings/search/terms.py b/src/python/txtai/embeddings/search/terms.py
index f9a76d20..42f183b2 100644
--- a/src/python/txtai/embeddings/search/terms.py
+++ b/src/python/txtai/embeddings/search/terms.py
@@ -37,9 +37,10 @@ class Terms:
# Parse query
parse = self.database.parse(query)
- # Join terms from similar clauses (absent when the query has no similar() clause)
+ # 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(" ".join(s) for s in parse["similar"]))
+ terms.append(" ".join(s[0] for s in parse["similar"]))
return terms
diff --git a/test/python/testdatabase/testrdbms.py b/test/python/testdatabase/testrdbms.py
index 49108894..d5da545f 100644
--- a/test/python/testdatabase/testrdbms.py
+++ b/test/python/testdatabase/testrdbms.py
@@ -239,6 +239,21 @@ class Common:
self.assertEqual(result["text"], self.data[4])
self.assertEqual(len(result.get("tokens")), 8)
+ def testExplainSimilarArgs(self):
+ """
+ Test query explain ignores extra similar clause arguments
+ """
+
+ self.embeddings.index([(uid, text, None) for uid, text in enumerate(self.data)])
+
+ expected = self.embeddings.explain("select id, text, score from txtai where similar('feel good story')", limit=1)[0]
+
+ # Candidates and weights are not part of the query text
+ for query in ["similar('feel good story', 10)", "similar('feel good story', 0.5)"]:
+ result = self.embeddings.explain(f"select id, text, score from txtai where {query}", limit=1)[0]
+ self.assertEqual(result["text"], self.data[4])
+ self.assertEqual(result["tokens"], expected["tokens"])
+
def testExplainEmpty(self):
"""
Test query explain with no filtering criteria
diff --git a/test/python/testembeddings.py b/test/python/testembeddings.py
index 582ebaaf..9f128731 100644
--- a/test/python/testembeddings.py
+++ b/test/python/testembeddings.py
@@ -785,6 +785,10 @@ class TestEmbeddings(unittest.TestCase):
# SQL query with a similar() clause extracts its terms
self.assertEqual(embeddings.terms("select id, text from txtai where similar('feel good story')"), "feel good story")
+ # Candidates, index and weights arguments are not keyword terms
+ self.assertEqual(embeddings.terms("select id, text from txtai where similar('feel good story', 10)"), "feel good story")
+ self.assertEqual(embeddings.terms("select id, text from txtai where similar('feel good story', 'index1', 0.5)"), "feel good story")
+
# 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)