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 issue with ANN indices after save/load. Store offsets/delete counts on self.config.

davidmezzetti502d0e5d0aaf
diff --git a/src/python/txtai/ann/base.py b/src/python/txtai/ann/base.py
index 666d052f..236a3c5b 100644
--- a/src/python/txtai/ann/base.py
+++ b/src/python/txtai/ann/base.py
@@ -16,9 +16,6 @@ class ANN:
# ANN index
self.model = None
- # Starting id offset
- self.offset = 0
-
# Model configuration
self.config = config
diff --git a/src/python/txtai/ann/faiss.py b/src/python/txtai/ann/faiss.py
index 657626ef..b4ff770e 100644
--- a/src/python/txtai/ann/faiss.py
+++ b/src/python/txtai/ann/faiss.py
@@ -43,16 +43,16 @@ class Faiss(ANN):
self.model.add_with_ids(embeddings, np.arange(embeddings.shape[0]))
# Update id offset
- self.offset = embeddings.shape[0]
+ self.config["offset"] = embeddings.shape[0]
def append(self, embeddings):
new = embeddings.shape[0]
# Append new ids
- self.model.add_with_ids(embeddings, np.arange(self.offset, self.offset + new))
+ self.model.add_with_ids(embeddings, np.arange(self.config["offset"], self.config["offset"] + new))
# Update id offset
- self.offset += new
+ self.config["offset"] += new
def delete(self, ids):
# Remove specified ids
diff --git a/src/python/txtai/ann/hnsw.py b/src/python/txtai/ann/hnsw.py
index 092680ab..71ba9428 100644
--- a/src/python/txtai/ann/hnsw.py
+++ b/src/python/txtai/ann/hnsw.py
@@ -15,13 +15,6 @@ class HNSW(ANN):
Builds an ANN model using the hnswlib library.
"""
- def __init__(self, config):
- # Parent constructor
- super().__init__(config)
-
- # Track number of successful deletes
- self.deletes = 0
-
def load(self, path):
# Load index
self.model = Index(dim=self.config["dimensions"], space=self.config["metric"])
@@ -44,26 +37,26 @@ class HNSW(ANN):
self.model.add_items(embeddings, np.arange(embeddings.shape[0]))
# Update id offset
- self.offset = embeddings.shape[0]
+ self.config["offset"] = embeddings.shape[0]
def append(self, embeddings):
new = embeddings.shape[0]
# Resize index
- self.model.resize_index(self.offset + new)
+ self.model.resize_index(self.config["offset"] + new)
# Append new ids
- self.model.add_items(embeddings, np.arange(self.offset, self.offset + new))
+ self.model.add_items(embeddings, np.arange(self.config["offset"], self.config["offset"] + new))
# Update id offset
- self.offset += new
+ self.config["offset"] += new
def delete(self, ids):
# Mark elements as deleted to omit from search results
for uid in ids:
try:
self.model.mark_deleted(uid)
- self.deletes += 1
+ self.config["deletes"] = self.config.get("deletes", 0) + 1
except RuntimeError:
# Ignore label not found error
continue
@@ -88,7 +81,7 @@ class HNSW(ANN):
return results
def count(self):
- return self.model.get_current_count() - self.deletes
+ return self.model.get_current_count() - self.config.get("deletes", 0)
def save(self, path):
# Write index
diff --git a/test/python/testembeddings.py b/test/python/testembeddings.py
index 8f4ab9cc..2d007b40 100644
--- a/test/python/testembeddings.py
+++ b/test/python/testembeddings.py
@@ -61,13 +61,13 @@ class TestEmbeddings(unittest.TestCase):
self.embeddings.upsert(data)
# Update data
- data[0] = (0, "See it first: baby panda is born at the local zoo", None)
+ data[0] = (0, "Feel good story: baby panda born", None)
self.embeddings.upsert([data[0]])
# Search for best match
uid = self.embeddings.search("feel good story", 1)[0][0]
- self.assertEqual(data[uid], data[4])
+ self.assertEqual(data[uid], data[0])
def testDelete(self):
"""