Skip to content

Build AI methods with your coding agent, then run them anywhere: as an MCP for chatbots, as a webapp for people, or via API for your software.

Refactor/plugin management (#129)

Louis Choquel11ee87d715fe
diff --git a/docs/pages/advanced-customization/index.md b/docs/pages/advanced-customization/index.md
index a30846b36..33fb3e03c 100644
--- a/docs/pages/advanced-customization/index.md
+++ b/docs/pages/advanced-customization/index.md
@@ -16,7 +16,6 @@ from pipelex import Pipelex
pipelex = Pipelex(
template_provider=MyTemplateProvider(),
llm_model_provider=MyLLMProvider(),
- plugin_manager=MyPluginManager(),
inference_manager=MyInferenceManager(),
pipeline_tracker=MyPipelineTracker(),
activity_manager=MyActivityManager(),
@@ -38,7 +37,6 @@ from pipelex.hub import PipelexHub
hub = PipelexHub()
hub.set_template_provider(MyTemplateProvider())
hub.set_llm_models_provider(MyLLMProvider())
-hub.set_plugin_manager(MyPluginManager())
# ... and so on for other components
```
@@ -88,54 +86,45 @@ Pipelex supports injection of the following components:
- Default: `LLMModelLibrary`
- [Details](llm-model-provider-injection.md)
-3. **Plugin Manager** (`PluginManager`)
-
- - Protocol: `PluginManagerProtocol`
- - Default: `PluginManager`
- - [Details](plugin-manager-injection.md)
-
-4. **Inference Manager** (`InferenceManager`)
+3. **Inference Manager** (`InferenceManager`)
- Protocol: `InferenceManagerProtocol`
- Default: `InferenceManager`
- [Details](inference-manager-injection.md)
-5. **Reporting Delegate** (`ReportingManager`)
+4. **Reporting Delegate** (`ReportingManager`)
- Protocol: `ReportingProtocol`
- Default: `ReportingManager` or `ReportingNoOp` if disabled
- [Details](reporting-delegate-injection.md)
-6. **Pipeline Tracker** (`PipelineTracker`)
+5. **Pipeline Tracker** (`PipelineTracker`)
- Protocol: `PipelineTrackerProtocol`
- Default: `PipelineTracker` or `PipelineTrackerNoOp` if disabled
- [Details](pipeline-tracker-injection.md)
-7. **Activity Manager** (`ActivityManager`)
+6. **Activity Manager** (`ActivityManager`)
- Protocol: `ActivityManagerProtocol`
- Default: `ActivityManager` or `ActivityManagerNoOp` if disabled
- [Details](activity-manager-injection.md)
-8. **Secrets Provider** (`EnvSecretsProvider`)
+7. **Secrets Provider** (`EnvSecretsProvider`)
- Protocol: `SecretsProviderProtocol`
- Default: `EnvSecretsProvider`
- [Details](secrets-provider-injection.md)
-9. **Content Generator** (`ContentGenerator`)
+8. **Content Generator** (`ContentGenerator`)
- Protocol: `ContentGeneratorProtocol`
- Default: `ContentGenerator`
- [Details](content-generator-injection.md)
-10. **Pipe Router** (`PipeRouter`)
+9. **Pipe Router** (`PipeRouter`)
- Protocol: `PipeRouterProtocol`
- Default: `PipeRouter`
- [Details](pipe-router-injection.md)
-## Best Practices
-
-⚠️ Under construction
diff --git a/pipelex/cogt/exceptions.py b/pipelex/cogt/exceptions.py
index eec64b48e..6e4388f9d 100644
--- a/pipelex/cogt/exceptions.py
+++ b/pipelex/cogt/exceptions.py
@@ -23,6 +23,10 @@ class SdkTypeError(CogtError):
pass
+class SdkRegistryError(CogtError):
+ pass
+
+
class LLMWorkerError(CogtError):
pass
@@ -128,5 +132,9 @@ class MissingDependencyError(CogtError):
super().__init__(error_msg)
+class MissingPluginError(CogtError):
+ pass
+
+
class OcrCapabilityError(CogtError):
pass
diff --git a/pipelex/cogt/imgg/imgg_worker_factory.py b/pipelex/cogt/imgg/imgg_worker_factory.py
index f9a822a98..29bc8b72b 100644
--- a/pipelex/cogt/imgg/imgg_worker_factory.py
+++ b/pipelex/cogt/imgg/imgg_worker_factory.py
@@ -5,9 +5,9 @@ from pipelex.cogt.imgg.imgg_engine import ImggEngine
from pipelex.cogt.imgg.imgg_platform import ImggPlatform
from pipelex.cogt.imgg.imgg_worker_abstract import ImggWorkerAbstract
from pipelex.cogt.llm.llm_models.llm_platform import LLMPlatform
-from pipelex.cogt.plugin_manager import PluginHandle
from pipelex.hub import get_plugin_manager, get_secret
from pipelex.plugins.openai.openai_imgg_worker import OpenAIImggWorker
+from pipelex.plugins.plugin_sdk_registry import PluginSdkHandle
from pipelex.reporting.reporting_protocol import ReportingProtocol
from pipelex.tools.secrets.secrets_errors import SecretNotFoundError
@@ -22,8 +22,8 @@ class ImggWorkerFactory:
imgg_engine: ImggEngine,
reporting_delegate: Optional[ReportingProtocol] = None,
) -> ImggWorkerAbstract:
- imgg_sdk_handle = PluginHandle.get_for_imgg_engine(imgg_platform=imgg_engine.imgg_platform)
- plugin_manager = get_plugin_manager()
+ imgg_sdk_handle = PluginSdkHandle.get_for_imgg_engine(imgg_platform=imgg_engine.imgg_platform)
+ plugin_sdk_registry = get_plugin_manager().plugin_sdk_registry
imgg_worker: ImggWorkerAbstract
match imgg_engine.imgg_platform:
case ImggPlatform.FAL_AI:
@@ -41,7 +41,9 @@ class ImggWorkerFactory:
from pipelex.plugins.fal.fal_imgg_worker import FalImggWorker
- imgg_sdk_instance = plugin_manager.get_imgg_sdk_instance(imgg_sdk_handle=imgg_sdk_handle) or plugin_manager.set_imgg_sdk_instance(
+ imgg_sdk_instance = plugin_sdk_registry.get_imgg_sdk_instance(
+ imgg_sdk_handle=imgg_sdk_handle
+ ) or plugin_sdk_registry.set_imgg_sdk_instance(
imgg_sdk_handle=imgg_sdk_handle,
imgg_sdk_instance=FalAsyncClient(key=fal_api_key),
)
@@ -54,7 +56,9 @@ class ImggWorkerFactory:
case ImggPlatform.OPENAI:
from pipelex.plugins.openai.openai_factory import OpenAIFactory
- imgg_sdk_instance = plugin_manager.get_llm_sdk_instance(llm_sdk_handle=imgg_sdk_handle) or plugin_manager.set_llm_sdk_instance(
+ imgg_sdk_instance = plugin_sdk_registry.get_llm_sdk_instance(
+ llm_sdk_handle=imgg_sdk_handle
+ ) or plugin_sdk_registry.set_llm_sdk_instance(
llm_sdk_handle=imgg_sdk_handle,
llm_sdk_instance=OpenAIFactory.make_openai_client(llm_platform=LLMPlatform.OPENAI),
)
diff --git a/pipelex/cogt/inference/inference_manager.py b/pipelex/cogt/inference/inference_manager.py
index 39c79b0da..26e36fbea 100644
--- a/pipelex/cogt/inference/inference_manager.py
+++ b/pipelex/cogt/inference/inference_manager.py
@@ -1,4 +1,4 @@
-from typing import Dict, Optional
+from typing import Dict, Type
from typing_extensions import override
@@ -12,6 +12,7 @@ from pipelex.cogt.llm.llm_models.llm_engine_blueprint import LLMEngineBlueprint
from pipelex.cogt.llm.llm_models.llm_engine_factory import LLMEngineFactory
from pipelex.cogt.llm.llm_worker_abstract import LLMWorkerAbstract
from pipelex.cogt.llm.llm_worker_factory import LLMWorkerFactory
+from pipelex.cogt.llm.llm_worker_internal_abstract import LLMWorkerInternalAbstract
from pipelex.cogt.ocr.ocr_engine_factory import OcrEngineFactory
from pipelex.cogt.ocr.ocr_worker_abstract import OcrWorkerAbstract
from pipelex.cogt.ocr.ocr_worker_factory import OcrWorkerFactory
@@ -31,10 +32,16 @@ class InferenceManager(InferenceManagerProtocol):
def teardown(self):
self.imgg_worker_factory = ImggWorkerFactory()
self.ocr_worker_factory = OcrWorkerFactory()
- self.llm_workers.clear()
- self.imgg_workers.clear()
- self.ocr_workers.clear()
- log.verbose("InferenceManagerAsync reset")
+ for llm_worker in self.llm_workers.values():
+ llm_worker.teardown()
+ self.llm_workers = {}
+ for imgg_worker in self.imgg_workers.values():
+ imgg_worker.teardown()
+ self.imgg_workers = {}
+ for ocr_worker in self.ocr_workers.values():
+ ocr_worker.teardown()
+ self.ocr_workers = {}
+ log.verbose("InferenceManager teardown done")
def print_workers(self):
log.debug("LLM Workers:")
@@ -60,15 +67,15 @@ class InferenceManager(InferenceManagerProtocol):
llm_handle_to_llm_engine_blueprint = get_llm_deck().llm_handles
log.verbose(f"{len(llm_handle_to_llm_engine_blueprint)} LLM engine_cards found")
for llm_handle, llm_engine_blueprint in llm_handle_to_llm_engine_blueprint.items():
- self._setup_one_llm_worker(llm_engine_blueprint=llm_engine_blueprint, llm_handle=llm_handle)
+ self._setup_one_internal_llm_worker(llm_engine_blueprint=llm_engine_blueprint, llm_handle=llm_handle)
log.verbose(f"Setup LLM worker for '{llm_handle}' on {llm_engine_blueprint.llm_platform_choice}")
log.debug("Done setting up LLM Workers (async)")
- def _setup_one_llm_worker(
+ def _setup_one_internal_llm_worker(
self,
llm_engine_blueprint: LLMEngineBlueprint,
llm_handle: str,
- ) -> LLMWorkerAbstract:
+ ) -> LLMWorkerInternalAbstract:
llm_engine = LLMEngineFactory.make_llm_engine(llm_engine_blueprint=llm_engine_blueprint)
llm_worker = LLMWorkerFactory.make_llm_worker(
llm_engine=llm_engine,
@@ -78,11 +85,7 @@ class InferenceManager(InferenceManagerProtocol):
return llm_worker
@override
- def get_llm_worker(
- self,
- llm_handle: str,
- specific_llm_engine_blueprint: Optional[LLMEngineBlueprint] = None,
- ) -> LLMWorkerAbstract:
+ def get_llm_worker(self, llm_handle: str) -> LLMWorkerAbstract:
if llm_worker := self.llm_workers.get(llm_handle):
return llm_worker
if not get_config().cogt.inference_manager_config.is_auto_setup_preset_llm:
@@ -90,15 +93,26 @@ class InferenceManager(InferenceManagerProtocol):
f"No LLM worker for '{llm_handle}', set it up or enable cogt.inference_manager_config.is_auto_setup_preset_llm"
)
- if not specific_llm_engine_blueprint:
- specific_llm_engine_blueprint = get_llm_deck().get_llm_engine_blueprint(llm_handle=llm_handle)
- llm_worker = self._setup_one_llm_worker(
- llm_engine_blueprint=specific_llm_engine_blueprint,
+ llm_engine_blueprint = get_llm_deck().get_llm_engine_blueprint(llm_handle=llm_handle)
+ llm_worker = self._setup_one_internal_llm_worker(
+ llm_engine_blueprint=llm_engine_blueprint,
llm_handle=llm_handle,
)
return llm_worker
+ @override
+ def set_llm_worker_from_external_plugin(
+ self,
+ llm_handle: str,
+ llm_worker_class: Type[LLMWorkerAbstract],
+ should_warn_if_already_registered: bool = True,
+ ):
+ if llm_handle in self.llm_workers:
+ if should_warn_if_already_registered:
+ log.warning(f"LLM worker for '{llm_handle}' already registered, skipping")
+ self.llm_workers[llm_handle] = llm_worker_class(reporting_delegate=get_report_delegate())
+
####################################################################################################
# Manage IMGG Workers
####################################################################################################
diff --git a/pipelex/cogt/inference/inference_manager_protocol.py b/pipelex/cogt/inference/inference_manager_protocol.py
index a03631506..3a9a598a7 100644
--- a/pipelex/cogt/inference/inference_manager_protocol.py
+++ b/pipelex/cogt/inference/inference_manager_protocol.py
@@ -1,7 +1,6 @@
-from typing import Optional, Protocol
+from typing import Protocol, Type
from pipelex.cogt.imgg.imgg_worker_abstract import ImggWorkerAbstract
-from pipelex.cogt.llm.llm_models.llm_engine_blueprint import LLMEngineBlueprint
from pipelex.cogt.llm.llm_worker_abstract import LLMWorkerAbstract
from pipelex.cogt.ocr.ocr_worker_abstract import OcrWorkerAbstract
@@ -20,11 +19,14 @@ class InferenceManagerProtocol(Protocol):
def setup_llm_workers(self): ...
- def get_llm_worker(
+ def get_llm_worker(self, llm_handle: str) -> LLMWorkerAbstract: ...
+
+ def set_llm_worker_from_external_plugin(
self,
llm_handle: str,
- specific_llm_engine_blueprint: Optional[LLMEngineBlueprint] = None,
- ) -> LLMWorkerAbstract: ...
+ llm_worker_class: Type[LLMWorkerAbstract],
+ should_warn_if_already_registered: bool = True,
+ ): ...
####################################################################################################
# IMG Generation Workers
diff --git a/pipelex/cogt/inference/inference_worker_abstract.py b/pipelex/cogt/inference/inference_worker_abstract.py
index 989be63a4..22f4510d4 100644
--- a/pipelex/cogt/inference/inference_worker_abstract.py
+++ b/pipelex/cogt/inference/inference_worker_abstract.py
@@ -11,6 +11,12 @@ class InferenceWorkerAbstract(ABC):
):
self.reporting_delegate = reporting_delegate
+ def setup(self):
+ pass
+
+ def teardown(self):
+ pass
+
@property
@abstractmethod
def desc(self) -> str:
diff --git a/pipelex/cogt/llm/llm_models/llm_deck.py b/pipelex/cogt/llm/llm_models/llm_deck.py
index dfe9c3265..1a2a8ff91 100644
--- a/pipelex/cogt/llm/llm_models/llm_deck.py
+++ b/pipelex/cogt/llm/llm_models/llm_deck.py
@@ -42,17 +42,14 @@ class LLMDeck(LLMDeckAbstract, ConfigModel):
@override
def get_llm_setting(self, llm_setting_or_preset_id: LLMSettingOrPresetId) -> LLMSetting:
- the_llm_setting: LLMSetting
- if isinstance(llm_setting_or_preset_id, str):
+ if isinstance(llm_setting_or_preset_id, LLMSetting):
+ return llm_setting_or_preset_id
+ else:
# it's a preset id
the_llm_preset = self.llm_presets.get(llm_setting_or_preset_id)
if not the_llm_preset:
raise LLMPresetNotFoundError(f"LLM preset '{llm_setting_or_preset_id}' not found in deck")
- the_llm_setting = the_llm_preset
- else:
- # it's an explict setting
- the_llm_setting = llm_setting_or_preset_id
- return the_llm_setting
+ return the_llm_preset
@override
def get_llm_setting_for_text(self, override: Optional[LLMSettingChoices] = None) -> LLMSetting:
@@ -139,6 +136,19 @@ class LLMDeck(LLMDeckAbstract, ConfigModel):
)
return llm_model
+ @override
+ def find_optional_llm_model(self, llm_handle: str) -> Optional[LLMModel]:
+ llm_models_provider = get_llm_models_provider()
+ llm_engine_blueprint = self.llm_handles.get(llm_handle)
+ if not llm_engine_blueprint:
+ return None
+ llm_model = llm_models_provider.get_optional_llm_model(
+ llm_name=llm_engine_blueprint.llm_name,
+ llm_version=llm_engine_blueprint.llm_version,
+ llm_platform_choice=llm_engine_blueprint.llm_platform_choice,
+ )
+ return llm_model
+
@override
@classmethod
def final_validate(cls, deck: Self):
@@ -215,11 +225,11 @@ class LLMDeck(LLMDeckAbstract, ConfigModel):
value.for_object_list_direct = None
return value
- def add_llm_handle_to_llm_engine_blueprint(self, llm_handle: str, llm_engine_default: str):
- if llm_handle in self.llm_handles:
- raise ConfigValidationError(f"LLM engine blueprint for '{llm_handle}' is already defined in llm_handle_to_llm_engine_blueprint")
+ def add_llm_name_as_handle_with_defaults(self, llm_name: str):
+ if llm_name in self.llm_handles:
+ raise ConfigValidationError(f"LLM engine blueprint for '{llm_name}' is already defined in llm deck's llm_handles")
# TODO: sort the defaults by llm family
- self.llm_handles[llm_handle] = LLMEngineBlueprint(llm_name=llm_engine_default)
+ self.llm_handles[llm_name] = LLMEngineBlueprint(llm_name=llm_name)
def validate_llm_presets(self) -> Self:
for llm_preset_id, llm_setting in self.llm_presets.items():
diff --git a/pipelex/cogt/llm/llm_models/llm_deck_abstract.py b/pipelex/cogt/llm/llm_models/llm_deck_abstract.py
index 971d64dd2..40c90d7dc 100644
--- a/pipelex/cogt/llm/llm_models/llm_deck_abstract.py
+++ b/pipelex/cogt/llm/llm_models/llm_deck_abstract.py
@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
-from typing import Dict, Optional
+from typing import Dict, List, Optional
from pydantic import Field
from typing_extensions import Self
@@ -11,6 +11,7 @@ from pipelex.cogt.llm.llm_models.llm_setting import LLMSetting, LLMSettingChoice
class LLMDeckAbstract(ABC):
llm_handles: Dict[str, LLMEngineBlueprint] = Field(default_factory=dict)
+ llm_external_handles: List[str] = Field(default_factory=list)
llm_presets: Dict[str, LLMSetting] = Field(default_factory=dict)
llm_choice_defaults: LLMSettingChoices
llm_choice_overrides: LLMSettingChoices = LLMSettingChoices(
@@ -57,6 +58,10 @@ class LLMDeckAbstract(ABC):
def find_llm_model(self, llm_handle: str) -> LLMModel:
pass
+ @abstractmethod
+ def find_optional_llm_model(self, llm_handle: str) -> Optional[LLMModel]:
+ pass
+
@classmethod
@abstractmethod
def final_validate(cls, deck: Self):
diff --git a/pipelex/cogt/llm/llm_models/llm_engine.py b/pipelex/cogt/llm/llm_models/llm_engine.py
index 92be8cf9b..383614ce2 100644
--- a/pipelex/cogt/llm/llm_models/llm_engine.py
+++ b/pipelex/cogt/llm/llm_models/llm_engine.py
@@ -31,7 +31,7 @@ platform llm id: '{self.llm_id}'"""
@property
def tag(self) -> str:
- return f"{self.llm_platform} - {self.llm_model.llm_name} - {self.llm_model.version}"
+ return f"{self.llm_platform}:{self.llm_model.llm_name}:{self.llm_model.version}:id-[{self.llm_id}]"
@property
def is_gen_object_supported(self) -> bool:
diff --git a/pipelex/cogt/llm/llm_models/llm_engine_factory.py b/pipelex/cogt/llm/llm_models/llm_engine_factory.py
index 08c12f524..a23cc6ff5 100644
--- a/pipelex/cogt/llm/llm_models/llm_engine_factory.py
+++ b/pipelex/cogt/llm/llm_models/llm_engine_factory.py
@@ -15,7 +15,7 @@ class LLMEngineFactory:
Create an instance of LLMEngine based on the parameters provided through the LLMEngineCard.
Args:
- llm_engine_blueprint: LLMEngineCard
+ llm_engine_blueprint: LLMEngineBlueprint
"""
llm_platform_choice = llm_engine_blueprint.llm_platform_choice
diff --git a/pipelex/cogt/llm/llm_models/llm_model_library.py b/pipelex/cogt/llm/llm_models/llm_model_library.py
index ebb0573cd..cefefcc78 100644
--- a/pipelex/cogt/llm/llm_models/llm_model_library.py
+++ b/pipelex/cogt/llm/llm_models/llm_model_library.py
@@ -1,5 +1,5 @@
import os
-from typing import Any, Dict, List
+from typing import Any, Dict, List, Optional
from pydantic import Field, RootModel
from typing_extensions import override
@@ -84,12 +84,25 @@ class LLMModelLibrary(LLMModelProviderAbstract, RootModel[LLMModelLibraryRoot]):
llm_version: str,
llm_platform_choice: LLMPlatformChoice,
) -> LLMModel:
+ llm_model = self.get_optional_llm_model(llm_name=llm_name, llm_version=llm_version, llm_platform_choice=llm_platform_choice)
+ if not llm_model:
+ raise LLMModelProviderError(f"Model '{llm_name}' version '{llm_version}' not found")
+ else:
+ return llm_model
+
+ @override
+ def get_optional_llm_model(
+ self,
+ llm_name: str,
+ llm_version: str,
+ llm_platform_choice: LLMPlatformChoice,
+ ) -> Optional[LLMModel]:
if llm_version == LATEST_VERSION_NAME:
- return self._get_llm_model_latest_version(llm_name=llm_name, llm_platform_choice=llm_platform_choice)
+ return self._get_optional_llm_model_latest_version(llm_name=llm_name, llm_platform_choice=llm_platform_choice)
found_llm_models = [llm_model for llm_model in self.root if llm_model.llm_name == llm_name and llm_model.version == llm_version]
if not found_llm_models:
- raise LLMModelProviderError(f"Model '{llm_name}' version '{llm_version}' not found")
+ return None
if len(found_llm_models) > 1:
raise LLMModelProviderError(f"Model '{llm_name}' version '{llm_version}' has multiple instances in the library")
llm_model = found_llm_models[0]
@@ -100,14 +113,14 @@ class LLMModelLibrary(LLMModelProviderAbstract, RootModel[LLMModelLibraryRoot]):
raise LLMModelPlatformError(f"Missing llm id for LLM model {llm_model.name_and_version}'s chosen platform='{llm_platform}'")
return llm_model
- def _get_llm_model_latest_version(
+ def _get_optional_llm_model_latest_version(
self,
llm_name: str,
llm_platform_choice: LLMPlatformChoice,
- ) -> LLMModel:
+ ) -> Optional[LLMModel]:
found_llm_models = [llm_model for llm_model in self.root if llm_model.llm_name == llm_name]
if not found_llm_models:
- raise LLMModelProviderError(f"Could not find model named '{llm_name}'")
+ return None
elif len(found_llm_models) == 1:
# only one so it is the latest we've got
llm_model = found_llm_models[0]
@@ -124,3 +137,16 @@ class LLMModelLibrary(LLMModelProviderAbstract, RootModel[LLMModelLibraryRoot]):
assert llm_platform_choice == DEFAULT_PLATFORM_INDICATOR
llm_model = max(found_llm_models, key=lambda m: m.version)
return llm_model
+
+ def _get_llm_model_latest_version(
+ self,
+ llm_name: str,
+ llm_platform_choice: LLMPlatformChoice,
+ ) -> LLMModel:
+ llm_model_latest_version = self.get_optional_llm_model(
+ llm_name=llm_name, llm_version=LATEST_VERSION_NAME, llm_platform_choice=llm_platform_choice
+ )
+ if llm_model_latest_version is not None:
+ return llm_model_latest_version
+ else:
+ raise LLMModelProviderError(f"Could not find model named '{llm_name}'")
diff --git a/pipelex/cogt/llm/llm_models/llm_model_provider_abstract.py b/pipelex/cogt/llm/llm_models/llm_model_provider_abstract.py
index 33c4d152f..4e0562154 100644
--- a/pipelex/cogt/llm/llm_models/llm_model_provider_abstract.py
+++ b/pipelex/cogt/llm/llm_models/llm_model_provider_abstract.py
@@ -1,6 +1,8 @@
from abc import ABC, abstractmethod
from typing import List
+from mistralai import Optional
+
from pipelex.cogt.llm.llm_models.llm_model import LLMModel
from pipelex.cogt.llm.llm_models.llm_platform import LLMPlatformChoice
@@ -24,6 +26,15 @@ class LLMModelProviderAbstract(ABC):
) -> LLMModel:
pass
+ @abstractmethod
+ def get_optional_llm_model(
+ self,
+ llm_name: str,
+ llm_version: str,
+ llm_platform_choice: LLMPlatformChoice,
+ ) -> Optional[LLMModel]:
+ pass
+
@abstractmethod
def setup(self):
pass
diff --git a/pipelex/cogt/llm/llm_worker_abstract.py b/pipelex/cogt/llm/llm_worker_abstract.py
index 1de792eca..2c4028e38 100644
--- a/pipelex/cogt/llm/llm_worker_abstract.py
+++ b/pipelex/cogt/llm/llm_worker_abstract.py
@@ -1,15 +1,11 @@
from abc import ABC, abstractmethod
from typing import Optional, Type
-from instructor.exceptions import InstructorRetryException
from typing_extensions import override
from pipelex import log
-from pipelex.cogt.exceptions import LLMCapabilityError, LLMCompletionError
from pipelex.cogt.inference.inference_worker_abstract import InferenceWorkerAbstract
from pipelex.cogt.llm.llm_job import LLMJob
-from pipelex.cogt.llm.llm_models.llm_engine import LLMEngine
-from pipelex.cogt.llm.structured_output import StructureMethod
from pipelex.pipeline.job_metadata import UnitJobId
from pipelex.reporting.reporting_protocol import ReportingProtocol
from pipelex.tools.typing.pydantic_utils import BaseModelTypeVar
@@ -18,21 +14,15 @@ from pipelex.tools.typing.pydantic_utils import BaseModelTypeVar
class LLMWorkerAbstract(InferenceWorkerAbstract, ABC):
def __init__(
self,
- llm_engine: LLMEngine,
- structure_method: Optional[StructureMethod],
reporting_delegate: Optional[ReportingProtocol] = None,
):
"""
Initialize the LLMWorker.
Args:
- llm_engine (LLMEngine): The LLM engine to be used by the worker.
- structure_method (Optional[StructureMethod]): The structure method to be used by the worker.
reporting_delegate (Optional[ReportingProtocol]): An optional report delegate for reporting unit jobs.
"""
InferenceWorkerAbstract.__init__(self, reporting_delegate=reporting_delegate)
- self.llm_engine = llm_engine
- self.structure_method = structure_method
#########################################################
# Instance methods
@@ -41,55 +31,52 @@ class LLMWorkerAbstract(InferenceWorkerAbstract, ABC):
@property
@override
def desc(self) -> str:
- return f"LLM Worker using:\n{self.llm_engine.desc}"
+ return "LLM Worker • if you're using an external plugin, override this method to describe your llm worker"
- def _check_can_perform_job(self, llm_job: LLMJob):
- # This can be overridden by subclasses for specific checks
- self._check_vision_support(llm_job=llm_job)
-
- def _check_vision_support(self, llm_job: LLMJob):
- if llm_job.llm_prompt.user_images:
- if not self.llm_engine.llm_model.is_vision_supported:
- raise LLMCapabilityError(f"LLM Engine '{self.llm_engine.tag}' does not support vision.")
-
- nb_images = len(llm_job.llm_prompt.user_images)
- max_prompt_images = self.llm_engine.llm_model.max_prompt_images or 5000
- if nb_images > max_prompt_images:
- raise LLMCapabilityError(f"LLM Engine '{self.llm_engine.tag}' does not accept that many images: {nb_images}.")
+ @property
+ @abstractmethod
+ def is_gen_object_supported(self) -> bool:
+ return False
- async def gen_text(
+ async def _before_job(
self,
llm_job: LLMJob,
- ) -> str:
- log.debug("LLM Worker gen_text")
- log.verbose(f"\n{self.llm_engine.desc}")
- log.verbose(llm_job.params_desc)
-
+ ):
# Verify that the job is valid
llm_job.validate_before_execution()
# Verify feasibility
self._check_can_perform_job(llm_job=llm_job)
- # TODO: Fix printing prompts that contain image bytes
- # log.verbose(llm_job.llm_prompt.desc, title="llm_prompt")
+ async def _after_job(
+ self,
+ llm_job: LLMJob,
+ ):
+ # Report job
+ llm_job.llm_job_after_complete()
+ if self.reporting_delegate:
+ self.reporting_delegate.report_inference_job(inference_job=llm_job)
+
+ def _check_can_perform_job(self, llm_job: LLMJob):
+ # This can be overridden by subclasses for specific checks
+ pass
+
+ async def gen_text(
+ self,
+ llm_job: LLMJob,
+ ) -> str:
+ log.debug("LLM Worker gen_text")
+ log.verbose(llm_job.params_desc)
+ log.verbose(llm_job.llm_prompt.desc, title="llm_prompt")
# metadata
llm_job.job_metadata.unit_job_id = UnitJobId.LLM_GEN_TEXT
- # Prepare job
- llm_job.llm_job_before_start(llm_engine=self.llm_engine)
+ await self._before_job(llm_job=llm_job)
result = await self._gen_text(llm_job=llm_job)
- # Cleanup result (Instructor adds the client's response as a _raw_response attribute, we don't want to pass it along)
- if hasattr(result, "_raw_response"):
- delattr(result, "_raw_response")
-
- # Report job
- llm_job.llm_job_after_complete()
- if self.reporting_delegate:
- self.reporting_delegate.report_inference_job(inference_job=llm_job)
+ await self._after_job(llm_job=llm_job)
return result
@@ -106,44 +93,22 @@ class LLMWorkerAbstract(InferenceWorkerAbstract, ABC):
schema: Type[BaseModelTypeVar],
) -> BaseModelTypeVar:
log.debug("LLM Worker gen_object")
- log.verbose(f"\n{self.llm_engine.desc}")
log.verbose(llm_job.params_desc)
-
- # Verify that the job is valid
- llm_job.validate_before_execution()
-
- # Verify feasibility
- if not self.llm_engine.is_gen_object_supported:
- raise LLMCapabilityError(f"LLM Engine '{self.llm_engine.tag}' does not support object generation.")
- self._check_can_perform_job(llm_job=llm_job)
-
- # TODO: Fix printing prompts that contain image bytes
- # log.verbose(llm_job.llm_prompt.desc, title="llm_prompt")
+ log.verbose(llm_job.llm_prompt.desc, title="llm_prompt")
# metadata
llm_job.job_metadata.unit_job_id = UnitJobId.LLM_GEN_OBJECT
- # Prepare job
- llm_job.llm_job_before_start(llm_engine=self.llm_engine)
+ await self._before_job(llm_job=llm_job)
# Execute job
- try:
- result = await self._gen_object(llm_job=llm_job, schema=schema)
- except InstructorRetryException as exc:
- raise LLMCompletionError(
- f"""Instructor failed to generate object: {schema} after retry with llm '{self.llm_engine.tag}'
- Reason: {exc}
- LLMPrompt: {llm_job.llm_prompt.desc}"""
- ) from exc
+ result = await self._gen_object(llm_job=llm_job, schema=schema)
# Cleanup result
if hasattr(result, "_raw_response"):
delattr(result, "_raw_response")
- # Report job
- llm_job.llm_job_after_complete()
- if self.reporting_delegate:
- self.reporting_delegate.report_inference_job(inference_job=llm_job)
+ await self._after_job(llm_job=llm_job)
return result
diff --git a/pipelex/cogt/llm/llm_worker_factory.py b/pipelex/cogt/llm/llm_worker_factory.py
index 21ccb3edd..55ef8d4a9 100644
--- a/pipelex/cogt/llm/llm_worker_factory.py
+++ b/pipelex/cogt/llm/llm_worker_factory.py
@@ -3,11 +3,11 @@ from typing import Optional
from pipelex.cogt.exceptions import MissingDependencyError
from pipelex.cogt.llm.llm_models.llm_engine import LLMEngine
from pipelex.cogt.llm.llm_models.llm_platform import LLMPlatform
-from pipelex.cogt.llm.llm_worker_abstract import LLMWorkerAbstract
+from pipelex.cogt.llm.llm_worker_internal_abstract import LLMWorkerInternalAbstract
from pipelex.cogt.llm.structured_output import StructureMethod
-from pipelex.cogt.plugin_manager import PluginHandle
from pipelex.config import get_config
from pipelex.hub import get_plugin_manager
+from pipelex.plugins.plugin_sdk_registry import PluginSdkHandle
from pipelex.reporting.reporting_protocol import ReportingProtocol
@@ -16,10 +16,10 @@ class LLMWorkerFactory:
def make_llm_worker(
llm_engine: LLMEngine,
reporting_delegate: Optional[ReportingProtocol] = None,
- ) -> LLMWorkerAbstract:
- llm_sdk_handle = PluginHandle.get_for_llm_platform(llm_platform=llm_engine.llm_platform)
- plugin_manager = get_plugin_manager()
- llm_worker: LLMWorkerAbstract
+ ) -> LLMWorkerInternalAbstract:
+ llm_sdk_handle = PluginSdkHandle.get_for_llm_platform(llm_platform=llm_engine.llm_platform)
+ plugin_sdk_registry = get_plugin_manager().plugin_sdk_registry
+ llm_worker: LLMWorkerInternalAbstract
match llm_engine.llm_platform:
case LLMPlatform.OPENAI | LLMPlatform.AZURE_OPENAI | LLMPlatform.PERPLEXITY | LLMPlatform.XAI:
from pipelex.plugins.openai.openai_factory import OpenAIFactory
@@ -30,7 +30,9 @@ class LLMWorkerFactory:
from pipelex.plugins.openai.openai_llm_worker import OpenAILLMWorker
- llm_sdk_instance = plugin_manager.get_llm_sdk_instance(llm_sdk_handle=llm_sdk_handle) or plugin_manager.set_llm_sdk_instance(
+ llm_sdk_instance = plugin_sdk_registry.get_llm_sdk_instance(
+ llm_sdk_handle=llm_sdk_handle
+ ) or plugin_sdk_registry.set_llm_sdk_instance(
llm_sdk_handle=llm_sdk_handle,
llm_sdk_instance=OpenAIFactory.make_openai_client(llm_platform=llm_engine.llm_platform),
)
@@ -50,7 +52,9 @@ class LLMWorkerFactory:
from pipelex.plugins.openai.openai_factory import OpenAIFactory
from pipelex.plugins.openai.openai_llm_worker import OpenAILLMWorker
- llm_sdk_instance = plugin_manager.get_llm_sdk_instance(llm_sdk_handle=llm_sdk_handle) or plugin_manager.set_llm_sdk_instance(
+ llm_sdk_instance = plugin_sdk_registry.get_llm_sdk_instance(
+ llm_sdk_handle=llm_sdk_handle
+ ) or plugin_sdk_registry.set_llm_sdk_instance(
llm_sdk_handle=llm_sdk_handle,
llm_sdk_instance=OpenAIFactory.make_openai_client(llm_platform=llm_engine.llm_platform),
)
@@ -65,7 +69,9 @@ class LLMWorkerFactory:
from pipelex.plugins.openai.openai_factory import OpenAIFactory
from pipelex.plugins.openai.openai_llm_worker import OpenAILLMWorker
- llm_sdk_instance = plugin_manager.get_llm_sdk_instance(llm_sdk_handle=llm_sdk_handle) or plugin_manager.set_llm_sdk_instance(
+ llm_sdk_instance = plugin_sdk_registry.get_llm_sdk_instance(
+ llm_sdk_handle=llm_sdk_handle
+ ) or plugin_sdk_registry.set_llm_sdk_instance(
llm_sdk_handle=llm_sdk_handle,
llm_sdk_instance=OpenAIFactory.make_openai_client(llm_platform=llm_engine.llm_platform),
)
@@ -93,7 +99,9 @@ class LLMWorkerFactory:
from pipelex.plugins.anthropic.anthropic_factory import AnthropicFactory
from pipelex.plugins.anthropic.anthropic_llm_worker import AnthropicLLMWorker
- llm_sdk_instance = plugin_manager.get_llm_sdk_instance(llm_sdk_handle=llm_sdk_handle) or plugin_manager.set_llm_sdk_instance(
+ llm_sdk_instance = plugin_sdk_registry.get_llm_sdk_instance(
+ llm_sdk_handle=llm_sdk_handle
+ ) or plugin_sdk_registry.set_llm_sdk_instance(
llm_sdk_handle=llm_sdk_handle,
llm_sdk_instance=AnthropicFactory.make_anthropic_client(llm_platform=llm_engine.llm_platform),
)
@@ -121,7 +129,9 @@ class LLMWorkerFactory:
from pipelex.plugins.mistral.mistral_factory import MistralFactory
from pipelex.plugins.mistral.mistral_llm_worker import MistralLLMWorker
- llm_sdk_instance = plugin_manager.get_llm_sdk_instance(llm_sdk_handle=llm_sdk_handle) or plugin_manager.set_llm_sdk_instance(
+ llm_sdk_instance = plugin_sdk_registry.get_llm_sdk_instance(
+ llm_sdk_handle=llm_sdk_handle
+ ) or plugin_sdk_registry.set_llm_sdk_instance(
llm_sdk_handle=llm_sdk_handle,
llm_sdk_instance=MistralFactory.make_mistral_client(),
)
@@ -144,7 +154,9 @@ class LLMWorkerFactory:
from pipelex.plugins.bedrock.bedrock_factory import BedrockFactory
from pipelex.plugins.bedrock.bedrock_llm_worker import BedrockLLMWorker
- llm_sdk_instance = plugin_manager.get_llm_sdk_instance(llm_sdk_handle=llm_sdk_handle) or plugin_manager.set_llm_sdk_instance(
+ llm_sdk_instance = plugin_sdk_registry.get_llm_sdk_instance(
+ llm_sdk_handle=llm_sdk_handle
+ ) or plugin_sdk_registry.set_llm_sdk_instance(
llm_sdk_handle=llm_sdk_handle,
llm_sdk_instance=BedrockFactory.make_bedrock_client(),
)
diff --git a/pipelex/cogt/llm/llm_worker_internal_abstract.py b/pipelex/cogt/llm/llm_worker_internal_abstract.py
new file mode 100644
index 000000000..afc435c1d
--- /dev/null
+++ b/pipelex/cogt/llm/llm_worker_internal_abstract.py
@@ -0,0 +1,67 @@
+from typing import Optional
+
+from typing_extensions import override
+
+from pipelex.cogt.exceptions import LLMCapabilityError
+from pipelex.cogt.llm.llm_job import LLMJob
+from pipelex.cogt.llm.llm_models.llm_engine import LLMEngine
+from pipelex.cogt.llm.llm_worker_abstract import LLMWorkerAbstract
+from pipelex.cogt.llm.structured_output import StructureMethod
+from pipelex.reporting.reporting_protocol import ReportingProtocol
+
+
+class LLMWorkerInternalAbstract(LLMWorkerAbstract):
+ def __init__(
+ self,
+ llm_engine: LLMEngine,
+ structure_method: Optional[StructureMethod] = None,
+ reporting_delegate: Optional[ReportingProtocol] = None,
+ ):
+ """
+ Initialize the LLMWorker.
+
+ Args:
+ llm_engine (LLMEngine): The LLM engine to be used by the worker.
+ structure_method (Optional[StructureMethod]): The structure method to be used by the worker.
+ reporting_delegate (Optional[ReportingProtocol]): An optional report delegate for reporting unit jobs.
+ """
+ LLMWorkerAbstract.__init__(self, reporting_delegate=reporting_delegate)
+ self.llm_engine = llm_engine
+ self.structure_method = structure_method
+
+ #########################################################
+ # Instance methods
+ #########################################################
+
+ @property
+ @override
+ def desc(self) -> str:
+ return f"LLM-Worker:{self.llm_engine.tag}"
+
+ @property
+ @override
+ def is_gen_object_supported(self) -> bool:
+ return self.llm_engine.is_gen_object_supported
+
+ @override
+ async def _before_job(
+ self,
+ llm_job: LLMJob,
+ ):
+ await super()._before_job(llm_job=llm_job)
+ llm_job.llm_job_before_start(llm_engine=self.llm_engine)
+
+ @override
+ def _check_can_perform_job(self, llm_job: LLMJob):
+ # This can be overridden by subclasses for specific checks
+ self._check_vision_support(llm_job=llm_job)
+
+ def _check_vision_support(self, llm_job: LLMJob):
+ if llm_job.llm_prompt.user_images:
+ if not self.llm_engine.llm_model.is_vision_supported:
+ raise LLMCapabilityError(f"LLM Engine '{self.llm_engine.tag}' does not support vision.")
+
+ nb_images = len(llm_job.llm_prompt.user_images)
+ max_prompt_images = self.llm_engine.llm_model.max_prompt_images or 5000
+ if nb_images > max_prompt_images:
+ raise LLMCapabilityError(f"LLM Engine '{self.llm_engine.tag}' does not accept that many images: {nb_images}.")
diff --git a/pipelex/cogt/ocr/ocr_worker_factory.py b/pipelex/cogt/ocr/ocr_worker_factory.py
index d004c3d35..4c96a1602 100644
--- a/pipelex/cogt/ocr/ocr_worker_factory.py
+++ b/pipelex/cogt/ocr/ocr_worker_factory.py
@@ -4,8 +4,8 @@ from pipelex.cogt.exceptions import MissingDependencyError
from pipelex.cogt.ocr.ocr_engine import OcrEngine
from pipelex.cogt.ocr.ocr_platform import OcrPlatform
from pipelex.cogt.ocr.ocr_worker_abstract import OcrWorkerAbstract
-from pipelex.cogt.plugin_manager import PluginHandle
from pipelex.hub import get_plugin_manager
+from pipelex.plugins.plugin_sdk_registry import PluginSdkHandle
from pipelex.reporting.reporting_protocol import ReportingProtocol
@@ -15,8 +15,8 @@ class OcrWorkerFactory:
ocr_engine: OcrEngine,
reporting_delegate: Optional[ReportingProtocol] = None,
) -> OcrWorkerAbstract:
- ocr_sdk_handle = PluginHandle.get_for_ocr_engine(ocr_platform=ocr_engine.ocr_platform)
- plugin_manager = get_plugin_manager()
+ ocr_sdk_handle = PluginSdkHandle.get_for_ocr_engine(ocr_platform=ocr_engine.ocr_platform)
+ plugin_sdk_registry = get_plugin_manager().plugin_sdk_registry
ocr_worker: OcrWorkerAbstract
match ocr_engine.ocr_platform:
case OcrPlatform.MISTRAL:
@@ -32,7 +32,9 @@ class OcrWorkerFactory:
from pipelex.plugins.mistral.mistral_factory import MistralFactory
from pipelex.plugins.mistral.mistral_ocr_worker import MistralOcrWorker
- ocr_sdk_instance = plugin_manager.get_ocr_sdk_instance(ocr_sdk_handle=ocr_sdk_handle) or plugin_manager.set_ocr_sdk_instance(
+ ocr_sdk_instance = plugin_sdk_registry.get_ocr_sdk_instance(
+ ocr_sdk_handle=ocr_sdk_handle
+ ) or plugin_sdk_registry.set_ocr_sdk_instance(
ocr_sdk_handle=ocr_sdk_handle,
ocr_sdk_instance=MistralFactory.make_mistral_client(),
)
diff --git a/pipelex/config.py b/pipelex/config.py
index 87005e265..5a50fc298 100644
--- a/pipelex/config.py
+++ b/pipelex/config.py
@@ -9,7 +9,6 @@ from pipelex.exceptions import PipelexConfigError, StaticValidationErrorType
from pipelex.hub import get_required_config
from pipelex.libraries.library_config import LibraryConfig
from pipelex.pipeline.track.tracker_config import TrackerConfig
-from pipelex.plugins.plugins_config import PluginsConfig
from pipelex.tools.aws.aws_config import AwsConfig
from pipelex.tools.config.models import ConfigModel, ConfigRoot
from pipelex.tools.log.log_config import LogConfig
@@ -114,7 +113,6 @@ class Pipelex(ConfigModel):
class PipelexConfig(ConfigRoot):
session_id: str = shortuuid.uuid()
cogt: Cogt
- plugins: PluginsConfig
pipelex: Pipelex
diff --git a/pipelex/core/pipe_input_details.py b/pipelex/core/pipe_input_details.py
index 94e0ccd4b..6e5bc96a0 100644
--- a/pipelex/core/pipe_input_details.py
+++ b/pipelex/core/pipe_input_details.py
@@ -23,13 +23,13 @@ class PipeInputDetails(RootModel[PipeInputDetailsRoot]):
# in case of sub-attribute, the variable name is the object name, before the 1st dot
transformed_key: str = required_input.split(".", 1)[0]
if transformed_key != required_input:
- log.warning(f"Sub-attribute {required_input} detected, using {transformed_key} as variable name")
+ log.verbose(f"Sub-attribute {required_input} detected, using {transformed_key} as variable name")
# Validate concept_code
concept_code = ConceptCodeFactory.make_concept_code_from_str(concept_str=concept_str)
if transformed_key in transformed_dict and transformed_dict[transformed_key] != concept_code:
- log.warning(
+ log.verbose(
f"Variable {transformed_key} already exists with a different concept code: {transformed_dict[transformed_key]} -> {concept_str}"
)
transformed_dict[transformed_key] = concept_code
diff --git a/pipelex/core/pipe_input_spec.py b/pipelex/core/pipe_input_spec.py
index 00466df5b..faab24c88 100644
--- a/pipelex/core/pipe_input_spec.py
+++ b/pipelex/core/pipe_input_spec.py
@@ -29,13 +29,13 @@ class PipeInputSpec(RootModel[PipeInputSpecRoot]):
# in case of sub-attribute, the variable name is the object name, before the 1st dot
transformed_key: str = required_input.split(".", 1)[0]
if transformed_key != required_input:
- log.warning(f"Sub-attribute {required_input} detected, using {transformed_key} as variable name")
+ log.verbose(f"Sub-attribute {required_input} detected, using {transformed_key} as variable name")
# Validate concept_code
concept_code = ConceptCodeFactory.make_concept_code_from_str(concept_str=concept_str)
if transformed_key in transformed_dict and transformed_dict[transformed_key] != concept_code:
- log.warning(
+ log.verbose(
f"Variable {transformed_key} already exists with a different concept code: {transformed_dict[transformed_key]} -> {concept_str}"
)
transformed_dict[transformed_key] = concept_code
diff --git a/pipelex/hub.py b/pipelex/hub.py
index 780c19f23..552a38211 100644
--- a/pipelex/hub.py
+++ b/pipelex/hub.py
@@ -10,13 +10,11 @@ from pipelex.cogt.content_generation.content_generator_protocol import (
from pipelex.cogt.imgg.imgg_worker_abstract import ImggWorkerAbstract
from pipelex.cogt.inference.inference_manager_protocol import InferenceManagerProtocol
from pipelex.cogt.llm.llm_models.llm_deck_abstract import LLMDeckAbstract
-from pipelex.cogt.llm.llm_models.llm_engine_blueprint import LLMEngineBlueprint
from pipelex.cogt.llm.llm_models.llm_model_provider_abstract import (
LLMModelProviderAbstract,
)
from pipelex.cogt.llm.llm_worker_abstract import LLMWorkerAbstract
from pipelex.cogt.ocr.ocr_worker_abstract import OcrWorkerAbstract
-from pipelex.cogt.plugin_manager import PluginManager
from pipelex.core.concept import Concept
from pipelex.core.concept_provider_abstract import ConceptProviderAbstract
from pipelex.core.domain import Domain
@@ -28,6 +26,7 @@ from pipelex.pipeline.activity.activity_manager_protocol import ActivityManagerP
from pipelex.pipeline.pipeline import Pipeline
from pipelex.pipeline.pipeline_manager_abstract import PipelineManagerAbstract
from pipelex.pipeline.track.pipeline_tracker_protocol import PipelineTrackerProtocol
+from pipelex.plugins.plugin_manager import PluginManager
from pipelex.reporting.reporting_protocol import ReportingProtocol
from pipelex.tools.config.manager import config_manager
from pipelex.tools.config.models import ConfigRoot
@@ -229,7 +228,7 @@ class PipelexHub:
def get_plugin_manager(self) -> PluginManager:
if self._plugin_manager is None:
- raise RuntimeError("SdkManager is not initialized")
+ raise RuntimeError("PluginManager2 is not initialized")
return self._plugin_manager
def get_inference_manager(self) -> InferenceManagerProtocol:
@@ -348,12 +347,8 @@ def get_inference_manager() -> InferenceManagerProtocol:
def get_llm_worker(
llm_handle: str,
- specific_llm_engine_blueprint: Optional[LLMEngineBlueprint] = None,
) -> LLMWorkerAbstract:
- return get_inference_manager().get_llm_worker(
- llm_handle=llm_handle,
- specific_llm_engine_blueprint=specific_llm_engine_blueprint,
- )
+ return get_inference_manager().get_llm_worker(llm_handle=llm_handle)
def get_imgg_worker(
diff --git a/pipelex/libraries/library_config.py b/pipelex/libraries/library_config.py
index 03736eaa2..e8b201e13 100644
--- a/pipelex/libraries/library_config.py
+++ b/pipelex/libraries/library_config.py
@@ -20,6 +20,8 @@ class LibraryConfig(ConfigModel):
exported_llm_deck_path: ClassVar[str] = f"{exported_library_root}/llm_deck"
internal_templates_path: ClassVar[str] = f"{internal_library_root}/templates"
exported_templates_path: ClassVar[str] = f"{exported_library_root}/templates"
+ internal_plugins_path: ClassVar[str] = f"{internal_library_root}/plugins"
+ exported_plugins_path: ClassVar[str] = f"{exported_library_root}/plugins"
@classmethod
def get_llm_deck_paths(cls) -> List[str]:
@@ -31,6 +33,10 @@ class LibraryConfig(ConfigModel):
def get_templates_paths(cls) -> List[str]:
return [str(path) for path in find_files_in_dir(dir_path=cls.exported_templates_path, pattern="*.toml", is_recursive=True)]
+ @classmethod
+ def get_plugin_config_path(cls) -> str:
+ return f"{cls.exported_plugins_path}/plugin_config.toml"
+
@classmethod
def export_libraries(cls, overwrite: bool = False) -> None:
"""Duplicate pipelex libraries files in the client project, preserving directory structure."""
@@ -84,3 +90,11 @@ class LibraryConfig(ConfigModel):
target_dir=cls.exported_templates_path,
overwrite=overwrite,
)
+
+ # plugins
+ copy_folder_from_package(
+ package_name=cls.package_name,
+ folder_path_in_package=cls.internal_plugins_path,
+ target_dir=cls.exported_plugins_path,
+ overwrite=overwrite,
+ )
diff --git a/pipelex/libraries/llm_deck/base_llm_deck.toml b/pipelex/libraries/llm_deck/base_llm_deck.toml
index 205f962f7..f64fbba24 100644
--- a/pipelex/libraries/llm_deck/base_llm_deck.toml
+++ b/pipelex/libraries/llm_deck/base_llm_deck.toml
@@ -1,10 +1,14 @@
+# LLM Deck base
####################################################################################################
-# LLM Deck base
+# LLM Handles
####################################################################################################
-# Match a llm_handle with a complete blueprint (llm_name, llm_version and llm_platform_choice).
+llm_external_handles = []
+
+# Match a llm_handle with either just and llm_name
+# or a complete blueprint including llm_version (defaulting to "latest") and llm_platform_choice (defaulting to "default").
[llm_handles]
gpt-4o-2024-08-06 = { llm_name = "gpt-4o", llm_version = "2024-08-06" }
best-claude = "claude-4-opus"
@@ -12,6 +16,7 @@ best-gemini = "gemini-2.5-pro"
best-mistral = "mistral-large"
best-grok = "grok-3"
+
####################################################################################################
# LLM Presets
####################################################################################################
diff --git a/pipelex/libraries/plugins/plugin_config.toml b/pipelex/libraries/plugins/plugin_config.toml
new file mode 100644
index 000000000..e3c86138b
--- /dev/null
+++ b/pipelex/libraries/plugins/plugin_config.toml
@@ -0,0 +1,39 @@
+
+####################################################################################################
+# Plugins config
+####################################################################################################
+
+[openai_config]
+image_output_compression = 100
+api_key_method = "env"
+
+[azure_openai_config]
+api_key_method = "env"
+
+# TODO: handle multiple azure openai accounts with different resource groups and account names for various llm model
+
+[perplexity_config]
+api_key_method = "env"
+
+[xai_config]
+api_key_method = "env"
+
+[vertexai_config]
+api_key_method = "env"
+
+[mistral_config]
+api_key_method = "env"
+
+[bedrock_config]
+client_method = "aioboto3"
+
+[anthropic_config]
+claude_4_reduced_tokens_limit = 8192 # use "unlimited" to enable the full 32/64K tokens Opus/Sonet but it raises streaming/timeout issues
+api_key_method = "env"
+
+[custom_endpoint_config]
+api_key_method = "env"
+
+[fal_config]
+flux_map_quality_to_steps = { "low" = 14, "medium" = 28, "high" = 56 }
+sdxl_lightning_map_quality_to_steps = { "low" = 2, "medium" = 4, "high" = 8 }
diff --git a/pipelex/pipe_operators/pipe_llm.py b/pipelex/pipe_operators/pipe_llm.py
index 4879e0ccc..228729a19 100644
--- a/pipelex/pipe_operators/pipe_llm.py
+++ b/pipelex/pipe_operators/pipe_llm.py
@@ -265,9 +265,9 @@ class PipeLLM(PipeOperator):
else:
log.verbose(f"{self.class_name} generate a single '{output_concept_code}' (class '{output_concept.structure_class_name}')")
- if not self.pipe_llm_prompt.prompting_style:
- llm_deck = get_llm_deck()
- llm_model = llm_deck.find_llm_model(llm_handle=self.llm_setting_main.llm_handle)
+ if not self.pipe_llm_prompt.prompting_style and (
+ llm_model := get_llm_deck().find_optional_llm_model(llm_handle=self.llm_setting_main.llm_handle)
+ ):
llm_family = llm_model.llm_family
if self.llm_setting_main.prompting_target:
log.dev(f"prompting_target for '{self.llm_setting_main.llm_handle}' from setting: {self.llm_setting_main}")
diff --git a/pipelex/pipelex.py b/pipelex/pipelex.py
index a6aff6f10..569fcd1bc 100644
--- a/pipelex/pipelex.py
+++ b/pipelex/pipelex.py
@@ -17,7 +17,6 @@ from pipelex.cogt.content_generation.content_generator_protocol import (
from pipelex.cogt.inference.inference_manager import InferenceManager
from pipelex.cogt.llm.llm_models.llm_model import LATEST_VERSION_NAME
from pipelex.cogt.llm.llm_models.llm_model_library import LLMModelLibrary
-from pipelex.cogt.plugin_manager import PluginManager
from pipelex.config import PipelexConfig, get_config
from pipelex.core.registry_models import PipelexRegistryModels
from pipelex.exceptions import PipelexConfigError, PipelexSetupError
@@ -36,6 +35,7 @@ from pipelex.pipeline.track.pipeline_tracker_protocol import (
PipelineTrackerNoOp,
PipelineTrackerProtocol,
)
+from pipelex.plugins.plugin_manager import PluginManager
from pipelex.reporting.reporting_manager import ReportingManager
from pipelex.reporting.reporting_protocol import ReportingNoOp, ReportingProtocol
from pipelex.test_extras.registry_test_models import PipelexTestModels
@@ -64,7 +64,6 @@ class Pipelex:
class_registry: Optional[ClassRegistryAbstract] = None,
template_provider: Optional[TemplateLibrary] = None,
llm_model_provider: Optional[LLMModelLibrary] = None,
- plugin_manager: Optional[PluginManager] = None,
inference_manager: Optional[InferenceManager] = None,
pipeline_manager: Optional[PipelineManager] = None,
pipeline_tracker: Optional[PipelineTracker] = None,
@@ -92,7 +91,6 @@ class Pipelex:
class_registry: Optional[ClassRegistryAbstract] = None,
template_provider: Optional[TemplateLibrary] = None,
llm_model_provider: Optional[LLMModelLibrary] = None,
- plugin_manager: Optional[PluginManager] = None,
inference_manager: Optional[InferenceManager] = None,
pipeline_manager: Optional[PipelineManager] = None,
pipeline_tracker: Optional[PipelineTracker] = None,
@@ -131,10 +129,10 @@ class Pipelex:
self.kajson_manager = KajsonManager(class_registry=self.class_registry)
# cogt
+ self.plugin_manager = PluginManager()
+ self.pipelex_hub.set_plugin_manager(self.plugin_manager)
self.llm_model_provider = llm_model_provider or LLMModelLibrary()
self.pipelex_hub.set_llm_models_provider(self.llm_model_provider)
- self.plugin_manager = plugin_manager or PluginManager()
- self.pipelex_hub.set_plugin_manager(self.plugin_manager)
self.inference_manager = inference_manager or InferenceManager()
self.pipelex_hub.set_inference_manager(self.inference_manager)
@@ -187,6 +185,7 @@ class Pipelex:
self.pipelex_hub.set_secrets_provider(secrets_provider or EnvSecretsProvider())
self.pipelex_hub.set_storage_provider(storage_provider)
# cogt
+ self.plugin_manager.setup()
self.pipelex_hub.set_content_generator(content_generator or ContentGenerator())
self.reporting_delegate.setup()
self.class_registry.register_classes(PipelexRegistryModels.get_all_models())
@@ -213,9 +212,8 @@ class Pipelex:
llm_deck = self.library_manager.load_deck()
for llm_model in self.llm_model_provider.get_all_llm_models():
if llm_model.version == LATEST_VERSION_NAME:
- llm_deck.add_llm_handle_to_llm_engine_blueprint(
- llm_handle=llm_model.llm_name,
- llm_engine_default=llm_model.llm_name,
+ llm_deck.add_llm_name_as_handle_with_defaults(
+ llm_name=llm_model.llm_name,
)
llm_deck.validate_llm_presets()
self.library_manager.load_libraries()
@@ -241,6 +239,7 @@ class Pipelex:
self.inference_manager.teardown()
self.reporting_delegate.teardown()
self.llm_model_provider.teardown()
+ self.plugin_manager.teardown()
# tools
self.kajson_manager.teardown()
@@ -262,6 +261,10 @@ class Pipelex:
log.info(f"Pipelex {PACKAGE_VERSION} initialized.")
return pipelex_instance
+ @classmethod
+ def get_optional_instance(cls) -> Optional[Self]:
+ return cls._pipelex_instance
+
@classmethod
def get_instance(cls) -> Self:
if cls._pipelex_instance is None:
diff --git a/pipelex/pipelex.toml b/pipelex/pipelex.toml
index 978485b4f..d05c1f703 100644
--- a/pipelex/pipelex.toml
+++ b/pipelex/pipelex.toml
@@ -128,47 +128,6 @@ ocr_handles = ["mistral/mistral-ocr-latest"]
page_output_text_file_name = "page_text.md"
default_page_views_dpi = 72
-####################################################################################################
-# Plugins config
-####################################################################################################
-
-[plugins]
-
-[plugins.openai_config]
-image_output_compression = 100
-api_key_method = "env"
-
-[plugins.azure_openai_config]
-api_key_method = "env"
-
-# TODO: handle multiple azure openai accounts with different resource groups and account names for various llm model
-
-[plugins.perplexity_config]
-api_key_method = "env"
-
-[plugins.xai_config]
-api_key_method = "env"
-
-[plugins.vertexai_config]
-api_key_method = "env"
-
-[plugins.mistral_config]
-api_key_method = "env"
-
-[plugins.bedrock_config]
-client_method = "aioboto3"
-
-[plugins.anthropic_config]
-claude_4_reduced_tokens_limit = 8192 # use "unlimited" to enable the full 32/64K tokens Opus/Sonet but it raises streaming/timeout issues
-api_key_method = "env"
-
-[plugins.custom_endpoint_config]
-api_key_method = "env"
-
-[plugins.fal_config]
-flux_map_quality_to_steps = { "low" = 14, "medium" = 28, "high" = 56 }
-sdxl_lightning_map_quality_to_steps = { "low" = 2, "medium" = 4, "high" = 8 }
-
####################################################################################################
# Pipelex prompting config
####################################################################################################
@@ -250,4 +209,3 @@ image_urls = [
"https://storage.googleapis.com/public_test_files_7fa6_4277_9ab/fashion/fashion_photo_1.jpg",
"https://storage.googleapis.com/public_test_files_7fa6_4277_9ab/fashion/fashion_photo_2.png",
]
-
diff --git a/pipelex/pipelex_template.toml b/pipelex/pipelex_template.toml
index cc535db55..287a71269 100644
--- a/pipelex/pipelex_template.toml
+++ b/pipelex/pipelex_template.toml
@@ -1,5 +1,5 @@
[pipelex.aws_config]
-api_key_method = "env"
+api_key_method = "env"
# The possible values are "env" and "secret_provider".
# "env" means means that the env var are stored in your .env file.
# "secret_provider" means that the env var are stored in your Secret Manager (See the doc for injecting a secret provider).
@@ -9,39 +9,6 @@ api_key_method = "env"
# This overrrides the defaults set for any llm handle
"gpt-4o-mini" = "openai"
-[plugins]
-
-[plugins.openai_config]
-image_output_compression = 100
-api_key_method = "env"
-
-[plugins.azure_openai_config]
-api_key_method = "env"
-
-# TODO: handle multiple azure openai accounts with different resource groups and account names for various llm model
-
-[plugins.perplexity_config]
-api_key_method = "env"
-
-[plugins.xai_config]
-api_key_method = "env"
-
-[plugins.vertexai_config]
-api_key_method = "env"
-
-[plugins.mistral_config]
-api_key_method = "env"
-
-[plugins.bedrock_config]
-client_method = "aioboto3"
-
-[plugins.anthropic_config]
-claude_4_reduced_tokens_limit = 8192 # use "unlimited" to enable the full 32/64K tokens Opus/Sonet but it raises streaming/timeout issues
-api_key_method = "env"
-
-[plugins.custom_endpoint_config]
-api_key_method = "env"
-
####################################################################################################
# OCR config
####################################################################################################
diff --git a/pipelex/plugins/anthropic/anthropic_factory.py b/pipelex/plugins/anthropic/anthropic_factory.py
index 4c318c7f5..6e44689dd 100644
--- a/pipelex/plugins/anthropic/anthropic_factory.py
+++ b/pipelex/plugins/anthropic/anthropic_factory.py
@@ -25,7 +25,7 @@ from pipelex.cogt.llm.llm_job import LLMJob
from pipelex.cogt.llm.llm_models.llm_platform import LLMPlatform
from pipelex.cogt.llm.token_category import NbTokensByCategoryDict, TokenCategory
from pipelex.config import get_config
-from pipelex.hub import get_secrets_provider
+from pipelex.hub import get_plugin_manager, get_secrets_provider
from pipelex.tools.misc.base_64_utils import (
load_binary_as_base64_async,
)
@@ -43,7 +43,7 @@ class AnthropicFactory:
# TODO: also support Anthropic with VertexAI
match llm_platform:
case LLMPlatform.ANTHROPIC:
- anthropic_config = get_config().plugins.anthropic_config
+ anthropic_config = get_plugin_manager().plugin_configs.anthropic_config
api_key = anthropic_config.get_api_key(secrets_provider=get_secrets_provider())
return AsyncAnthropic(api_key=api_key)
case LLMPlatform.BEDROCK_ANTHROPIC:
diff --git a/pipelex/plugins/anthropic/anthropic_llm_worker.py b/pipelex/plugins/anthropic/anthropic_llm_worker.py
index 85aa8b63a..97aa88dd1 100644
--- a/pipelex/plugins/anthropic/anthropic_llm_worker.py
+++ b/pipelex/plugins/anthropic/anthropic_llm_worker.py
@@ -9,15 +9,15 @@ from pipelex.cogt.exceptions import LLMCompletionError, LLMEngineParameterError,
from pipelex.cogt.llm.llm_job import LLMJob
from pipelex.cogt.llm.llm_models.llm_engine import LLMEngine
from pipelex.cogt.llm.llm_models.llm_platform import LLMPlatform
-from pipelex.cogt.llm.llm_worker_abstract import LLMWorkerAbstract
+from pipelex.cogt.llm.llm_worker_internal_abstract import LLMWorkerInternalAbstract
from pipelex.cogt.llm.structured_output import StructureMethod
-from pipelex.config import get_config
+from pipelex.hub import get_plugin_manager
from pipelex.plugins.anthropic.anthropic_factory import AnthropicFactory
from pipelex.reporting.reporting_protocol import ReportingProtocol
from pipelex.tools.typing.pydantic_utils import BaseModelTypeVar
-class AnthropicLLMWorker(LLMWorkerAbstract):
+class AnthropicLLMWorker(LLMWorkerInternalAbstract):
def __init__(
self,
sdk_instance: Any,
@@ -25,7 +25,7 @@ class AnthropicLLMWorker(LLMWorkerAbstract):
structure_method: Optional[StructureMethod] = None,
reporting_delegate: Optional[ReportingProtocol] = None,
):
- LLMWorkerAbstract.__init__(
+ LLMWorkerInternalAbstract.__init__(
self,
llm_engine=llm_engine,
structure_method=structure_method,
@@ -63,7 +63,7 @@ class AnthropicLLMWorker(LLMWorkerAbstract):
# TODO: implement streaming behind the scenes to avoid timeout/streaming errors with Claude 4 and high tokens
def _adapt_max_tokens(self, max_tokens: Optional[int]) -> int:
max_tokens = max_tokens or self.default_max_tokens
- if claude_4_tokens_limit := get_config().plugins.anthropic_config.claude_4_tokens_limit:
+ if claude_4_tokens_limit := get_plugin_manager().plugin_configs.anthropic_config.claude_4_tokens_limit:
if max_tokens > claude_4_tokens_limit:
max_tokens = claude_4_tokens_limit
log.warning(f"Max tokens is greater than the claude 4 reduced tokens limit, reducing to {max_tokens}")
diff --git a/pipelex/plugins/bedrock/bedrock_factory.py b/pipelex/plugins/bedrock/bedrock_factory.py
index b12dca2bd..513a2c458 100644
--- a/pipelex/plugins/bedrock/bedrock_factory.py
+++ b/pipelex/plugins/bedrock/bedrock_factory.py
@@ -2,8 +2,7 @@ from pipelex import log
from pipelex.cogt.exceptions import LLMCapabilityError, PromptImageFormatError
from pipelex.cogt.image.prompt_image import PromptImageBytes
from pipelex.cogt.llm.llm_job import LLMJob
-from pipelex.config import get_config
-from pipelex.hub import get_secrets_provider
+from pipelex.hub import get_plugin_manager, get_secrets_provider
from pipelex.plugins.bedrock.bedrock_client_protocol import BedrockClientProtocol
from pipelex.plugins.bedrock.bedrock_config import BedrockClientMethod
from pipelex.plugins.bedrock.bedrock_message import BedrockContentItem, BedrockImage, BedrockMessage, BedrockSource, ImageFormat
@@ -16,7 +15,7 @@ class BedrockFactory:
@classmethod
def make_bedrock_client(cls) -> BedrockClientProtocol:
- bedrock_config = get_config().plugins.bedrock_config
+ bedrock_config = get_plugin_manager().plugin_configs.bedrock_config
aws_region = bedrock_config.configure(secrets_provider=get_secrets_provider())
client_method = bedrock_config.client_method
bedrock_async_client: BedrockClientProtocol
diff --git a/pipelex/plugins/bedrock/bedrock_llm_worker.py b/pipelex/plugins/bedrock/bedrock_llm_worker.py
index 95f4ec264..87e479573 100644
--- a/pipelex/plugins/bedrock/bedrock_llm_worker.py
+++ b/pipelex/plugins/bedrock/bedrock_llm_worker.py
@@ -6,7 +6,7 @@ from pipelex import log
from pipelex.cogt.exceptions import LLMCapabilityError, LLMEngineParameterError, SdkTypeError
from pipelex.cogt.llm.llm_job import LLMJob
from pipelex.cogt.llm.llm_models.llm_engine import LLMEngine
-from pipelex.cogt.llm.llm_worker_abstract import LLMWorkerAbstract
+from pipelex.cogt.llm.llm_worker_internal_abstract import LLMWorkerInternalAbstract
from pipelex.cogt.llm.structured_output import StructureMethod
from pipelex.plugins.bedrock.bedrock_client_protocol import BedrockClientProtocol
from pipelex.plugins.bedrock.bedrock_factory import BedrockFactory
@@ -14,7 +14,7 @@ from pipelex.reporting.reporting_protocol import ReportingProtocol
from pipelex.tools.typing.pydantic_utils import BaseModelTypeVar
-class BedrockLLMWorker(LLMWorkerAbstract):
+class BedrockLLMWorker(LLMWorkerInternalAbstract):
def __init__(
self,
sdk_instance: Any,
@@ -22,7 +22,7 @@ class BedrockLLMWorker(LLMWorkerAbstract):
structure_method: Optional[StructureMethod] = None,
reporting_delegate: Optional[ReportingProtocol] = None,
):
- LLMWorkerAbstract.__init__(
+ LLMWorkerInternalAbstract.__init__(
self,
llm_engine=llm_engine,
structure_method=structure_method,
diff --git a/pipelex/plugins/fal/fal_factory.py b/pipelex/plugins/fal/fal_factory.py
index b4a2f08b3..88d47dc11 100644
--- a/pipelex/plugins/fal/fal_factory.py
+++ b/pipelex/plugins/fal/fal_factory.py
@@ -7,7 +7,7 @@ from pipelex.cogt.imgg.imgg_engine import ImggEngine
from pipelex.cogt.imgg.imgg_handle import ImggHandle
from pipelex.cogt.imgg.imgg_job import ImggJob
from pipelex.cogt.imgg.imgg_job_components import AspectRatio, OutputFormat, Quality
-from pipelex.config import get_config
+from pipelex.hub import get_plugin_manager
class FalFactory:
@@ -17,12 +17,12 @@ class FalFactory:
@classmethod
def make_nb_steps_from_quality_for_flux_pro(cls, quality: Quality) -> int:
- flux_map_quality_to_steps = get_config().plugins.fal_config.flux_map_quality_to_steps
+ flux_map_quality_to_steps = get_plugin_manager().plugin_configs.fal_config.flux_map_quality_to_steps
return flux_map_quality_to_steps[quality.value]
@classmethod
def make_nb_steps_from_quality_for_sdxl_lightning(cls, quality: Quality) -> int:
- sdxl_lightning_map_quality_to_steps = get_config().plugins.fal_config.sdxl_lightning_map_quality_to_steps
+ sdxl_lightning_map_quality_to_steps = get_plugin_manager().plugin_configs.fal_config.sdxl_lightning_map_quality_to_steps
return sdxl_lightning_map_quality_to_steps[quality.value]
@classmethod
diff --git a/pipelex/plugins/mistral/mistral_factory.py b/pipelex/plugins/mistral/mistral_factory.py
index 9115bb15e..2d00aa6b7 100644
--- a/pipelex/plugins/mistral/mistral_factory.py
+++ b/pipelex/plugins/mistral/mistral_factory.py
@@ -24,8 +24,7 @@ from pipelex.cogt.image.prompt_image import PromptImage, PromptImageBytes, Promp
from pipelex.cogt.llm.llm_job import LLMJob
from pipelex.cogt.llm.token_category import NbTokensByCategoryDict, TokenCategory
from pipelex.cogt.ocr.ocr_output import ExtractedImageFromPage, OcrOutput, Page
-from pipelex.config import get_config
-from pipelex.hub import get_secrets_provider
+from pipelex.hub import get_plugin_manager, get_secrets_provider
from pipelex.plugins.openai.openai_factory import OpenAIFactory
from pipelex.tools.misc.base_64_utils import encode_to_base64, load_binary_as_base64
@@ -38,7 +37,7 @@ class MistralFactory:
@classmethod
def make_mistral_client(cls) -> Mistral:
return Mistral(
- api_key=get_config().plugins.mistral_config.api_key(secrets_provider=get_secrets_provider()),
+ api_key=get_plugin_manager().plugin_configs.mistral_config.api_key(secrets_provider=get_secrets_provider()),
)
#########################################################
diff --git a/pipelex/plugins/mistral/mistral_llm_worker.py b/pipelex/plugins/mistral/mistral_llm_worker.py
index 433b531fc..5cbb1965d 100644
--- a/pipelex/plugins/mistral/mistral_llm_worker.py
+++ b/pipelex/plugins/mistral/mistral_llm_worker.py
@@ -9,14 +9,14 @@ from pipelex import log
from pipelex.cogt.exceptions import LLMCompletionError, LLMEngineParameterError, SdkTypeError
from pipelex.cogt.llm.llm_job import LLMJob
from pipelex.cogt.llm.llm_models.llm_engine import LLMEngine
-from pipelex.cogt.llm.llm_worker_abstract import LLMWorkerAbstract
+from pipelex.cogt.llm.llm_worker_internal_abstract import LLMWorkerInternalAbstract
from pipelex.cogt.llm.structured_output import StructureMethod
from pipelex.plugins.mistral.mistral_factory import MistralFactory
from pipelex.reporting.reporting_protocol import ReportingProtocol
from pipelex.tools.typing.pydantic_utils import BaseModelTypeVar
-class MistralLLMWorker(LLMWorkerAbstract):
+class MistralLLMWorker(LLMWorkerInternalAbstract):
def __init__(
self,
sdk_instance: Any,
@@ -24,7 +24,12 @@ class MistralLLMWorker(LLMWorkerAbstract):
structure_method: Optional[StructureMethod] = None,
reporting_delegate: Optional[ReportingProtocol] = None,
):
- super().__init__(llm_engine=llm_engine, structure_method=structure_method, reporting_delegate=reporting_delegate)
+ LLMWorkerInternalAbstract.__init__(
+ self,
+ llm_engine=llm_engine,
+ structure_method=structure_method,
+ reporting_delegate=reporting_delegate,
+ )
if not isinstance(sdk_instance, Mistral):
raise SdkTypeError(f"Provided LLM sdk_instance for {self.__class__.__name__} is not of type Mistral: it's a '{type(sdk_instance)}'")
diff --git a/pipelex/plugins/openai/custom_endpoint_config.py b/pipelex/plugins/openai/custom_endpoint_config.py
index 8c371d010..2564ee6fa 100644
--- a/pipelex/plugins/openai/custom_endpoint_config.py
+++ b/pipelex/plugins/openai/custom_endpoint_config.py
@@ -3,15 +3,15 @@ from typing import Optional, Tuple
from pydantic import Field
from pipelex import log
-from pipelex.cogt.exceptions import CogtError
from pipelex.tools.config.models import ConfigModel
from pipelex.tools.environment import EnvVarNotFoundError, get_required_env
+from pipelex.tools.exceptions import CredentialsError
from pipelex.tools.secrets.secrets_errors import SecretNotFoundError
from pipelex.tools.secrets.secrets_provider_abstract import SecretsProviderAbstract
from pipelex.types import StrEnum
-class CustomEndpointCredentialsError(CogtError):
+class CustomEndpointCredentialsError(CredentialsError):
pass
diff --git a/pipelex/plugins/openai/openai_config.py b/pipelex/plugins/openai/openai_config.py
index 901f38206..c79804502 100644
--- a/pipelex/plugins/openai/openai_config.py
+++ b/pipelex/plugins/openai/openai_config.py
@@ -1,15 +1,15 @@
from pydantic import Field
from pipelex import log
-from pipelex.cogt.exceptions import CogtError
from pipelex.tools.config.models import ConfigModel
from pipelex.tools.environment import EnvVarNotFoundError, get_required_env
+from pipelex.tools.exceptions import CredentialsError
from pipelex.tools.secrets.secrets_errors import SecretNotFoundError
from pipelex.tools.secrets.secrets_provider_abstract import SecretsProviderAbstract
from pipelex.types import StrEnum
-class OpenAICredentialsError(CogtError):
+class OpenAICredentialsError(CredentialsError):
pass
diff --git a/pipelex/plugins/openai/openai_factory.py b/pipelex/plugins/openai/openai_factory.py
index 0cb832f8b..bf0e2a555 100644
--- a/pipelex/plugins/openai/openai_factory.py
+++ b/pipelex/plugins/openai/openai_factory.py
@@ -19,8 +19,7 @@ from pipelex.cogt.llm.llm_job import LLMJob
from pipelex.cogt.llm.llm_models.llm_engine import LLMEngine
from pipelex.cogt.llm.llm_models.llm_platform import LLMPlatform
from pipelex.cogt.llm.token_category import NbTokensByCategoryDict, TokenCategory
-from pipelex.config import get_config
-from pipelex.hub import get_secrets_provider
+from pipelex.hub import get_plugin_manager, get_secrets_provider
from pipelex.tools.misc.base_64_utils import load_binary_as_base64
@@ -31,7 +30,7 @@ class OpenAIFactory:
api_key: Optional[str] = None
match llm_platform:
case LLMPlatform.AZURE_OPENAI:
- azure_openai_config = get_config().plugins.azure_openai_config
+ azure_openai_config = get_plugin_manager().plugin_configs.azure_openai_config
endpoint, api_version, api_key = azure_openai_config.configure(secrets_provider=get_secrets_provider())
log.verbose(f"Making AsyncAzureOpenAI client with endpoint: {endpoint}, api_version: {api_version}")
@@ -41,7 +40,7 @@ class OpenAIFactory:
api_version=api_version,
)
case LLMPlatform.PERPLEXITY:
- perplexity_config = get_config().plugins.perplexity_config
+ perplexity_config = get_plugin_manager().plugin_configs.perplexity_config
endpoint, api_key = perplexity_config.configure(secrets_provider=get_secrets_provider())
log.verbose(f"Making perplexity AsyncOpenAI client with endpoint: {endpoint}")
@@ -50,11 +49,11 @@ class OpenAIFactory:
base_url=endpoint,
)
case LLMPlatform.OPENAI:
- openai_config = get_config().plugins.openai_config
+ openai_config = get_plugin_manager().plugin_configs.openai_config
api_key = openai_config.get_api_key(secrets_provider=get_secrets_provider())
the_client = openai.AsyncOpenAI(api_key=api_key)
case LLMPlatform.VERTEXAI:
- vertexai_config = get_config().plugins.vertexai_config
+ vertexai_config = get_plugin_manager().plugin_configs.vertexai_config
endpoint, api_key = vertexai_config.configure(secrets_provider=get_secrets_provider())
log.verbose(f"Making vertex AsyncOpenAI client with endpoint: {endpoint}")
@@ -63,7 +62,7 @@ class OpenAIFactory:
base_url=endpoint,
)
case LLMPlatform.XAI:
- xai_config = get_config().plugins.xai_config
+ xai_config = get_plugin_manager().plugin_configs.xai_config
endpoint, api_key = xai_config.configure(secrets_provider=get_secrets_provider())
log.verbose(f"Making Xai AsyncOpenAI client with endpoint: {endpoint}")
@@ -72,7 +71,7 @@ class OpenAIFactory:
base_url=endpoint,
)
case LLMPlatform.CUSTOM_LLM:
- custom_endpoint_config = get_config().plugins.custom_endpoint_config
+ custom_endpoint_config = get_plugin_manager().plugin_configs.custom_endpoint_config
base_url, api_key = custom_endpoint_config.configure(secrets_provider=get_secrets_provider())
log.verbose(f"Making custom AsyncOpenAI client with base_url: {base_url}")
diff --git a/pipelex/plugins/openai/openai_imgg_worker.py b/pipelex/plugins/openai/openai_imgg_worker.py
index b68aa7c82..6ac84f67f 100644
--- a/pipelex/plugins/openai/openai_imgg_worker.py
+++ b/pipelex/plugins/openai/openai_imgg_worker.py
@@ -11,7 +11,7 @@ from pipelex.cogt.imgg.imgg_engine import ImggEngine
from pipelex.cogt.imgg.imgg_job import ImggJob
from pipelex.cogt.imgg.imgg_job_components import Quality
from pipelex.cogt.imgg.imgg_worker_abstract import ImggWorkerAbstract
-from pipelex.config import get_config
+from pipelex.hub import get_plugin_manager
from pipelex.plugins.openai.openai_imgg_factory import OpenAIImggFactory
from pipelex.reporting.reporting_protocol import ReportingProtocol
from pipelex.tools.misc.base_64_utils import save_base64_to_binary_file
@@ -54,7 +54,7 @@ class OpenAIImggWorker(ImggWorkerAbstract):
moderation = OpenAIImggFactory.moderation_for_gpt_image_1(is_moderated=imgg_job.job_params.is_moderated)
background = OpenAIImggFactory.background_for_gpt_image_1(background=imgg_job.job_params.background)
quality = OpenAIImggFactory.quality_for_gpt_image_1(quality=imgg_job.job_params.quality or Quality.LOW)
- output_compression = get_config().plugins.openai_config.image_output_compression
+ output_compression = get_plugin_manager().plugin_configs.openai_config.image_output_compression
result = await self.openai_client.images.generate(
prompt=imgg_job.imgg_prompt.positive_text,
model=self.imgg_engine.imgg_model_name,
diff --git a/pipelex/plugins/openai/openai_llm_worker.py b/pipelex/plugins/openai/openai_llm_worker.py
index c0305ebbf..525cd17e9 100644
--- a/pipelex/plugins/openai/openai_llm_worker.py
+++ b/pipelex/plugins/openai/openai_llm_worker.py
@@ -11,14 +11,14 @@ from pipelex.cogt.exceptions import LLMCompletionError, LLMEngineParameterError,
from pipelex.cogt.llm.llm_job import LLMJob
from pipelex.cogt.llm.llm_models.llm_engine import LLMEngine
from pipelex.cogt.llm.llm_models.llm_family import LLMFamily
-from pipelex.cogt.llm.llm_worker_abstract import LLMWorkerAbstract
+from pipelex.cogt.llm.llm_worker_internal_abstract import LLMWorkerInternalAbstract
from pipelex.cogt.llm.structured_output import StructureMethod
from pipelex.plugins.openai.openai_factory import OpenAIFactory
from pipelex.reporting.reporting_protocol import ReportingProtocol
from pipelex.tools.typing.pydantic_utils import BaseModelTypeVar
-class OpenAILLMWorker(LLMWorkerAbstract):
+class OpenAILLMWorker(LLMWorkerInternalAbstract):
def __init__(
self,
sdk_instance: Any,
@@ -26,7 +26,12 @@ class OpenAILLMWorker(LLMWorkerAbstract):
structure_method: Optional[StructureMethod],
reporting_delegate: Optional[ReportingProtocol] = None,
):
- super().__init__(llm_engine=llm_engine, structure_method=structure_method, reporting_delegate=reporting_delegate)
+ LLMWorkerInternalAbstract.__init__(
+ self,
+ llm_engine=llm_engine,
+ structure_method=structure_method,
+ reporting_delegate=reporting_delegate,
+ )
if not isinstance(sdk_instance, openai.AsyncOpenAI):
raise SdkTypeError(
diff --git a/pipelex/plugins/openai/perplexity_config.py b/pipelex/plugins/openai/perplexity_config.py
index 9f7665474..3fd06d0e0 100644
--- a/pipelex/plugins/openai/perplexity_config.py
+++ b/pipelex/plugins/openai/perplexity_config.py
@@ -3,15 +3,15 @@ from typing import Tuple
from pydantic import Field
from pipelex import log
-from pipelex.cogt.exceptions import CogtError
from pipelex.tools.config.models import ConfigModel
from pipelex.tools.environment import EnvVarNotFoundError, get_required_env
+from pipelex.tools.exceptions import CredentialsError
from pipelex.tools.secrets.secrets_errors import SecretNotFoundError
from pipelex.tools.secrets.secrets_provider_abstract import SecretsProviderAbstract
from pipelex.types import StrEnum
-class PerplexityCredentialsError(CogtError):
+class PerplexityCredentialsError(CredentialsError):
pass
diff --git a/pipelex/plugins/openai/vertexai_config.py b/pipelex/plugins/openai/vertexai_config.py
index 861fa8bd1..92b4e5bce 100644
--- a/pipelex/plugins/openai/vertexai_config.py
+++ b/pipelex/plugins/openai/vertexai_config.py
@@ -3,16 +3,17 @@ from typing import Any, Dict, Tuple
from pydantic import Field
from pipelex import log
-from pipelex.cogt.exceptions import CogtError, MissingDependencyError
+from pipelex.cogt.exceptions import MissingDependencyError
from pipelex.tools.config.models import ConfigModel
from pipelex.tools.environment import EnvVarNotFoundError, get_required_env
+from pipelex.tools.exceptions import CredentialsError
from pipelex.tools.misc.json_utils import load_json_dict_from_path
from pipelex.tools.secrets.secrets_errors import SecretNotFoundError
from pipelex.tools.secrets.secrets_provider_abstract import SecretsProviderAbstract
from pipelex.types import StrEnum
-class VertexAICredentialsError(CogtError):
+class VertexAICredentialsError(CredentialsError):
pass
diff --git a/pipelex/plugins/openai/xai_config.py b/pipelex/plugins/openai/xai_config.py
index 45b49a2e4..1719e8d9e 100644
--- a/pipelex/plugins/openai/xai_config.py
+++ b/pipelex/plugins/openai/xai_config.py
@@ -3,15 +3,15 @@ from typing import Tuple
from pydantic import Field
from pipelex import log
-from pipelex.cogt.exceptions import CogtError
from pipelex.tools.config.models import ConfigModel
from pipelex.tools.environment import EnvVarNotFoundError, get_required_env
+from pipelex.tools.exceptions import CredentialsError
from pipelex.tools.secrets.secrets_errors import SecretNotFoundError
from pipelex.tools.secrets.secrets_provider_abstract import SecretsProviderAbstract
from pipelex.types import StrEnum
-class XaiCredentialsError(CogtError):
+class XaiCredentialsError(CredentialsError):
pass
diff --git a/pipelex/plugins/plugin_manager.py b/pipelex/plugins/plugin_manager.py
new file mode 100644
index 000000000..9c14cb73d
--- /dev/null
+++ b/pipelex/plugins/plugin_manager.py
@@ -0,0 +1,27 @@
+from typing import Optional
+
+from pipelex.libraries.library_config import LibraryConfig
+from pipelex.plugins.plugin_sdk_registry import PluginSdkRegistry
+from pipelex.plugins.plugins_config import PluginConfig
+from pipelex.tools.misc.toml_utils import load_toml_from_path
+
+
+class PluginManager:
+ def __init__(self):
+ self._plugin_configs: Optional[PluginConfig] = None
+ self.plugin_sdk_registry = PluginSdkRegistry()
+
+ @property
+ def plugin_configs(self) -> PluginConfig:
+ if self._plugin_configs is None:
+ raise RuntimeError("Plugin configs not loaded")
+ return self._plugin_configs
+
+ def setup(self):
+ plugin_config_path = LibraryConfig.get_plugin_config_path()
+ plugin_config_dict = load_toml_from_path(path=plugin_config_path)
+ self._plugin_configs = PluginConfig.model_validate(plugin_config_dict)
+
+ def teardown(self):
+ self._plugin_configs = None
+ self.plugin_sdk_registry.teardown()
diff --git a/pipelex/cogt/plugin_manager.py b/pipelex/plugins/plugin_sdk_registry.py
similarity index 53%
rename from pipelex/cogt/plugin_manager.py
rename to pipelex/plugins/plugin_sdk_registry.py
index 85955cbf4..59619aa32 100644
--- a/pipelex/cogt/plugin_manager.py
+++ b/pipelex/plugins/plugin_sdk_registry.py
@@ -8,7 +8,7 @@ from pipelex.cogt.ocr.ocr_platform import OcrPlatform
from pipelex.types import StrEnum
-class PluginHandle(StrEnum):
+class PluginSdkHandle(StrEnum):
OPENAI_SDK = "openai_sdk"
AZURE_OPENAI_SDK = "azure_openai_sdk"
ANTHROPIC_SDK = "anthropic_sdk"
@@ -22,70 +22,73 @@ class PluginHandle(StrEnum):
FAL_SDK = "fal_sdk"
@staticmethod
- def get_for_llm_platform(llm_platform: LLMPlatform) -> "PluginHandle":
+ def get_for_llm_platform(llm_platform: LLMPlatform) -> "PluginSdkHandle":
match llm_platform:
case LLMPlatform.OPENAI:
- return PluginHandle.OPENAI_SDK
+ return PluginSdkHandle.OPENAI_SDK
case LLMPlatform.AZURE_OPENAI:
- return PluginHandle.AZURE_OPENAI_SDK
+ return PluginSdkHandle.AZURE_OPENAI_SDK
case LLMPlatform.ANTHROPIC:
- return PluginHandle.ANTHROPIC_SDK
+ return PluginSdkHandle.ANTHROPIC_SDK
case LLMPlatform.MISTRAL:
- return PluginHandle.MISTRAL_SDK
+ return PluginSdkHandle.MISTRAL_SDK
case LLMPlatform.BEDROCK:
- return PluginHandle.BEDROCK_SDK
+ return PluginSdkHandle.BEDROCK_SDK
case LLMPlatform.BEDROCK_ANTHROPIC:
- return PluginHandle.BEDROCK_ANTHROPIC_SDK
+ return PluginSdkHandle.BEDROCK_ANTHROPIC_SDK
case LLMPlatform.PERPLEXITY:
- return PluginHandle.PERPLEXITY_OPENAI_SDK
+ return PluginSdkHandle.PERPLEXITY_OPENAI_SDK
case LLMPlatform.VERTEXAI:
- return PluginHandle.VERTEXAI_OPENAI_SDK
+ return PluginSdkHandle.VERTEXAI_OPENAI_SDK
case LLMPlatform.XAI:
- return PluginHandle.XAI_OPENAI_SDK
+ return PluginSdkHandle.XAI_OPENAI_SDK
case LLMPlatform.CUSTOM_LLM:
- return PluginHandle.CUSTOM_LLM_OPENAI_SDK
+ return PluginSdkHandle.CUSTOM_LLM_OPENAI_SDK
@staticmethod
- def get_for_ocr_engine(ocr_platform: OcrPlatform) -> "PluginHandle":
+ def get_for_ocr_engine(ocr_platform: OcrPlatform) -> "PluginSdkHandle":
match ocr_platform:
case OcrPlatform.MISTRAL:
- return PluginHandle.MISTRAL_SDK
+ return PluginSdkHandle.MISTRAL_SDK
@staticmethod
- def get_for_imgg_engine(imgg_platform: ImggPlatform) -> "PluginHandle":
+ def get_for_imgg_engine(imgg_platform: ImggPlatform) -> "PluginSdkHandle":
match imgg_platform:
case ImggPlatform.FAL_AI:
- return PluginHandle.FAL_SDK
+ return PluginSdkHandle.FAL_SDK
case ImggPlatform.OPENAI:
- return PluginHandle.OPENAI_SDK
+ return PluginSdkHandle.OPENAI_SDK
-PluginManagerRoot = Dict[str, Any]
+PluginSdkRegistryRoot = Dict[str, Any]
-class PluginManager(RootModel[PluginManagerRoot]):
- root: PluginManagerRoot = Field(default_factory=dict)
+class PluginSdkRegistry(RootModel[PluginSdkRegistryRoot]):
+ root: PluginSdkRegistryRoot = Field(default_factory=dict)
- def reset(self):
- self.root.clear()
+ def teardown(self):
+ for llm_sdk_instance in self.root.values():
+ if hasattr(llm_sdk_instance, "teardown"):
+ llm_sdk_instance.teardown()
+ self.root = {}
- def get_llm_sdk_instance(self, llm_sdk_handle: PluginHandle) -> Optional[Any]:
+ def get_llm_sdk_instance(self, llm_sdk_handle: PluginSdkHandle) -> Optional[Any]:
return self.root.get(llm_sdk_handle)
- def set_llm_sdk_instance(self, llm_sdk_handle: PluginHandle, llm_sdk_instance: Any) -> Any:
+ def set_llm_sdk_instance(self, llm_sdk_handle: PluginSdkHandle, llm_sdk_instance: Any) -> Any:
self.root[llm_sdk_handle] = llm_sdk_instance
return llm_sdk_instance
- def get_ocr_sdk_instance(self, ocr_sdk_handle: PluginHandle) -> Optional[Any]:
+ def get_ocr_sdk_instance(self, ocr_sdk_handle: PluginSdkHandle) -> Optional[Any]:
return self.root.get(ocr_sdk_handle)
- def set_ocr_sdk_instance(self, ocr_sdk_handle: PluginHandle, ocr_sdk_instance: Any) -> Any:
+ def set_ocr_sdk_instance(self, ocr_sdk_handle: PluginSdkHandle, ocr_sdk_instance: Any) -> Any:
self.root[ocr_sdk_handle] = ocr_sdk_instance
return ocr_sdk_instance
- def get_imgg_sdk_instance(self, imgg_sdk_handle: PluginHandle) -> Optional[Any]:
+ def get_imgg_sdk_instance(self, imgg_sdk_handle: PluginSdkHandle) -> Optional[Any]:
return self.root.get(imgg_sdk_handle)
- def set_imgg_sdk_instance(self, imgg_sdk_handle: PluginHandle, imgg_sdk_instance: Any) -> Any:
+ def set_imgg_sdk_instance(self, imgg_sdk_handle: PluginSdkHandle, imgg_sdk_instance: Any) -> Any:
self.root[imgg_sdk_handle] = imgg_sdk_instance
return imgg_sdk_instance
diff --git a/pipelex/plugins/plugins_config.py b/pipelex/plugins/plugins_config.py
index ac5dc7d47..cd11b970f 100644
--- a/pipelex/plugins/plugins_config.py
+++ b/pipelex/plugins/plugins_config.py
@@ -11,7 +11,7 @@ from pipelex.plugins.openai.xai_config import XaiConfig
from pipelex.tools.config.models import ConfigModel
-class PluginsConfig(ConfigModel):
+class PluginConfig(ConfigModel):
anthropic_config: AnthropicConfig
azure_openai_config: AzureOpenAIConfig
bedrock_config: BedrockConfig
diff --git a/pipelex/plugins/specific_llm/specific_llm_config.py b/pipelex/plugins/specific_llm/specific_llm_config.py
new file mode 100644
index 000000000..3eff2f16a
--- /dev/null
+++ b/pipelex/plugins/specific_llm/specific_llm_config.py
@@ -0,0 +1,7 @@
+from typing import Dict
+
+from pipelex.tools.config.models import ConfigModel
+
+
+class SpecificLLMConfig(ConfigModel):
+ llm_worker_classes: Dict[str, str]
diff --git a/pipelex/tools/aws/aws_config.py b/pipelex/tools/aws/aws_config.py
index 6f5fce9f2..1dbda8abb 100644
--- a/pipelex/tools/aws/aws_config.py
+++ b/pipelex/tools/aws/aws_config.py
@@ -6,12 +6,12 @@ from pipelex import log
from pipelex.hub import get_secret
from pipelex.tools.config.models import ConfigModel
from pipelex.tools.environment import EnvVarNotFoundError, get_required_env
-from pipelex.tools.exceptions import ToolException
+from pipelex.tools.exceptions import CredentialsError
from pipelex.tools.secrets.secrets_errors import SecretNotFoundError
from pipelex.types import StrEnum
-class AwsCredentialsError(ToolException):
+class AwsCredentialsError(CredentialsError):
pass
diff --git a/pipelex/tools/exceptions.py b/pipelex/tools/exceptions.py
index 054477cfc..20eef6a10 100644
--- a/pipelex/tools/exceptions.py
+++ b/pipelex/tools/exceptions.py
@@ -14,6 +14,10 @@ class ToolException(RootException):
pass
+class CredentialsError(RootException):
+ pass
+
+
class TracebackMessageErrorMode(StrEnum):
ERROR = "error"
EXCEPTION = "exception"
diff --git a/pipelex/tools/typing/pydantic_utils.py b/pipelex/tools/typing/pydantic_utils.py
index ff269e7f7..d4115b02d 100644
--- a/pipelex/tools/typing/pydantic_utils.py
+++ b/pipelex/tools/typing/pydantic_utils.py
@@ -26,7 +26,9 @@ def format_pydantic_validation_error(exc: ValidationError) -> str:
extra_fields = [f"{'.'.join(map(str, err['loc']))}: {err['input']}" for err in exc.errors() if err["type"] == "extra_forbidden"]
type_errors = [f"{'.'.join(map(str, err['loc']))}: expected {err['type']}" for err in exc.errors() if err["type"] == "type_error"]
value_errors = [f"{'.'.join(map(str, err['loc']))}: {err['msg']}" for err in exc.errors() if err["type"] == "value_error"]
- enum_errors = [f"{'.'.join(map(str, err['loc']))}: invalid enum value" for err in exc.errors() if err["type"] == "enum"]
+ enum_errors = [
+ f"{'.'.join(map(str, err['loc']))}: invalid enum value '{err.get('input', 'unknown')}'" for err in exc.errors() if err["type"] == "enum"
+ ]
model_type_errors: List[str] = []
for err in exc.errors():
if err["type"] == "model_type":
diff --git a/pyproject.toml b/pyproject.toml
index c1f3697f4..6e57ffae7 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
[project]
name = "pipelex"
-version = "0.4.8"
+version = "0.4.9"
description = "Pipelex is an open-source dev tool based on a simple declarative language that lets you define replicable, structured, composable LLM pipelines."
authors = [{ name = "Evotis S.A.S.", email = "[email protected]" }]
maintainers = [{ name = "Pipelex staff", email = "[email protected]" }]
diff --git a/tests/integration/pipelex/cogt/test_llm_engines.py b/tests/integration/pipelex/cogt/test_llm_engines.py
index b304f2cff..a388ddf58 100644
--- a/tests/integration/pipelex/cogt/test_llm_engines.py
+++ b/tests/integration/pipelex/cogt/test_llm_engines.py
@@ -22,13 +22,12 @@ class TestLLMEngines:
generated_text = await llm_worker.gen_text(llm_job=llm_job)
assert generated_text
pretty_print(generated_text)
- llm_model = llm_worker.llm_engine.llm_model
- if llm_model.is_gen_object_supported:
+ if llm_worker.is_gen_object_supported:
generated_object = await llm_worker.gen_object(llm_job=llm_job, schema=Person)
assert generated_object
pretty_print(generated_object)
else:
- log.info(f"No object generation supported for this model: '{llm_model.name_and_version_and_platform}'")
+ log.info(f"No object generation supported for this worker: '{llm_worker.desc}'")
async def test_one_llm_engine(self, llm_job_params: LLMJobParams, llm_handle: str):
inference_manager = get_inference_manager()
@@ -44,7 +43,6 @@ class TestLLMEngines:
log.info(f"Testing {llm_id} on {llm_platform}")
llm_models_provider = get_llm_models_provider()
llm_models = llm_models_provider.get_all_llm_models()
- llm_worker_factory = LLMWorkerFactory()
count = 0
for llm_model in llm_models:
platform_llm_id = llm_model.platform_llm_id.get(llm_platform)
@@ -55,7 +53,7 @@ class TestLLMEngines:
llm_platform=llm_platform,
llm_model=llm_model,
)
- llm_worker = llm_worker_factory.make_llm_worker(
+ llm_worker = LLMWorkerFactory.make_llm_worker(
llm_engine=llm_engine,
reporting_delegate=get_report_delegate(),
)
@@ -73,16 +71,15 @@ class TestLLMEngines:
pytest.fail(f"No llm_engines found for llm_id '{llm_id}' on platform '{llm_platform}'")
async def test_llm_engines_from_one_family(self, llm_job_params: LLMJobParams, llm_family: LLMFamily):
- inference_manager = get_inference_manager()
llm_handle_to_llm_engine_blueprint = get_llm_deck().llm_handles
count = 0
- for llm_handle, llm_engine_blueprint in llm_handle_to_llm_engine_blueprint.items():
+ for _, llm_engine_blueprint in llm_handle_to_llm_engine_blueprint.items():
llm_engine = LLMEngineFactory.make_llm_engine(llm_engine_blueprint=llm_engine_blueprint)
if llm_engine.llm_model.llm_family != llm_family:
continue
- llm_worker = inference_manager.get_llm_worker(
- llm_handle=llm_handle,
- specific_llm_engine_blueprint=llm_engine_blueprint,
+ llm_worker = LLMWorkerFactory.make_llm_worker(
+ llm_engine=llm_engine,
+ reporting_delegate=get_report_delegate(),
)
llm_job = LLMJobFactory.make_llm_job_from_prompt_contents(
system_text=None,
@@ -95,15 +92,14 @@ class TestLLMEngines:
log.info(f"Tested {count} llm_engines for family {llm_family}")
async def test_llm_engines_from_one_creator(self, llm_job_params: LLMJobParams, llm_creator: LLMCreator):
- inference_manager = get_inference_manager()
llm_handle_to_llm_engine_blueprint = get_llm_deck().llm_handles
- for llm_handle, llm_engine_blueprint in llm_handle_to_llm_engine_blueprint.items():
+ for _, llm_engine_blueprint in llm_handle_to_llm_engine_blueprint.items():
llm_engine = LLMEngineFactory.make_llm_engine(llm_engine_blueprint=llm_engine_blueprint)
if llm_engine.llm_model.llm_family.creator != llm_creator:
continue
- llm_worker = inference_manager.get_llm_worker(
- llm_handle=llm_handle,
- specific_llm_engine_blueprint=llm_engine_blueprint,
+ llm_worker = LLMWorkerFactory.make_llm_worker(
+ llm_engine=llm_engine,
+ reporting_delegate=get_report_delegate(),
)
llm_job = LLMJobFactory.make_llm_job_from_prompt_contents(
system_text=None,
@@ -115,7 +111,6 @@ class TestLLMEngines:
async def test_llm_engines_from_one_platform(self, llm_job_params: LLMJobParams, llm_platform: LLMPlatform):
llm_models_provider = get_llm_models_provider()
llm_models = llm_models_provider.get_all_llm_models()
- llm_worker_factory = LLMWorkerFactory()
for llm_model in llm_models:
if llm_platform not in llm_model.enabled_platforms:
continue
@@ -123,7 +118,7 @@ class TestLLMEngines:
llm_platform=llm_platform,
llm_model=llm_model,
)
- llm_worker = llm_worker_factory.make_llm_worker(
+ llm_worker = LLMWorkerFactory.make_llm_worker(
llm_engine=llm_engine,
reporting_delegate=get_report_delegate(),
)
diff --git a/tests/integration/pipelex/cogt/test_llm_gen_object.py b/tests/integration/pipelex/cogt/test_llm_gen_object.py
index 5fb151c35..05005e319 100644
--- a/tests/integration/pipelex/cogt/test_llm_gen_object.py
+++ b/tests/integration/pipelex/cogt/test_llm_gen_object.py
@@ -31,8 +31,8 @@ class TestLLMGenObject:
@pytest.mark.parametrize("user_text, expected_instance", LLMTestCases.SINGLE_OBJECT)
async def test_gen_object_async_using_handle(self, llm_job_params: LLMJobParams, llm_handle: str, user_text: str, expected_instance: BaseModel):
llm_worker = get_llm_worker(llm_handle=llm_handle)
- if not llm_worker.llm_engine.is_gen_object_supported:
- pytest.skip(f"LLM '{llm_handle}' does not support object generation")
+ if not llm_worker.is_gen_object_supported:
+ pytest.skip(f"LLM worker '{llm_worker.desc}' does not support object generation")
llm_job = LLMJobFactory.make_llm_job_from_prompt_contents(
user_text=user_text,
llm_job_params=llm_job_params,
@@ -46,8 +46,8 @@ class TestLLMGenObject:
@pytest.mark.parametrize("user_text, expected_instance", LLMTestCases.SINGLE_OBJECT)
async def test_gen_object_async_using_llm_preset(self, llm_preset_id: str, user_text: str, expected_instance: BaseModel):
llm_worker, llm_job = get_async_worker_and_job(llm_preset_id=llm_preset_id, user_text=user_text)
- if not llm_worker.llm_engine.is_gen_object_supported:
- pytest.skip(f"LLM '{llm_worker.llm_engine.tag}' does not support object generation")
+ if not llm_worker.is_gen_object_supported:
+ pytest.skip(f"'{llm_worker.desc}' does not support object generation")
expected_class = expected_instance.__class__
output = await llm_worker.gen_object(llm_job=llm_job, schema=expected_class)
pretty_print(output)
@@ -59,8 +59,8 @@ class TestLLMGenObject:
self, llm_job_params: LLMJobParams, llm_handle: str, case_tuples: List[Tuple[str, BaseModel]]
):
llm_worker = get_llm_worker(llm_handle=llm_handle)
- if not llm_worker.llm_engine.is_gen_object_supported:
- pytest.skip(f"LLM '{llm_handle}' does not support object generation")
+ if not llm_worker.is_gen_object_supported:
+ pytest.skip(f"'{llm_worker.desc}' does not support object generation")
tasks: List[asyncio.Task[BaseModel]] = []
for case_tuple in case_tuples:
user_text, expected_instance = case_tuple
@@ -83,8 +83,8 @@ class TestLLMGenObject:
@pytest.mark.parametrize("case_tuples", LLMTestCases.MULTIPLE_OBJECTS)
async def test_gen_object_async_multiple_using_llm_preset(self, llm_preset_id: str, case_tuples: List[Tuple[str, BaseModel]]):
llm_worker, llm_job = get_async_worker_and_job(llm_preset_id=llm_preset_id, user_text=case_tuples[0][0])
- if not llm_worker.llm_engine.is_gen_object_supported:
- pytest.skip(f"LLM '{llm_worker.llm_engine.tag}' does not support object generation")
+ if not llm_worker.is_gen_object_supported:
+ pytest.skip(f"'{llm_worker.desc}' does not support object generation")
tasks: List[asyncio.Task[BaseModel]] = []
for case_tuple in case_tuples:
user_text, expected_instance = case_tuple
diff --git a/tests/integration/pipelex/cogt/test_llm_gen_text.py b/tests/integration/pipelex/cogt/test_llm_gen_text.py
index 66f9f231a..fc42fd90f 100644
--- a/tests/integration/pipelex/cogt/test_llm_gen_text.py
+++ b/tests/integration/pipelex/cogt/test_llm_gen_text.py
@@ -1,17 +1,20 @@
import asyncio
-from typing import List
+from typing import List, Tuple
import pytest
from pipelex import log, pretty_print
+from pipelex.cogt.llm.llm_job import LLMJob
from pipelex.cogt.llm.llm_job_components import LLMJobParams
from pipelex.cogt.llm.llm_job_factory import LLMJobFactory
from pipelex.cogt.llm.llm_models.llm_family import LLMFamily
+from pipelex.cogt.llm.llm_worker_abstract import LLMWorkerAbstract
+from pipelex.cogt.llm.llm_worker_internal_abstract import LLMWorkerInternalAbstract
from pipelex.hub import get_llm_deck, get_llm_worker, get_report_delegate
from tests.integration.pipelex.cogt.test_data import LLMTestCases
-def get_async_worker_and_job(llm_preset_id: str, user_text: str):
+def get_worker_and_job(llm_preset_id: str, user_text: str) -> Tuple[LLMWorkerAbstract, LLMJob]:
llm_setting = get_llm_deck().get_llm_setting(llm_setting_or_preset_id=llm_preset_id)
pretty_print(llm_setting, title=llm_preset_id)
pretty_print(user_text)
@@ -43,14 +46,14 @@ class TestLLMGenText:
@pytest.mark.parametrize("topic, prompt_text", LLMTestCases.SINGLE_TEXT)
async def test_gen_text_async_using_llm_preset(self, llm_preset_id: str, topic: str, prompt_text: str):
- llm_worker, llm_job = get_async_worker_and_job(llm_preset_id=llm_preset_id, user_text=prompt_text)
+ llm_worker, llm_job = get_worker_and_job(llm_preset_id=llm_preset_id, user_text=prompt_text)
generated_text = await llm_worker.gen_text(llm_job=llm_job)
assert generated_text
pretty_print(generated_text)
@pytest.mark.parametrize("topic, prompt_text", LLMTestCases.SINGLE_TEXT)
async def test_gen_text_async_multiple_using_llm_preset(self, llm_preset_id: str, topic: str, prompt_text: str):
- llm_worker, llm_job = get_async_worker_and_job(llm_preset_id=llm_preset_id, user_text=prompt_text)
+ llm_worker, llm_job = get_worker_and_job(llm_preset_id=llm_preset_id, user_text=prompt_text)
job_params_base = llm_job.job_params
max_tokens = 30
temperature = 0.1
@@ -60,8 +63,8 @@ class TestLLMGenText:
temperature += 0.2
if temperature > 1:
break
- if llm_worker.llm_engine.llm_model.llm_family == LLMFamily.O_SERIES:
- log.warning("LLMFamily.O1, forcing temprature to 1, setting minimum tokens to avoid empty output")
+ if isinstance(llm_worker, LLMWorkerInternalAbstract) and llm_worker.llm_engine.llm_model.llm_family == LLMFamily.O_SERIES:
+ log.warning("LLMFamily O_series, forcing temprature to 1, setting minimum tokens to avoid empty output")
completion_max_tokens = max(max_tokens, 2000)
llm_job.job_params = job_params_base.model_copy(update={"max_tokens": completion_max_tokens, "temperature": 1})
else:
diff --git a/tests/integration/pipelex/plugins/test_external_plugin.py b/tests/integration/pipelex/plugins/test_external_plugin.py
new file mode 100644
index 000000000..e7cd67003
--- /dev/null
+++ b/tests/integration/pipelex/plugins/test_external_plugin.py
@@ -0,0 +1,113 @@
+from typing import Type
+
+import pytest
+from polyfactory.factories.pydantic_factory import ModelFactory
+from typing_extensions import override
+
+from pipelex import log, pretty_print
+from pipelex.cogt.llm.llm_job import LLMJob
+from pipelex.cogt.llm.llm_job_components import LLMJobParams
+from pipelex.cogt.llm.llm_job_factory import LLMJobFactory
+from pipelex.cogt.llm.llm_models.llm_setting import LLMSetting, LLMSettingChoices
+from pipelex.cogt.llm.llm_worker_abstract import LLMWorkerAbstract
+from pipelex.cogt.llm.token_category import NbTokensByCategoryDict, TokenCategory
+from pipelex.core.concept_native import NativeConcept
+from pipelex.hub import get_inference_manager, get_pipe_router, get_report_delegate
+from pipelex.pipe_operators.pipe_llm import PipeLLM, PipeLLMOutput
+from pipelex.pipe_operators.pipe_llm_prompt import PipeLLMPrompt
+from pipelex.pipe_works.pipe_job_factory import PipeJobFactory
+from pipelex.tools.typing.pydantic_utils import BaseModelTypeVar
+from tests.integration.pipelex.cogt.test_data import LLMTestConstants, Person
+from tests.integration.pipelex.test_data import PipeTestCases
+
+EXTERNAL_PLUGIN_NAME = "mock_external_llm"
+
+
+class MockExternalLLMWorker(LLMWorkerAbstract):
+ @property
+ @override
+ def is_gen_object_supported(self) -> bool:
+ return True
+
+ @override
+ async def _gen_text(
+ self,
+ llm_job: LLMJob,
+ ) -> str:
+ response_text = f"This is a mock LLM response from '{self.__class__}'"
+
+ if llm_tokens_usage := llm_job.job_report.llm_tokens_usage:
+ nb_tokens_by_category: NbTokensByCategoryDict = {
+ TokenCategory.INPUT: 100,
+ TokenCategory.OUTPUT: 100,
+ }
+ llm_tokens_usage.nb_tokens_by_category = nb_tokens_by_category
+ return response_text
+
+ @override
+ async def _gen_object(
+ self,
+ llm_job: LLMJob,
+ schema: Type[BaseModelTypeVar],
+ ) -> BaseModelTypeVar:
+ class ObjectFactory(ModelFactory[schema]): # type: ignore
+ __model__ = schema
+ __use_examples__ = True
+
+ obj = ObjectFactory.build()
+ return obj
+
+
[email protected](loop_scope="class")
+class TestExternalPlugin:
+ async def test_external_llm_worker(self):
+ llm_worker = MockExternalLLMWorker(reporting_delegate=get_report_delegate())
+ llm_job = LLMJobFactory.make_llm_job_from_prompt_contents(
+ system_text=None,
+ user_text=LLMTestConstants.USER_TEXT_SHORT,
+ llm_job_params=LLMJobParams(
+ temperature=0.5,
+ max_tokens=None,
+ seed=None,
+ ),
+ )
+ generated_text = await llm_worker.gen_text(llm_job=llm_job)
+ assert generated_text
+ pretty_print(generated_text)
+ generated_object = await llm_worker.gen_object(llm_job=llm_job, schema=Person)
+ assert generated_object
+ pretty_print(generated_object)
+
+ async def test_pipe_llm_with_external_llm_handle(self):
+ llm_handle = EXTERNAL_PLUGIN_NAME
+ get_inference_manager().set_llm_worker_from_external_plugin(
+ llm_handle=llm_handle,
+ llm_worker_class=MockExternalLLMWorker,
+ )
+ pipe_job = PipeJobFactory.make_pipe_job(
+ pipe=PipeLLM(
+ code="adhoc_for_test_pipe_llm_with_external_llm_handle",
+ domain="generic",
+ output_concept_code=NativeConcept.TEXT.code,
+ pipe_llm_prompt=PipeLLMPrompt(
+ code="adhoc_for_test_pipe_llm_with_external_llm_handle",
+ domain="generic",
+ system_prompt=PipeTestCases.SYSTEM_PROMPT,
+ user_text=PipeTestCases.USER_PROMPT,
+ ),
+ llm_choices=LLMSettingChoices.make_completed_with_defaults(
+ for_text=LLMSetting(
+ llm_handle=llm_handle,
+ temperature=0.5,
+ max_tokens=None,
+ ),
+ ),
+ ),
+ )
+ pipe_llm_output: PipeLLMOutput = await get_pipe_router().run_pipe_job(
+ pipe_job=pipe_job,
+ )
+
+ log.verbose(pipe_llm_output, title="stuff")
+ llm_generated_text = pipe_llm_output.main_stuff_as_text
+ pretty_print(llm_generated_text, title="llm_generated_text")
diff --git a/tests/integration/pipelex/plugins/test_openai.py b/tests/integration/pipelex/plugins/test_openai.py
index a85e485fa..21d04cd9e 100644
--- a/tests/integration/pipelex/plugins/test_openai.py
+++ b/tests/integration/pipelex/plugins/test_openai.py
@@ -3,8 +3,7 @@ import pytest
from pipelex import pretty_print
from pipelex.cogt.exceptions import LLMSDKError
from pipelex.cogt.llm.llm_models.llm_platform import LLMPlatform
-from pipelex.config import get_config
-from pipelex.hub import get_secrets_provider
+from pipelex.hub import get_plugin_manager, get_secrets_provider
from pipelex.plugins.openai.openai_llms import openai_list_available_models
@@ -14,7 +13,7 @@ from pipelex.plugins.openai.openai_llms import openai_list_available_models
@pytest.mark.asyncio(loop_scope="class")
class TestOpenAI:
async def test_openai_api_key(self):
- openai_config = get_config().plugins.openai_config
+ openai_config = get_plugin_manager().plugin_configs.openai_config
assert openai_config.get_api_key(secrets_provider=get_secrets_provider())
# pytest -k test_openai_list_available_models -s -vv
diff --git a/tests/integration/pipelex/test_data.py b/tests/integration/pipelex/test_data.py
index 77e2ec745..3daf1136c 100644
--- a/tests/integration/pipelex/test_data.py
+++ b/tests/integration/pipelex/test_data.py
@@ -2,7 +2,13 @@ from typing import ClassVar, List, Optional, Tuple, Type
from pipelex.core.pipe_run_params import PipeOutputMultiplicity
from pipelex.core.stuff import Stuff
-from pipelex.core.stuff_content import ImageContent, ListContent, PDFContent, StructuredContent, TextContent
+from pipelex.core.stuff_content import (
+ ImageContent,
+ ListContent,
+ PDFContent,
+ StructuredContent,
+ TextContent,
+)
from pipelex.core.stuff_factory import StuffBlueprint, StuffFactory
from pipelex.exceptions import PipeStackOverflowError
from tests.cases import ImageTestCases, PDFTestCases
@@ -83,8 +89,14 @@ class PipeTestCases:
)
STUFFS_IMAGE_ATTRIBUTES: ClassVar[List[Tuple[Stuff, List[str]]]] = [ # stuff, attribute_paths
(STUFF_WITH_IMAGE_ATTRIBUTE, ["stuff_with_image.image_attribute"]),
- (STUFF_WITH_IMAGE_ATTRIBUTE_IN_SUB_OBJECT, ["stuff_with_image_in_sub_object.image_attribute"]),
- (STUFF_WITH_IMAGE_ATTRIBUTE_IN_SUB_OBJECT, ["stuff_with_image_in_sub_object.sub_object.image_attribute"]),
+ (
+ STUFF_WITH_IMAGE_ATTRIBUTE_IN_SUB_OBJECT,
+ ["stuff_with_image_in_sub_object.image_attribute"],
+ ),
+ (
+ STUFF_WITH_IMAGE_ATTRIBUTE_IN_SUB_OBJECT,
+ ["stuff_with_image_in_sub_object.sub_object.image_attribute"],
+ ),
(
STUFF_WITH_IMAGE_ATTRIBUTE_IN_SUB_OBJECT,
[
@@ -93,7 +105,11 @@ class PipeTestCases:
],
),
]
- TRICKY_QUESTION_BLUEPRINT = StuffBlueprint(stuff_name="question", concept_code="answer.Question", content=USER_TEXT_TRICKY_2)
+ TRICKY_QUESTION_BLUEPRINT = StuffBlueprint(
+ stuff_name="question",
+ concept_code="answer.Question",
+ content=USER_TEXT_TRICKY_2,
+ )
BLUEPRINT_AND_PIPE: ClassVar[List[Tuple[str, StuffBlueprint, str]]] = [ # topic, blueprint, pipe
(
"Tricky question conclude",