Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Update test to workaround HF Hub HTTP 429 issues, closes #926

davidmezzetti6ca8a61856e7
diff --git a/test/python/testdatabase/testrdbms.py b/test/python/testdatabase/testrdbms.py
index 76fad714..35899a84 100644
--- a/test/python/testdatabase/testrdbms.py
+++ b/test/python/testdatabase/testrdbms.py
@@ -381,7 +381,7 @@ class Common:
# Build data array
data = [(uid, text, None) for uid, text in enumerate(self.data)]
- # Index data with sparse + dense vectors
+ # Index data with sparse keyword vectors
embeddings = Embeddings({"keyword": True, "content": self.backend})
embeddings.index(data)
@@ -689,6 +689,44 @@ class Common:
result = self.embeddings.search("select * from txtai where text like :x", parameters={"x": "%iceberg%"})[0]
self.assertEqual(result["text"], self.data[1])
+ def testSparse(self):
+ """
+ Test sparse vector search
+ """
+
+ # Build data array
+ data = [(uid, text, None) for uid, text in enumerate(self.data)]
+
+ # Index data with sparse vectors
+ embeddings = Embeddings({"sparse": "sparse-encoder-testing/splade-bert-tiny-nq", "content": self.backend})
+ embeddings.index(data)
+
+ # Run search
+ result = embeddings.search("lottery ticket", 1)[0]
+ self.assertEqual(result["text"], data[4][1])
+
+ # Test count method
+ self.assertEqual(embeddings.count(), len(data))
+
+ # Generate temp file path
+ index = os.path.join(tempfile.gettempdir(), f"embeddings.{self.category()}.sparse")
+
+ # Test load/save
+ embeddings.save(index)
+ embeddings.load(index)
+
+ # Run search
+ result = embeddings.search("lottery ticket", 1)[0]
+ self.assertEqual(result["text"], data[4][1])
+
+ # Update data
+ data[0] = (0, "Feel good story: baby panda born", None)
+ embeddings.upsert([data[0]])
+
+ # Search for best match
+ result = embeddings.search("feel good story", 1)[0]
+ self.assertEqual(result["text"], data[0][1])
+
def testSubindex(self):
"""
Test subindex
diff --git a/test/python/testpipeline/testtext/testtranslation.py b/test/python/testpipeline/testtext/testtranslation.py
index 2980abf2..79f878a3 100644
--- a/test/python/testpipeline/testtext/testtranslation.py
+++ b/test/python/testpipeline/testtext/testtranslation.py
@@ -12,14 +12,21 @@ class TestTranslation(unittest.TestCase):
Translation tests.
"""
+ @classmethod
+ def setUpClass(cls):
+ """
+ Create single translation instance.
+ """
+
+ cls.translate = Translation()
+
def testDetect(self):
"""
Test language detection
"""
- translate = Translation()
test = ["This is a test language detection."]
- language = translate.detect(test)
+ language = self.translate.detect(test)
self.assertListEqual(language, ["en"])
@@ -43,10 +50,8 @@ class TestTranslation(unittest.TestCase):
Test a translation longer than max tokenization length
"""
- translate = Translation()
-
text = "This is a test translation to Spanish. " * 100
- translation = translate(text, "es")
+ translation = self.translate(text, "es")
# Validate translation text
self.assertIsNotNone(translation)
@@ -56,9 +61,7 @@ class TestTranslation(unittest.TestCase):
Test a translation using M2M100 models
"""
- translate = Translation()
-
- text = translate("This is a test translation to Croatian", "hr")
+ text = self.translate("This is a test translation to Croatian", "hr")
# Validate translation text
self.assertEqual(text, "Ovo je testni prijevod na hrvatski")
@@ -68,16 +71,14 @@ class TestTranslation(unittest.TestCase):
Test a translation using Marian models
"""
- translate = Translation()
-
text = "This is a test translation into Spanish"
- translation = translate(text, "es")
+ translation = self.translate(text, "es")
# Validate translation text
self.assertEqual(translation, "Esta es una traducción de prueba al español")
# Validate translation back
- translation = translate(translation, "en")
+ translation = self.translate(translation, "en")
self.assertEqual(translation, text)
def testNoLang(self):
@@ -85,26 +86,22 @@ class TestTranslation(unittest.TestCase):
Test no matching language id
"""
- translate = Translation()
- self.assertIsNone(translate.langid([], "zz"))
+ self.assertIsNone(self.translate.langid([], "zz"))
def testNoModel(self):
"""
Test no known available model found
"""
- translate = Translation()
- self.assertEqual(translate.modelpath("zz", "en"), "Helsinki-NLP/opus-mt-mul-en")
+ self.assertEqual(self.translate.modelpath("zz", "en"), "Helsinki-NLP/opus-mt-mul-en")
def testNoTranslation(self):
"""
Test translation skipped when text already in destination language
"""
- translate = Translation()
-
text = "This is a test translation to English"
- translation = translate(text, "en")
+ translation = self.translate(text, "en")
# Validate no translation
self.assertEqual(text, translation)
@@ -115,10 +112,8 @@ class TestTranslation(unittest.TestCase):
model and language.
"""
- translate = Translation()
-
text = "This is a test translation into Spanish"
- result = translate(text, "es", showmodels=True)
+ result = self.translate(text, "es", showmodels=True)
translation, language, modelpath = result
# Validate translation text
@@ -129,7 +124,7 @@ class TestTranslation(unittest.TestCase):
self.assertEqual(modelpath, "Helsinki-NLP/opus-mt-en-es")
# Validate translation back
- result = translate(translation, "en", showmodels=True)
+ result = self.translate(translation, "en", showmodels=True)
translation, language, modelpath = result
self.assertEqual(translation, text)