Skip to content

armgit / txtai

Public Imported from GitHub
0

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

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.py
index 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 pickle
import numpy as np
@@ -105,7 +104,7 @@ class Transform:
with open(stream, "rb") as queue:
x = 0
for _ in range(batches):
- batch = pickle.load(queue)
+ batch = np.load(queue)
embeddings[x : x + batch.shape[0]] = batch
x += batch.shape[0]
diff --git a/src/python/txtai/vectors/base.py b/src/python/txtai/vectors/base.py
index 84405862..9e0983ef 100644
--- a/src/python/txtai/vectors/base.py
+++ b/src/python/txtai/vectors/base.py
@@ -2,13 +2,10 @@
Vectors module
"""
-import pickle
import tempfile
import 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.py
index 35924604..d9751490 100644
--- a/src/python/txtai/vectors/words.py
+++ b/src/python/txtai/vectors/words.py
@@ -4,7 +4,6 @@ Word Vectors module
import logging
import os
-import pickle
import tempfile
from errno import ENOENT
@@ -22,7 +21,6 @@ except ImportError:
WORDS = False
from ..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 += 1
embeddings = []
# Final embeddings batch
if embeddings:
- pickle.dump(np.array(embeddings, dtype=np.float32), output, protocol=__pickle__)
+ np.save(output, np.array(embeddings, dtype=np.float32))
batches += 1
return (ids, dimensions, batches, stream)
diff --git a/test/python/testvectors/testcustom.py b/test/python/testvectors/testcustom.py
index 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 pickle
import unittest
+import numpy as np
+
from txtai.vectors import VectorsFactory
@@ -39,7 +40,7 @@ class TestCustom(unittest.TestCase):
# Test shape of serialized embeddings
with 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.py
index 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 pickle
import unittest
import numpy as np
@@ -44,7 +43,7 @@ class TestExternal(unittest.TestCase):
# Test shape of serialized embeddings
with 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.py
index 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 pickle
import unittest
import numpy as np
@@ -41,7 +40,7 @@ class TestHFVectors(unittest.TestCase):
# Test shape of serialized embeddings
with 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.py
index eebefc00..1716519e 100644
--- a/test/python/testvectors/testlitellm.py
+++ b/test/python/testvectors/testlitellm.py
@@ -4,12 +4,13 @@ LiteLLM module tests
import json
import os
-import pickle
import unittest
from http.server import HTTPServer, BaseHTTPRequestHandler
from threading import Thread
+import numpy as np
+
from txtai.vectors import VectorsFactory
@@ -80,4 +81,4 @@ class TestLiteLLM(unittest.TestCase):
# Test shape of serialized embeddings
with 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.py
index 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 pickle
import unittest
+import numpy as np
+
from txtai.vectors import VectorsFactory
@@ -36,4 +37,4 @@ class TestLlamaCpp(unittest.TestCase):
# Test shape of serialized embeddings
with 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.py
index 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 pickle
import tempfile
import unittest
from 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 embeddings
with 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 embeddings
with 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 embeddings
with 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 embeddings
with 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):
"""