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 various issues with subindex reloading #618

davidmezzettifd6bf1263d46
diff --git a/src/python/txtai/embeddings/base.py b/src/python/txtai/embeddings/base.py
index 63d89630..d6370471 100644
--- a/src/python/txtai/embeddings/base.py
+++ b/src/python/txtai/embeddings/base.py
@@ -514,12 +514,8 @@ class Embeddings:
if apath:
return os.path.exists(apath)
- # Return true if path has a config or config.json file and an embeddings (dense) or scoring (sparse) file
- return (
- path
- and (os.path.exists(f"{path}/config") or os.path.exists(f"{path}/config.json"))
- and (os.path.exists(f"{path}/embeddings") or os.path.exists(f"{path}/scoring"))
- )
+ # Return true if path has a config.json or config file with an offset set
+ return path and (os.path.exists(f"{path}/config.json") or os.path.exists(f"{path}/config")) and "offset" in self.loadconfig(path)
def load(self, path=None, cloud=None, config=None, **kwargs):
"""
@@ -809,7 +805,7 @@ class Embeddings:
def loadconfig(self, path):
"""
- Loads index configuration. This method supports both config pickle files and config.json files.
+ Loads index configuration. This method supports both config.json and config pickle files.
Args:
path: path to directory
diff --git a/src/python/txtai/embeddings/index/indexes.py b/src/python/txtai/embeddings/index/indexes.py
index ee05376c..e3c3c1f2 100644
--- a/src/python/txtai/embeddings/index/indexes.py
+++ b/src/python/txtai/embeddings/index/indexes.py
@@ -155,7 +155,10 @@ class Indexes:
"""
for name, index in self.indexes.items():
- index.load(os.path.join(path, name))
+ # Load subindex if it exists, subindexes aren't required to have data
+ directory = os.path.join(path, name)
+ if index.exists(directory):
+ index.load(directory)
def save(self, path):
"""
diff --git a/test/python/testdatabase/testrdbms.py b/test/python/testdatabase/testrdbms.py
index 92ea3771..83ea1ad2 100644
--- a/test/python/testdatabase/testrdbms.py
+++ b/test/python/testdatabase/testrdbms.py
@@ -724,6 +724,43 @@ class Common:
# Close embeddings
embeddings.close()
+ def testSubindexEmpty(self):
+ """
+ Test loading an empty subindex
+ """
+
+ # Build data array
+ data = [(uid, {"column1": text}, None) for uid, text in enumerate(self.data)]
+
+ # Disable top-level indexing and create subindexes
+ embeddings = Embeddings(
+ {
+ "content": self.backend,
+ "defaults": False,
+ "indexes": {
+ "index1": {"path": "sentence-transformers/nli-mpnet-base-v2", "columns": {"text": "column1"}},
+ "index2": {"path": "sentence-transformers/nli-mpnet-base-v2", "columns": {"text": "column2"}},
+ },
+ }
+ )
+ embeddings.index(data)
+
+ # Generate temp file path
+ index = os.path.join(tempfile.gettempdir(), f"embeddings.{self.category()}.subindexempty")
+
+ # Save index
+ embeddings.save(index)
+
+ # Test exists
+ self.assertTrue(embeddings.exists(index))
+
+ # Load index
+ embeddings.load(index)
+
+ # Test search
+ result = embeddings.search("feel good story", 1)[0]
+ self.assertEqual(result["text"], data[4][1]["text"])
+
def testTerms(self):
"""
Test extracting keyword terms from query