Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Updated unit tests

davidmezzetti3a44e9ae9b55
diff --git a/test/python/testann.py b/test/python/testann.py
index 153ac006..b55e2603 100644
--- a/test/python/testann.py
+++ b/test/python/testann.py
@@ -1,7 +1,8 @@
"""
-Embeddings module tests
+ANN module tests
"""
+import os
import tempfile
import unittest
@@ -9,15 +10,55 @@ import numpy as np
from txtai.ann import ANN
-class TestEmbeddings(unittest.TestCase):
+class TestANN(unittest.TestCase):
"""
- Embeddings tests
+ ANN tests
"""
- def setUp(self):
+ def testAnnoy(self):
"""
- Initialize test data.
+ Test Annoy backend
"""
- # Generate random embeddings
- # np.random.rand(x, y)
+ self.assertEqual(self.backend("annoy").config["backend"], "annoy")
+
+ @unittest.skipIf(os.name == "nt", "Faiss not installed on Windows")
+ def testFaiss(self):
+ """
+ Test Faiss backend
+ """
+
+ self.assertEqual(self.backend("faiss").config["backend"], "faiss")
+ self.assertEqual(self.backend("faiss", 5000).config["backend"], "faiss")
+
+ def testHnsw(self):
+ """
+ Test Hnswlib
+ """
+
+ self.assertEqual(self.backend("hnsw").config["backend"], "hnsw")
+
+ def testSave(self):
+ """
+ Tests ANN save/load.
+ """
+
+ with tempfile.NamedTemporaryFile(mode="wb") as output:
+ index = output.name
+
+ model = self.backend("annoy")
+ model.save(index)
+ model.load(index)
+
+ def backend(self, name, length=100):
+ """
+ Tests a backend
+ """
+
+ # Generate test data
+ data = np.random.rand(length, 300).astype(np.float32)
+
+ model = ANN.create({"backend": name, "dimensions": data.shape[1]})
+ model.index(data)
+
+ return model