Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Pass additional parameters to extractor task question template #575

davidmezzettifee257fbc6c9
diff --git a/src/python/txtai/workflow/task/template.py b/src/python/txtai/workflow/task/template.py
index 71a717f4..2c1605c3 100644
--- a/src/python/txtai/workflow/task/template.py
+++ b/src/python/txtai/workflow/task/template.py
@@ -102,9 +102,14 @@ class ExtractorTask(TemplateTask):
"""
def prepare(self, element):
- # Allow partial input with the "question" field used to complete prompt template
+ # Apply prompt template using all variables except "query" and use output as question
if isinstance(element, dict):
- element["question"] = super().prepare(element["question"])
+ # Make a copy without query and run through template
+ params = dict(element)
+ params.pop("query", None)
+ params["text"] = params.pop("question")
+
+ element["question"] = super().prepare(params)
return element
# Default mode is to use element text for both query and question
diff --git a/test/python/testworkflow.py b/test/python/testworkflow.py
index ea35e4ad..60bfe480 100644
--- a/test/python/testworkflow.py
+++ b/test/python/testworkflow.py
@@ -453,6 +453,11 @@ class TestWorkflow(unittest.TestCase):
results = list(workflow([{"query": "query", "question": "prompt"}]))
self.assertEqual(results[0], {"query": "query", "question": "This is a prompt"})
+ # Test additional template parameters
+ workflow = Workflow([ExtractorTask(template="This is a {text} with another {param}")])
+ results = list(workflow([{"query": "query", "question": "prompt", "param": "value"}]))
+ self.assertEqual(results[0], {"query": "query", "question": "This is a prompt with another value", "param": "value"})
+
def testTensorTransformWorkflow(self):
"""
Test a tensor workflow with list transformations