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 pipeline factory #76

davidmezzetti1f78957aa6c4
diff --git a/src/python/txtai/pipeline/__init__.py b/src/python/txtai/pipeline/__init__.py
index 8c1e67a1..8006b0a8 100644
--- a/src/python/txtai/pipeline/__init__.py
+++ b/src/python/txtai/pipeline/__init__.py
@@ -4,6 +4,7 @@ Pipeline imports
from .base import Pipeline
from .extractor import Extractor
+from .factory import Factory
from .hfmodel import HFModel
from .hfpipeline import HFPipeline
from .labels import Labels
diff --git a/src/python/txtai/pipeline/factory.py b/src/python/txtai/pipeline/factory.py
new file mode 100644
index 00000000..eb9fe2cd
--- /dev/null
+++ b/src/python/txtai/pipeline/factory.py
@@ -0,0 +1,50 @@
+"""
+Pipeline factory module
+"""
+
+
+class Factory:
+ """
+ Pipeline factory. Creates new Pipeline instances.
+ """
+
+ @staticmethod
+ def get(pipeline):
+ """
+ Gets a new instance of pipeline class.
+
+ Args:
+ pipeline: Pipeline instance class
+
+ Returns:
+ Pipeline class
+ """
+
+ # Local pipeline if no package
+ if "." not in pipeline:
+ # Get parent package
+ pipeline = ".".join(__name__.split(".")[:-1]) + "." + pipeline.capitalize()
+
+ parts = pipeline.split(".")
+ module = ".".join(parts[:-1])
+ m = __import__(module)
+ for comp in parts[1:]:
+ m = getattr(m, comp)
+
+ return m
+
+ @staticmethod
+ def create(config, pipeline):
+ """
+ Creates a new Pipeline instance
+
+ Args:
+ config: Pipeline configuration
+ pipeline: Pipeline instance class
+
+ Returns:
+ Pipeline
+ """
+
+ # Get Pipeline instance
+ return Factory.get(pipeline)(**config)