💡 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
Key the vectors model cache by configuration (#1223)
Morgan Carr767272f2d5e4
diff --git a/src/python/txtai/vectors/base.py b/src/python/txtai/vectors/base.pyindex bd058973..d90dc2ae 100644--- a/src/python/txtai/vectors/base.py+++ b/src/python/txtai/vectors/base.py@@ -96,16 +96,23 @@ class Vectors:model"""+ # Build a cache key from settings that change the loaded model when a configuration is present.+ if self.config:+ config = {"path": path, "method": self.config.get("method"), "vectors": self.config.get("vectors", {})}+ key = json.dumps(config, sort_keys=True, default=str)+ else:+ key = path+# Check if model is cached- if self.models and path in self.models:- return self.models[path]+ if self.models and key in self.models:+ return self.models[key]# Create new modelmodel = self.loadmodel(path)# Store model in cacheif self.models is not None and path:- self.models[path] = model+ self.models[key] = modelreturn modeldiff --git a/test/python/testembeddings.py b/test/python/testembeddings.pyindex a1c42d2a..10211599 100644--- a/test/python/testembeddings.py+++ b/test/python/testembeddings.py@@ -654,6 +654,53 @@ class TestEmbeddings(unittest.TestCase):# Close embeddingsembeddings.close()+ def testSubindexVectors(self):+ """+ Test subindexes with the same model path and different vectors settings+ """++ # Build data array+ data = [(uid, text, None) for uid, text in enumerate(self.data)]++ # Subindexes share a models cache. The same model path with different vectors settings must load separate models.+ path = "neuml/colbert-bert-tiny"+ embeddings = Embeddings(+ {+ "defaults": False,+ "indexes": {+ "index1": {"path": path, "vectors": {"muvera": {"repetitions": 1}}},+ "index2": {"path": path, "vectors": {"muvera": {"repetitions": 2}}},+ },+ }+ )+ embeddings.index(data)++ # MUVERA output dimensions = repetitions * 2^5 * 16+ self.assertEqual(embeddings.transform("feel good story", index="index1").shape, (512,))+ self.assertEqual(embeddings.transform("feel good story", index="index2").shape, (1024,))+ self.assertEqual(embeddings.indexes["index1"].config["dimensions"], 512)+ self.assertEqual(embeddings.indexes["index2"].config["dimensions"], 1024)+ self.assertIsNot(embeddings.indexes["index1"].model.model, embeddings.indexes["index2"].model.model)++ # Close embeddings+ embeddings.close()++ # Per-encode settings share the same loaded model+ embeddings = Embeddings(+ {+ "defaults": False,+ "indexes": {+ "index1": {"path": path, "maxlength": 32, "vectors": {"muvera": {"repetitions": 1}}},+ "index2": {"path": path, "maxlength": 64, "vectors": {"muvera": {"repetitions": 1}}},+ },+ }+ )+ embeddings.index(data)+ self.assertIs(embeddings.indexes["index1"].model.model, embeddings.indexes["index2"].model.model)++ # Close embeddings+ embeddings.close()+def testTerms(self):"""Test extracting keyword terms from queries