Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Fix compatibility with latest version of Faiss, closes #986

davidmezzetti99e3457b1ffc
diff --git a/src/python/txtai/ann/dense/faiss.py b/src/python/txtai/ann/dense/faiss.py
index 7d010fc0..9ac427f6 100644
--- a/src/python/txtai/ann/dense/faiss.py
+++ b/src/python/txtai/ann/dense/faiss.py
@@ -77,9 +77,12 @@ class Faiss(ANN):
self.backend.remove_ids(np.array(ids, dtype=np.int64))
def search(self, queries, limit):
- # Set nprobe and nflip search parameters
- self.backend.nprobe = self.nprobe()
- self.backend.nflip = self.setting("nflip", self.backend.nprobe)
+ # Set nprobe and nflip search parameters, if available
+ if hasattr(self.backend, "nprobe"):
+ self.backend.nprobe = self.nprobe()
+
+ if hasattr(self.backend, "nflip"):
+ self.backend.nflip = self.setting("nflip", self.nprobe())
# Run the query
scores, ids = self.backend.search(queries, limit)
diff --git a/test/python/testann/testdense.py b/test/python/testann/testdense.py
index 5b9263e2..cac36090 100644
--- a/test/python/testann/testdense.py
+++ b/test/python/testann/testdense.py
@@ -59,6 +59,21 @@ class TestDense(unittest.TestCase):
self.runTests("faiss")
+ def testFaissBinary(self):
+ """
+ Test Faiss backend with a binary hash index
+ """
+
+ ann = ANNFactory.create({"backend": "faiss", "quantize": 1, "dimensions": 240 * 8, "faiss": {"components": "BHash32"}})
+
+ # Generate and index dummy data
+ data = np.random.rand(100, 240).astype(np.uint8)
+ ann.index(data)
+
+ # Generate query vector and test search
+ query = np.random.rand(240).astype(np.uint8)
+ self.assertGreater(ann.search(np.array([query]), 1)[0][0][1], 0)
+
def testFaissCustom(self):
"""
Test Faiss backend with custom settings