💡 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
Modify vector transforms to load/save data using np.load/np.save, closes #777
davidmezzetti61bf4879d5d9
diff --git a/src/python/txtai/embeddings/index/transform.py b/src/python/txtai/embeddings/index/transform.pyindex 2f3149ff..b3aba238 100644--- a/src/python/txtai/embeddings/index/transform.py+++ b/src/python/txtai/embeddings/index/transform.py@@ -3,7 +3,6 @@ Transform module"""import os-import pickleimport numpy as np@@ -105,7 +104,7 @@ class Transform:with open(stream, "rb") as queue:x = 0for _ in range(batches):- batch = pickle.load(queue)+ batch = np.load(queue)embeddings[x : x + batch.shape[0]] = batchx += batch.shape[0]diff --git a/src/python/txtai/vectors/base.py b/src/python/txtai/vectors/base.pyindex 84405862..9e0983ef 100644--- a/src/python/txtai/vectors/base.py+++ b/src/python/txtai/vectors/base.py@@ -2,13 +2,10 @@Vectors module"""-import pickleimport tempfileimport numpy as np-from ..version import __pickle__-class Vectors:"""@@ -195,7 +192,7 @@ class Vectors:embeddings = self.vectorize(documents)if embeddings is not None:dimensions = embeddings.shape[1]- pickle.dump(embeddings, output, protocol=__pickle__)+ np.save(output, embeddings)return (ids, dimensions)diff --git a/src/python/txtai/vectors/words.py b/src/python/txtai/vectors/words.pyindex 35924604..d9751490 100644--- a/src/python/txtai/vectors/words.py+++ b/src/python/txtai/vectors/words.py@@ -4,7 +4,6 @@ Word Vectors moduleimport loggingimport os-import pickleimport tempfilefrom errno import ENOENT@@ -22,7 +21,6 @@ except ImportError:WORDS = Falsefrom ..pipeline import Tokenizer-from ..version import __pickle__from .base import Vectors@@ -124,14 +122,14 @@ class WordVectors(Vectors):embeddings.append(embedding)if len(embeddings) == batchsize:- pickle.dump(np.array(embeddings, dtype=np.float32), output, protocol=__pickle__)+ np.save(output, np.array(embeddings, dtype=np.float32))batches += 1embeddings = []# Final embeddings batchif embeddings:- pickle.dump(np.array(embeddings, dtype=np.float32), output, protocol=__pickle__)+ np.save(output, np.array(embeddings, dtype=np.float32))batches += 1return (ids, dimensions, batches, stream)diff --git a/test/python/testvectors/testcustom.py b/test/python/testvectors/testcustom.pyindex ff2fdf96..b533f0cf 100644--- a/test/python/testvectors/testcustom.py+++ b/test/python/testvectors/testcustom.py@@ -3,9 +3,10 @@ Custom module tests"""import os-import pickleimport unittest+import numpy as np+from txtai.vectors import VectorsFactory@@ -39,7 +40,7 @@ class TestCustom(unittest.TestCase):# Test shape of serialized embeddingswith open(stream, "rb") as queue:- self.assertEqual(pickle.load(queue).shape, (500, 768))+ self.assertEqual(np.load(queue).shape, (500, 768))def testNotFound(self):"""diff --git a/test/python/testvectors/testexternal.py b/test/python/testvectors/testexternal.pyindex b4dd1d07..db3ae4ae 100644--- a/test/python/testvectors/testexternal.py+++ b/test/python/testvectors/testexternal.py@@ -3,7 +3,6 @@ External module tests"""import os-import pickleimport unittestimport numpy as np@@ -44,7 +43,7 @@ class TestExternal(unittest.TestCase):# Test shape of serialized embeddingswith open(stream, "rb") as queue:- self.assertEqual(pickle.load(queue).shape, (500, 768))+ self.assertEqual(np.load(queue).shape, (500, 768))def testMethod(self):"""diff --git a/test/python/testvectors/testhuggingface.py b/test/python/testvectors/testhuggingface.pyindex 02e26c0e..00d1c8a8 100644--- a/test/python/testvectors/testhuggingface.py+++ b/test/python/testvectors/testhuggingface.py@@ -3,7 +3,6 @@ Huggingface module tests"""import os-import pickleimport unittestimport numpy as np@@ -41,7 +40,7 @@ class TestHFVectors(unittest.TestCase):# Test shape of serialized embeddingswith open(stream, "rb") as queue:- self.assertEqual(pickle.load(queue).shape, (500, 768))+ self.assertEqual(np.load(queue).shape, (500, 768))def testSentenceTransformers(self):"""diff --git a/test/python/testvectors/testlitellm.py b/test/python/testvectors/testlitellm.pyindex eebefc00..1716519e 100644--- a/test/python/testvectors/testlitellm.py+++ b/test/python/testvectors/testlitellm.py@@ -4,12 +4,13 @@ LiteLLM module testsimport jsonimport os-import pickleimport unittestfrom http.server import HTTPServer, BaseHTTPRequestHandlerfrom threading import Thread+import numpy as np+from txtai.vectors import VectorsFactory@@ -80,4 +81,4 @@ class TestLiteLLM(unittest.TestCase):# Test shape of serialized embeddingswith open(stream, "rb") as queue:- self.assertEqual(pickle.load(queue).shape, (1, 768))+ self.assertEqual(np.load(queue).shape, (1, 768))diff --git a/test/python/testvectors/testllama.py b/test/python/testvectors/testllama.pyindex 9dbf48ab..99a53a4f 100644--- a/test/python/testvectors/testllama.py+++ b/test/python/testvectors/testllama.py@@ -3,9 +3,10 @@ Llama module tests"""import os-import pickleimport unittest+import numpy as np+from txtai.vectors import VectorsFactory@@ -36,4 +37,4 @@ class TestLlamaCpp(unittest.TestCase):# Test shape of serialized embeddingswith open(stream, "rb") as queue:- self.assertEqual(pickle.load(queue).shape, (1, 768))+ self.assertEqual(np.load(queue).shape, (1, 768))diff --git a/test/python/testvectors/testwordvectors.py b/test/python/testvectors/testwordvectors.pyindex 4c826e31..9130ad91 100644--- a/test/python/testvectors/testwordvectors.py+++ b/test/python/testvectors/testwordvectors.py@@ -3,12 +3,13 @@ WordVectors module tests"""import os-import pickleimport tempfileimport unittestfrom unittest.mock import patch+import numpy as np+from txtai.vectors import WordVectors, VectorsFactory@@ -70,7 +71,7 @@ class TestWordVectors(unittest.TestCase):# Test shape of serialized embeddingswith open(stream, "rb") as queue:- self.assertEqual(pickle.load(queue).shape, (1, 10))+ self.assertEqual(np.load(queue).shape, (1, 10))@patch("os.cpu_count")def testIndexBatch(self, cpucount):@@ -95,8 +96,8 @@ class TestWordVectors(unittest.TestCase):# Test shape of serialized embeddingswith open(stream, "rb") as queue:- self.assertEqual(pickle.load(queue).shape, (512, 10))- self.assertEqual(pickle.load(queue).shape, (488, 10))+ self.assertEqual(np.load(queue).shape, (512, 10))+ self.assertEqual(np.load(queue).shape, (488, 10))def testIndexSerial(self):"""@@ -117,7 +118,7 @@ class TestWordVectors(unittest.TestCase):# Test shape of serialized embeddingswith open(stream, "rb") as queue:- self.assertEqual(pickle.load(queue).shape, (1, 10))+ self.assertEqual(np.load(queue).shape, (1, 10))def testIndexSerialBatch(self):"""@@ -138,8 +139,8 @@ class TestWordVectors(unittest.TestCase):# Test shape of serialized embeddingswith open(stream, "rb") as queue:- self.assertEqual(pickle.load(queue).shape, (512, 10))- self.assertEqual(pickle.load(queue).shape, (488, 10))+ self.assertEqual(np.load(queue).shape, (512, 10))+ self.assertEqual(np.load(queue).shape, (488, 10))def testLookup(self):"""