💡 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 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.pyindex 666d052f..236a3c5b 100644--- a/src/python/txtai/ann/base.py+++ b/src/python/txtai/ann/base.py@@ -16,9 +16,6 @@ class ANN:# ANN indexself.model = None- # Starting id offset- self.offset = 0-# Model configurationself.config = configdiff --git a/src/python/txtai/ann/faiss.py b/src/python/txtai/ann/faiss.pyindex 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"] += newdef delete(self, ids):# Remove specified idsdiff --git a/src/python/txtai/ann/hnsw.py b/src/python/txtai/ann/hnsw.pyindex 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 indexself.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"] += newdef delete(self, ids):# Mark elements as deleted to omit from search resultsfor uid in ids:try:self.model.mark_deleted(uid)- self.deletes += 1+ self.config["deletes"] = self.config.get("deletes", 0) + 1except RuntimeError:# Ignore label not found errorcontinue@@ -88,7 +81,7 @@ class HNSW(ANN):return resultsdef 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 indexdiff --git a/test/python/testembeddings.py b/test/python/testembeddings.pyindex 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 matchuid = self.embeddings.search("feel good story", 1)[0][0]- self.assertEqual(data[uid], data[4])+ self.assertEqual(data[uid], data[0])def testDelete(self):"""