diff --git a/runtime/prompty/prompty/__init__.py b/runtime/prompty/prompty/__init__.py index a931c0f..ec3ac59 100644 --- a/runtime/prompty/prompty/__init__.py +++ b/runtime/prompty/prompty/__init__.py @@ -6,6 +6,7 @@ from prompty.tracer import trace from prompty.core import ( Frontmatter, + InvokerException, InvokerFactory, ModelSettings, Prompty, @@ -321,18 +322,28 @@ def run( if parameters != {}: prompt.model.parameters = param_hoisting(parameters, prompt.model.parameters) + invoker_type = prompt.model.configuration["type"] + + # invoker registration check + if not InvokerFactory.has_invoker("executor", invoker_type): + raise InvokerException( + f"{invoker_type} Invoker has not been registered properly.", invoker_type + ) + # execute - executor = InvokerFactory.create_executor( - prompt.model.configuration["type"], prompt - ) + executor = InvokerFactory.create_executor(invoker_type, prompt) result = executor(content) # skip? if not raw: + # invoker registration check + if not InvokerFactory.has_invoker("processor", invoker_type): + raise InvokerException( + f"{invoker_type} Invoker has not been registered properly.", invoker_type + ) + # process - processor = InvokerFactory.create_processor( - prompt.model.configuration["type"], prompt - ) + processor = InvokerFactory.create_processor(invoker_type, prompt) result = processor(result) return result diff --git a/runtime/prompty/prompty/azure/__init__.py b/runtime/prompty/prompty/azure/__init__.py index d44688a..be49541 100644 --- a/runtime/prompty/prompty/azure/__init__.py +++ b/runtime/prompty/prompty/azure/__init__.py @@ -1,3 +1,10 @@ # __init__.py -from .executor import AzureOpenAIExecutor -from .processor import AzureOpenAIProcessor +from prompty.core import InvokerException + +try: + from .executor import AzureOpenAIExecutor + from .processor import AzureOpenAIProcessor +except ImportError: + raise InvokerException( + "Error registering AzureOpenAIExecutor and AzureOpenAIProcessor", "azure" + ) diff --git a/runtime/prompty/prompty/azure_openai/__init__.py b/runtime/prompty/prompty/azure_openai/__init__.py new file mode 100644 index 0000000..db30357 --- /dev/null +++ b/runtime/prompty/prompty/azure_openai/__init__.py @@ -0,0 +1,10 @@ +# __init__.py +from prompty.core import InvokerException + +try: + from ..azure.executor import AzureOpenAIExecutor + from ..azure.processor import AzureOpenAIProcessor +except ImportError: + raise InvokerException( + "Error registering AzureOpenAIExecutor and AzureOpenAIProcessor", "azure" + ) diff --git a/runtime/prompty/prompty/core.py b/runtime/prompty/prompty/core.py index 4c25089..80f6851 100644 --- a/runtime/prompty/prompty/core.py +++ b/runtime/prompty/prompty/core.py @@ -343,6 +343,21 @@ class InvokerFactory: _executors: Dict[str, Invoker] = {} _processors: Dict[str, Invoker] = {} + @classmethod + def has_invoker( + cls, type: Literal["renderer", "parser", "executor", "processor"], name: str + ) -> bool: + if type == "renderer": + return name in cls._renderers + elif type == "parser": + return name in cls._parsers + elif type == "executor": + return name in cls._executors + elif type == "processor": + return name in cls._processors + else: + raise ValueError(f"Type {type} not found") + @classmethod def add_renderer(cls, name: str, invoker: Invoker) -> None: cls._renderers[name] = invoker @@ -416,6 +431,17 @@ def create_processor(cls, name: str, prompty: Prompty) -> Invoker: return cls._processors[name](prompty) +class InvokerException(Exception): + """Exception class for Invoker""" + + def __init__(self, message: str, type: str) -> None: + super().__init__(message) + self.type = type + + def __str__(self) -> str: + return f"{super().__str__()}. Make sure to pip install any necessary package extras (i.e. could be something like `pip install prompty[{self.type}]`) for {self.type} as well as import the appropriate invokers (i.e. could be something like `import prompty.{self.type}`)." + + @InvokerFactory.register_renderer("NOOP") @InvokerFactory.register_parser("NOOP") @InvokerFactory.register_executor("NOOP") diff --git a/runtime/prompty/prompty/openai/__init__.py b/runtime/prompty/prompty/openai/__init__.py index d44688a..e2f4e7f 100644 --- a/runtime/prompty/prompty/openai/__init__.py +++ b/runtime/prompty/prompty/openai/__init__.py @@ -1,3 +1,10 @@ # __init__.py -from .executor import AzureOpenAIExecutor -from .processor import AzureOpenAIProcessor +from prompty.core import InvokerException + +try: + from .executor import OpenAIExecutor + from .processor import OpenAIProcessor +except ImportError: + raise InvokerException( + "Error registering OpenAIExecutor and OpenAIProcessor", "openai" + ) diff --git a/runtime/prompty/prompty/openai/executor.py b/runtime/prompty/prompty/openai/executor.py index 19c42ca..a4bd64e 100644 --- a/runtime/prompty/prompty/openai/executor.py +++ b/runtime/prompty/prompty/openai/executor.py @@ -7,7 +7,7 @@ @InvokerFactory.register_executor("openai") -class AzureOpenAIExecutor(Invoker): +class OpenAIExecutor(Invoker): """OpenAI Executor""" def __init__(self, prompty: Prompty) -> None: diff --git a/runtime/prompty/prompty/openai/processor.py b/runtime/prompty/prompty/openai/processor.py index bfbf9b3..a615ec7 100644 --- a/runtime/prompty/prompty/openai/processor.py +++ b/runtime/prompty/prompty/openai/processor.py @@ -6,7 +6,7 @@ @InvokerFactory.register_processor("openai") -class AzureOpenAIProcessor(Invoker): +class OpenAIProcessor(Invoker): """OpenAI Processor""" def __init__(self, prompty: Prompty) -> None: diff --git a/runtime/prompty/prompty/serverless/__init__.py b/runtime/prompty/prompty/serverless/__init__.py index b49b4bc..679180d 100644 --- a/runtime/prompty/prompty/serverless/__init__.py +++ b/runtime/prompty/prompty/serverless/__init__.py @@ -1,3 +1,8 @@ # __init__.py -from .executor import ServerlessExecutor -from .processor import ServerlessProcessor +from prompty.core import InvokerException + +try: + from .executor import ServerlessExecutor + from .processor import ServerlessProcessor +except ImportError: + raise InvokerException("Error registering ServerlessExecutor and ServerlessProcessor", "serverless") diff --git a/runtime/prompty/prompty/tracer.py b/runtime/prompty/prompty/tracer.py index 4d6c6d6..f5f3ed2 100644 --- a/runtime/prompty/prompty/tracer.py +++ b/runtime/prompty/prompty/tracer.py @@ -232,7 +232,7 @@ def add(key: str, value: Any) -> None: if len(self.stack) == 0: trace_file = ( self.output - / f"{frame['name']}.{datetime.now().strftime('%Y%m%d.%H%M%S')}.ptrace" + / f"{frame['name']}.{datetime.now().strftime('%Y%m%d.%H%M%S')}.tracy" ) v = importlib.metadata.version("prompty")