Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Check for empty documents before indexing, closes #224

davidmezzetti0a1c5e7b4c5b
diff --git a/src/python/txtai/embeddings/base.py b/src/python/txtai/embeddings/base.py
index 3e575d32..df12d111 100644
--- a/src/python/txtai/embeddings/base.py
+++ b/src/python/txtai/embeddings/base.py
@@ -95,27 +95,27 @@ class Embeddings:
with tempfile.NamedTemporaryFile(mode="wb", suffix=".npy") as buffer:
# Load documents into database and transform to vectors
ids, dimensions, embeddings = transform(documents, buffer)
+ if ids:
+ # Build LSA model (if enabled). Remove principal components from embeddings.
+ if self.config.get("pca"):
+ self.reducer = Reducer(embeddings, self.config["pca"])
+ self.reducer(embeddings)
- # Build LSA model (if enabled). Remove principal components from embeddings.
- if self.config.get("pca"):
- self.reducer = Reducer(embeddings, self.config["pca"])
- self.reducer(embeddings)
+ # Normalize embeddings
+ self.normalize(embeddings)
- # Normalize embeddings
- self.normalize(embeddings)
+ # Save index dimensions
+ self.config["dimensions"] = dimensions
- # Save index dimensions
- self.config["dimensions"] = dimensions
+ # Create approximate nearest neighbor index
+ self.ann = ANNFactory.create(self.config)
- # Create approximate nearest neighbor index
- self.ann = ANNFactory.create(self.config)
+ # Add embeddings to the index
+ self.ann.index(embeddings)
- # Add embeddings to the index
- self.ann.index(embeddings)
-
- # Save indexids-ids mapping for indexes with no database, except when this is a reindex action
- if not reindex and not self.database:
- self.config["ids"] = ids
+ # Save indexids-ids mapping for indexes with no database, except when this is a reindex action
+ if not reindex and not self.database:
+ self.config["ids"] = ids
def upsert(self, documents):
"""
@@ -138,16 +138,16 @@ class Embeddings:
with tempfile.NamedTemporaryFile(mode="wb", suffix=".npy") as buffer:
# Load documents into database and transform to vectors
ids, _, embeddings = transform(documents, buffer)
+ if ids:
+ # Normalize embeddings
+ self.normalize(embeddings)
- # Normalize embeddings
- self.normalize(embeddings)
-
- # Append embeddings to the index
- self.ann.append(embeddings)
+ # Append embeddings to the index
+ self.ann.append(embeddings)
- # Save indexids-ids mapping for indexes with no database
- if not self.database:
- self.config["ids"] = self.config["ids"] + ids
+ # Save indexids-ids mapping for indexes with no database
+ if not self.database:
+ self.config["ids"] = self.config["ids"] + ids
def delete(self, ids):
"""
diff --git a/src/python/txtai/embeddings/transform.py b/src/python/txtai/embeddings/transform.py
index f38ab68d..ffd1ffe1 100644
--- a/src/python/txtai/embeddings/transform.py
+++ b/src/python/txtai/embeddings/transform.py
@@ -56,14 +56,16 @@ class Transform:
# Transform documents to vectors and load into database
ids, dimensions, batches, stream = self.model.index(self.stream(documents))
- # Load streamed embeddings back to memory
- embeddings = np.memmap(buffer, dtype=np.float32, shape=(len(ids), dimensions), mode="w+")
- with open(stream, "rb") as queue:
- x = 0
- for _ in range(batches):
- batch = pickle.load(queue)
- embeddings[x : x + batch.shape[0]] = batch
- x += batch.shape[0]
+ # Check that embeddings are available and load as a memmap
+ embeddings = None
+ if ids:
+ embeddings = np.memmap(buffer, dtype=np.float32, shape=(len(ids), dimensions), mode="w+")
+ with open(stream, "rb") as queue:
+ x = 0
+ for _ in range(batches):
+ batch = pickle.load(queue)
+ embeddings[x : x + batch.shape[0]] = batch
+ x += batch.shape[0]
# Remove temporary file
os.remove(stream)