Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Add tests

davidmezzetti20526e6bd7a4
diff --git a/src/python/txtai/workflow/task/template.py b/src/python/txtai/workflow/task/template.py
index dbe25324..f1e073da 100644
--- a/src/python/txtai/workflow/task/template.py
+++ b/src/python/txtai/workflow/task/template.py
@@ -10,7 +10,7 @@ from .file import Task
class TemplateTask(Task):
"""
Task that generates text from a template and task inputs. Templates can be used to prepare data for a number of tasks
- including generating large languge model (LLM) prompts.
+ including generating large language model (LLM) prompts.
"""
def register(self, template=None, rules=None, strict=True):
diff --git a/test/python/testdatabase/testembeddings.py b/test/python/testdatabase/testembeddings.py
index f695a3bf..e7664d2e 100644
--- a/test/python/testdatabase/testembeddings.py
+++ b/test/python/testdatabase/testembeddings.py
@@ -469,6 +469,14 @@ class TestEmbeddings(unittest.TestCase):
with self.assertRaises(SQLError):
self.embeddings.search("select * from txtai where bad,query")
+ def testTerms(self):
+ """
+ Test extracting keyword terms from query
+ """
+
+ result = self.embeddings.terms("select * from txtai where similar('keyword terms')")
+ self.assertEqual(result, "keyword terms")
+
def testUpsert(self):
"""
Test upsert
diff --git a/test/python/testpipeline/testextractor.py b/test/python/testpipeline/testextractor.py
index 47cb20e6..4da507ed 100644
--- a/test/python/testpipeline/testextractor.py
+++ b/test/python/testpipeline/testextractor.py
@@ -86,7 +86,7 @@ class TestExtractor(unittest.TestCase):
question = "How many home runs?"
- answers = extractor([(question, question, question, True)], self.data)
+ answers = extractor([(question, question, question, False)], self.data)
self.assertIsNotNone(answers)
def testNoAnswer(self):
@@ -116,6 +116,23 @@ class TestExtractor(unittest.TestCase):
answers = extractor([(question, question, question, True)], self.data)
self.assertTrue(answers[0][1].startswith("Giants hit 3 HRs"))
+ def testOutputs(self):
+ """
+ Test output formatting rules
+ """
+
+ question = "How many home runs?"
+
+ # Test flatten to list of answers
+ extractor = Extractor(self.embeddings, "distilbert-base-cased-distilled-squad", True, output="flatten")
+ answers = extractor([(question, question, question, True)], self.data)
+ self.assertTrue(answers[0].startswith("Giants hit 3 HRs"))
+
+ # Test reference field
+ extractor = Extractor(self.embeddings, "distilbert-base-cased-distilled-squad", True, output="reference")
+ answers = extractor([(question, question, question, True)], self.data)
+ self.assertTrue(self.data[answers[0][2]].startswith("Giants hit 3 HRs"))
+
def testSearch(self):
"""
Test qa extraction with an embeddings search for context
@@ -145,7 +162,7 @@ class TestExtractor(unittest.TestCase):
Context:
"""
- answers = extractor([("prompt", prompt, prompt, True)], self.data)
+ answers = extractor([("prompt", prompt, prompt, False)], self.data)
self.assertEqual(answers[0][1], "3")
def testSimilarity(self):
diff --git a/test/python/testworkflow.py b/test/python/testworkflow.py
index a4649bfe..874aa51c 100644
--- a/test/python/testworkflow.py
+++ b/test/python/testworkflow.py
@@ -16,7 +16,19 @@ import torch
from txtai.api import API
from txtai.embeddings import Documents, Embeddings
from txtai.pipeline import Nop, Segmentation, Summary, Translation, Textractor
-from txtai.workflow import Workflow, Task, ConsoleTask, ExportTask, FileTask, ImageTask, RetrieveTask, StorageTask, WorkflowTask
+from txtai.workflow import (
+ Workflow,
+ Task,
+ ConsoleTask,
+ ExportTask,
+ ExtractorTask,
+ FileTask,
+ ImageTask,
+ RetrieveTask,
+ StorageTask,
+ TemplateTask,
+ WorkflowTask,
+)
# pylint: disable = C0411
from utils import Utils
@@ -375,6 +387,65 @@ class TestWorkflow(unittest.TestCase):
self.assertEqual(len(results), 19)
+ def testTemplateInput(self):
+ """
+ Test template task input
+ """
+
+ workflow = Workflow([TemplateTask(template="This is a {text}")])
+
+ # Test with string inputs
+ results = list(workflow(["prompt"]))
+ self.assertEqual(results[0], "This is a prompt")
+
+ # Test with dict inputs
+ results = list(workflow([{"text": "prompt"}]))
+ self.assertEqual(results[0], "This is a prompt")
+
+ # Test with tuple inputs
+ workflow = Workflow([TemplateTask(template="This is a {arg0}", unpack=False)])
+ results = list(workflow([("prompt",)]))
+ self.assertEqual(results[0], "This is a prompt")
+
+ # Test invalid inputs
+ with self.assertRaises(KeyError):
+ workflow = Workflow([TemplateTask(template="No variables")])
+ results = list(workflow([{"unused": "prompt"}]))
+
+ # Test no template
+ workflow = Workflow([TemplateTask()])
+ results = list(workflow(["prompt"]))
+ self.assertEqual(results[0], "prompt")
+
+ def testTemplateRules(self):
+ """
+ Test template task rules
+ """
+
+ # Test rule applied
+ workflow = Workflow([TemplateTask(template="This is a {text}", rules={"text": "Test skip"})])
+ results = list(workflow([{"text": "Test skip"}]))
+ self.assertEqual(results[0], "Test skip")
+
+ # Test rule not applied
+ results = list(workflow([{"text": "prompt"}]))
+ self.assertEqual(results[0], "This is a prompt")
+
+ def testTemplateExtractor(self):
+ """
+ Test extractor template task
+ """
+
+ # Test extractor outputs
+ workflow = Workflow([ExtractorTask(template="This is a {text}")])
+ results = list(workflow(["prompt"]))
+ self.assertEqual(results[0], {"query": "prompt", "question": "This is a prompt"})
+
+ # Test partial extractor outputs
+ workflow = Workflow([ExtractorTask(template="This is a {text}")])
+ results = list(workflow([{"query": "query", "question": "prompt"}]))
+ self.assertEqual(results[0], {"query": "query", "question": "This is a prompt"})
+
def testTensorTransformWorkflow(self):
"""
Test a tensor workflow with list transformations