Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Set the HNSW delete counter when loading an index (#1242)

Morgan Carr19ff237c3758
diff --git a/src/python/txtai/ann/dense/hnsw.py b/src/python/txtai/ann/dense/hnsw.py
index b10722fe..d4416097 100644
--- a/src/python/txtai/ann/dense/hnsw.py
+++ b/src/python/txtai/ann/dense/hnsw.py
@@ -35,6 +35,9 @@ class HNSW(ANN):
self.backend = Index(dim=self.config["dimensions"], space=self.config["metric"])
self.backend.load_index(path)
+ # Default the delete counter for configurations that don't have it
+ self.config["deletes"] = self.config.get("deletes", 0)
+
def index(self, embeddings):
# Inner product is equal to cosine similarity on normalized vectors
self.config["metric"] = "ip"
diff --git a/test/python/testann/testdense.py b/test/python/testann/testdense.py
index 2185856d..8ce99894 100644
--- a/test/python/testann/testdense.py
+++ b/test/python/testann/testdense.py
@@ -356,6 +356,26 @@ class TestDense(unittest.TestCase):
# Test with custom settings
self.runTests("hnsw", {"hnsw": {"efconstruction": 100, "m": 8, "randomseed": 0, "efsearch": 15}})
+ def testHnswLoad(self):
+ """
+ Test Hnswlib backend with a configuration that doesn't have a delete counter
+ """
+
+ # Save an index
+ index = os.path.join(tempfile.gettempdir(), "hnsw.load")
+ self.backend("hnsw", length=100).save(index)
+
+ # Open the index with a configuration that only has the settings needed to load it
+ ann = ANNFactory.create({"backend": "hnsw", "dimensions": 240, "metric": "ip"})
+ ann.load(index)
+
+ # Validate count, search and delete
+ self.assertEqual(ann.count(), 100)
+ self.assertEqual(len(ann.search(np.random.rand(1, 240).astype(np.float32), 1)[0]), 1)
+
+ ann.delete([0])
+ self.assertEqual(ann.count(), 99)
+
def testMilvus(self):
"""
Test milvus-lite backend