Skip to content

Commit

Permalink
tracy files for traces and sensible Invoker Exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
sethjuarez committed Aug 16, 2024
1 parent 44e9026 commit 40ab99a
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 15 deletions.
23 changes: 17 additions & 6 deletions runtime/prompty/prompty/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from prompty.tracer import trace
from prompty.core import (
Frontmatter,
InvokerException,
InvokerFactory,
ModelSettings,
Prompty,
Expand Down Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions runtime/prompty/prompty/azure/__init__.py
Original file line number Diff line number Diff line change
@@ -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"
)
10 changes: 10 additions & 0 deletions runtime/prompty/prompty/azure_openai/__init__.py
Original file line number Diff line number Diff line change
@@ -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"
)
26 changes: 26 additions & 0 deletions runtime/prompty/prompty/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
11 changes: 9 additions & 2 deletions runtime/prompty/prompty/openai/__init__.py
Original file line number Diff line number Diff line change
@@ -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"
)
2 changes: 1 addition & 1 deletion runtime/prompty/prompty/openai/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


@InvokerFactory.register_executor("openai")
class AzureOpenAIExecutor(Invoker):
class OpenAIExecutor(Invoker):
"""OpenAI Executor"""

def __init__(self, prompty: Prompty) -> None:
Expand Down
2 changes: 1 addition & 1 deletion runtime/prompty/prompty/openai/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


@InvokerFactory.register_processor("openai")
class AzureOpenAIProcessor(Invoker):
class OpenAIProcessor(Invoker):
"""OpenAI Processor"""

def __init__(self, prompty: Prompty) -> None:
Expand Down
9 changes: 7 additions & 2 deletions runtime/prompty/prompty/serverless/__init__.py
Original file line number Diff line number Diff line change
@@ -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")
2 changes: 1 addition & 1 deletion runtime/prompty/prompty/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 40ab99a

Please sign in to comment.