💡 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
Add function parameter to embeddings reindex, closes #185
davidmezzetti5f9a98b5b133
diff --git a/src/python/txtai/embeddings/base.py b/src/python/txtai/embeddings/base.pyindex 3f8cc871..10491c7e 100644--- a/src/python/txtai/embeddings/base.py+++ b/src/python/txtai/embeddings/base.py@@ -197,7 +197,7 @@ class Embeddings:return deletes- def reindex(self, config, columns=None):+ def reindex(self, config, columns=None, function=None):"""Recreates the approximate nearest neighbor (ann) index using config. This method only works if documentcontent storage is enabled.@@ -205,6 +205,7 @@ class Embeddings:Args:config: new configcolumns: optional list of document columns used to rebuild data+ function: optional function to prepare content for indexing"""if self.database:@@ -217,7 +218,10 @@ class Embeddings:self.configure(config)# Reindex- self.index(self.database.reindex(columns), True)+ if function:+ self.index(function(self.database.reindex(columns)), True)+ else:+ self.index(self.database.reindex(columns), True)def transform(self, document):"""diff --git a/test/python/testdatabase/testencoder.py b/test/python/testdatabase/testencoder.pyindex f28a575e..a111e77c 100644--- a/test/python/testdatabase/testencoder.py+++ b/test/python/testdatabase/testencoder.py@@ -3,7 +3,11 @@ Tests encoding/decoding database objects"""import glob+import osimport unittest+import tempfile++from io import BytesIOfrom PIL import Image@@ -99,3 +103,35 @@ class TestEncoder(unittest.TestCase):self.assertTrue(result["id"].endswith("stars.jpg"))self.assertTrue(isinstance(result["object"], Image.Image))++ def testReindexFunction(self):+ """+ Tests reindex with objects and a function+ """++ try:+ # Streaming function that loads images on the fly+ def prepare(documents):+ for uid, data, tags in documents:+ yield (uid, Image.open(data), tags)++ # Create an index for the list of images+ self.embeddings.index(self.data)++ # Set default encoder and use function to load images+ self.embeddings.config["objects"] = True++ # Save and load index to force default encoder+ index = os.path.join(tempfile.gettempdir(), "objects")+ self.embeddings.save(index)+ self.embeddings.load(index)++ # Reindex images+ self.embeddings.reindex({"method": "sentence-transformers", "path": "sentence-transformers/clip-ViT-B-32"}, function=prepare)++ result = self.embeddings.search("select id, object from txtai where similar('universe') limit 1")[0]++ self.assertTrue(result["id"].endswith("stars.jpg"))+ self.assertTrue(isinstance(result["object"], BytesIO))+ finally:+ self.embeddings.config["objects"] = "image"