💡 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
Check for empty documents before indexing, closes #224
davidmezzetti0a1c5e7b4c5b
diff --git a/src/python/txtai/embeddings/base.py b/src/python/txtai/embeddings/base.pyindex 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 vectorsids, 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"] = idsdef 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 vectorsids, _, 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"] + idsdef delete(self, ids):"""diff --git a/src/python/txtai/embeddings/transform.py b/src/python/txtai/embeddings/transform.pyindex 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 databaseids, 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 fileos.remove(stream)