💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
Fix limit=0 being treated as unset in search and graph query (#1521)
Arsha8d7177cca5e
diff --git a/src/python/txtai/embeddings/search/base.py b/src/python/txtai/embeddings/search/base.pyindex 4611dbec..5d0c2156 100644--- a/src/python/txtai/embeddings/search/base.py+++ b/src/python/txtai/embeddings/search/base.py@@ -60,7 +60,7 @@ class Search:"""# Default input parameters- limit = limit if limit else 3+ limit = limit if limit is not None else 3weights = weights if weights is not None else 0.5# Return empty results if there is no database and indexesdiff --git a/src/python/txtai/graph/query.py b/src/python/txtai/graph/query.pyindex 93315be1..2c6ff812 100644--- a/src/python/txtai/graph/query.py+++ b/src/python/txtai/graph/query.py@@ -60,7 +60,7 @@ class Query:logger.debug(query)# Run openCypher query- return GrandCypher(graph.backend, limit if limit else 3).run(query)+ return GrandCypher(graph.backend, limit if limit is not None else 3).run(query)def isquery(self, queries):"""diff --git a/test/python/testembeddings.py b/test/python/testembeddings.pyindex 425a3331..6669f845 100644--- a/test/python/testembeddings.py+++ b/test/python/testembeddings.py@@ -534,6 +534,24 @@ class TestEmbeddings(unittest.TestCase):# A plain integer limit still worksself.assertEqual(len(embeddings.search("select id from txtai order by id limit 3")), 3)+ def testLimitZero(self):+ """+ Test that a computed limit of 0 (e.g. remaining page slots or a quota) returns no results instead of+ falling back to the default+ """++ data = [(0, "apple pie recipe", None), (1, "apple cake recipe", None), (2, "banana bread", None)]++ embeddings = Embeddings({"keyword": True, "content": True})+ embeddings.index(data)++ # A limit that evaluates to 0 must return no results, not silently fall back to the default+ self.assertEqual(embeddings.search("select id from txtai where similar('apple')", 0), [])+ self.assertEqual(embeddings.batchsearch(["select id from txtai where similar('apple')"], 0), [[]])++ # Omitting limit still uses the documented default and finds both apple matches+ self.assertEqual(len(embeddings.search("select id from txtai where similar('apple')")), 2)+def testOffset(self):"""Test an OFFSET clause with a similar clausediff --git a/test/python/testgraph.py b/test/python/testgraph.pyindex 5e15a9a0..14733793 100644--- a/test/python/testgraph.py+++ b/test/python/testgraph.py@@ -594,6 +594,23 @@ class TestGraph(unittest.TestCase):)self.assertEqual(list(results.scan())[0], 4)+ def testSearchLimitZero(self):+ """+ Test that a computed limit of 0 (e.g. remaining page slots or a quota) returns no results instead of+ falling back to the default+ """++ self.embeddings.index([(uid, text, None) for uid, text in enumerate(self.data)])++ results = self.embeddings.search(+ """+ MATCH (A)-[]->(B)+ RETURN A, B+ """,+ limit=0,+ )+ self.assertEqual(len(results), 0)+def testSearchBatch(self):"""Test batch search