💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
Preserve workflow and task configuration dictionaries (#1377)
leeshengis93a767f00430
diff --git a/src/python/txtai/app/base.py b/src/python/txtai/app/base.pyindex dc0047e3..fa5c246b 100644--- a/src/python/txtai/app/base.py+++ b/src/python/txtai/app/base.py@@ -258,7 +258,7 @@ class Application:"""# Check for task shorthand syntax- task = {"action": task} if isinstance(task, (str, list)) else task+ task = {"action": task} if isinstance(task, (str, list)) else task.copy()if "action" in task:action = task["action"]diff --git a/src/python/txtai/workflow/factory.py b/src/python/txtai/workflow/factory.pyindex 506f077c..087dbd03 100644--- a/src/python/txtai/workflow/factory.py+++ b/src/python/txtai/workflow/factory.py@@ -24,16 +24,20 @@ class WorkflowFactory:Workflow"""+ # Copy configuration before resolving tasks+ config = config.copy()+# Resolve workflow taskstasks = []for tconfig in config["tasks"]:+ tconfig = tconfig.copy()task = tconfig.pop("task") if "task" in tconfig else ""tasks.append(TaskFactory.create(tconfig, task))config["tasks"] = tasksif "stream" in config:- sconfig = config["stream"]+ sconfig = config["stream"].copy()task = sconfig.pop("task") if "task" in sconfig else "stream"config["stream"] = TaskFactory.create(sconfig, task)diff --git a/src/python/txtai/workflow/task/factory.py b/src/python/txtai/workflow/task/factory.pyindex 9d46f1a2..69cc14d2 100644--- a/src/python/txtai/workflow/task/factory.py+++ b/src/python/txtai/workflow/task/factory.py@@ -47,6 +47,9 @@ class TaskFactory:Task"""+ # Copy configuration before binding arguments+ config = config.copy()+# Create lambda function if additional arguments presentif "args" in config:args = config.pop("args")diff --git a/test/python/testworkflow.py b/test/python/testworkflow.pyindex 14546f87..ce88f7cd 100644--- a/test/python/testworkflow.py+++ b/test/python/testworkflow.py@@ -3,6 +3,7 @@ Workflow module tests"""import contextlib+import copyimport globimport ioimport os@@ -16,10 +17,13 @@ import numpy as npimport torchfrom txtai.api import API+from txtai.app import Applicationfrom txtai.embeddings import Documents, Embeddingsfrom txtai.pipeline import Nop, Segmentation, Summary, Translation, Textractorfrom txtai.workflow import (Workflow,+ WorkflowFactory,+ TaskFactory,Task,ConsoleTask,ExportTask,@@ -76,6 +80,41 @@ class TestWorkflow(unittest.TestCase):- transform"""+ def testApplicationConfig(self):+ """+ Test that an application can reuse its workflow configuration+ """++ workflow = {+ "tasks": [{"task": "template", "template": "Hi {text}"}, {"action": "nop", "args": []}],+ "stream": {"task": "stream", "action": "nop", "batch": True},+ }+ config = {"workflow": {"test": workflow}}+ expected = copy.deepcopy(config)+ for _ in range(2):+ app = Application(config)+ self.assertEqual(list(app.workflow("test", ["x"])), ["Hi x"])+ self.assertEqual(config, expected)++ def testFactoryConfig(self):+ """+ Test that factories preserve task, argument and stream configuration+ """++ def action(values, suffix):+ return [value + suffix for value in values]++ args = ["!"]+ task = {"action": action, "args": args}+ config = {"tasks": [dict(task, task="")], "stream": {"task": "stream", "action": iter, "batch": True}}+ expected = copy.deepcopy(config)+ for _ in range(2):+ self.assertEqual(TaskFactory.create(task, "")(["x"]), ["x!"])+ self.assertIs(task["action"], action)+ self.assertIs(task["args"], args)+ self.assertEqual(list(WorkflowFactory.create(config, "test")(["x"])), ["x!"])+ self.assertEqual(config, expected)+def testBaseWorkflow(self):"""Test a basic workflow