Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Fix issue when workflow task generates no output, closes #214

davidmezzettiec59cce14b0e
diff --git a/src/python/txtai/workflow/task/__init__.py b/src/python/txtai/workflow/task/__init__.py
index 473e03d9..40a913e0 100644
--- a/src/python/txtai/workflow/task/__init__.py
+++ b/src/python/txtai/workflow/task/__init__.py
@@ -3,6 +3,8 @@ Task imports
"""
from .base import Task
+from .console import ConsoleTask
+from .export import ExportTask
from .factory import TaskFactory
from .file import FileTask
from .image import ImageTask
diff --git a/src/python/txtai/workflow/task/base.py b/src/python/txtai/workflow/task/base.py
index 0648b44d..9ca6aa9a 100644
--- a/src/python/txtai/workflow/task/base.py
+++ b/src/python/txtai/workflow/task/base.py
@@ -124,7 +124,8 @@ class Task:
for x, element in indexed:
if x in ids:
# Get result for process id
- result = results[ids.index(x)]
+ index = ids.index(x)
+ result = results[index] if index < len(results) else None
if isinstance(result, OneToMany):
# One to many transformations
diff --git a/src/python/txtai/workflow/task/console.py b/src/python/txtai/workflow/task/console.py
new file mode 100644
index 00000000..482290c9
--- /dev/null
+++ b/src/python/txtai/workflow/task/console.py
@@ -0,0 +1,24 @@
+"""
+ConsoleTask module
+"""
+
+import json
+
+from .base import Task
+
+
+class ConsoleTask(Task):
+ """
+ Task that prints task elements to the console.
+ """
+
+ def __call__(self, elements, executor=None):
+ # Run task
+ outputs = super().__call__(elements, executor)
+
+ # Print inputs and outputs to console
+ print("Inputs:", json.dumps(elements, indent=2))
+ print("Outputs:", json.dumps(outputs, indent=2))
+
+ # Return results
+ return outputs
diff --git a/src/python/txtai/workflow/task/export.py b/src/python/txtai/workflow/task/export.py
new file mode 100644
index 00000000..70b8dcc7
--- /dev/null
+++ b/src/python/txtai/workflow/task/export.py
@@ -0,0 +1,64 @@
+"""
+ExportTask module
+"""
+
+import datetime
+import os
+
+# Conditional import
+try:
+ import pandas as pd
+
+ PANDAS = True
+except ImportError:
+ PANDAS = False
+
+from .base import Task
+
+
+class ExportTask(Task):
+ """
+ Task that exports task elements using Pandas.
+ """
+
+ def register(self, output=None, timestamp=None):
+ """
+ Add export parameters to task. Checks if required dependencies are installed.
+
+ Args:
+ output: output file path
+ timestamp: true if output file should be timestamped
+ """
+
+ if not PANDAS:
+ raise ImportError('ExportTask is not available - install "workflow" extra to enable')
+
+ # pylint: disable=W0201
+ self.output = output
+ self.timestamp = timestamp
+
+ def __call__(self, elements, executor=None):
+ # Run task
+ outputs = super().__call__(elements, executor)
+
+ # Get output file extension
+ output = self.output
+ parts = list(os.path.splitext(output))
+ extension = parts[-1].lower()
+
+ # Add timestamp to filename
+ if self.timestamp:
+ timestamp = datetime.datetime.now().strftime("%Y%m%dT%H%M%S")
+ parts[-1] = timestamp + parts[-1]
+
+ # Create full path to output file
+ output = ".".join(parts)
+
+ # Write output
+ if extension == ".xlsx":
+ pd.DataFrame(outputs).to_excel(output, index=False)
+ else:
+ pd.DataFrame(outputs).to_csv(output, index=False)
+
+ # Return results
+ return outputs
diff --git a/src/python/txtai/workflow/task/factory.py b/src/python/txtai/workflow/task/factory.py
index b73192c0..8ef45481 100644
--- a/src/python/txtai/workflow/task/factory.py
+++ b/src/python/txtai/workflow/task/factory.py
@@ -27,6 +27,7 @@ class TaskFactory:
# Get parent package
task = ".".join(__name__.split(".")[:-1]) + "." + task.capitalize() + "Task"
+ # Attempt to load custom task
parts = task.split(".")
module = ".".join(parts[:-1])
m = __import__(module)