Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
rgbkrk committed Feb 27, 2024
1 parent 43a812c commit 3d9c477
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 128 deletions.
2 changes: 2 additions & 0 deletions chatlab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
)
from .registry import FunctionRegistry
from spork import Markdown
from instructor import Partial

__version__ = __version__

Expand All @@ -51,4 +52,5 @@
"FunctionRegistry",
"ChatlabMetadata",
"expose_exception_to_llm",
"Partial"
]
4 changes: 2 additions & 2 deletions chatlab/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ async def __process_stream(
tool_argument = ToolArguments(
id=tool_call.id, name=tool_call.function.name, arguments=tool_call.function.arguments
)
# Now we get the function and see if it has the ChatLabMetadata for a render func
func = self.function_registry.get_chatlab_metadata(tool_call.function.name)

# If the user provided a custom renderer, set it on the tool argument object for displaying
func = self.function_registry.get_chatlab_metadata(tool_call.function.name)
if func is not None and func.render is not None:
tool_argument.custom_render = func.render

Expand Down
6 changes: 3 additions & 3 deletions chatlab/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class WhatTime(BaseModel):
from openai.types.chat import ChatCompletionToolParam

from .decorators import ChatlabMetadata

from .errors import ChatLabError

class APIManifest(TypedDict, total=False):
"""The schema for the API."""
Expand All @@ -80,13 +80,13 @@ class APIManifest(TypedDict, total=False):
"""


class FunctionArgumentError(Exception):
class FunctionArgumentError(ChatLabError):
"""Exception raised when a function is called with invalid arguments."""

pass


class UnknownFunctionError(Exception):
class UnknownFunctionError(ChatLabError):
"""Exception raised when a function is called that is not registered."""

pass
Expand Down
18 changes: 10 additions & 8 deletions chatlab/views/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from pydantic import ValidationError
from spork import AutoUpdate

import warnings

from ..components.function_details import ChatFunctionComponent

from ..registry import FunctionRegistry, FunctionArgumentError, UnknownFunctionError, extract_arguments, extract_model_from_function
Expand Down Expand Up @@ -101,23 +103,23 @@ def render(self):
possible_args = parser.parse(self.arguments)

Model = extract_model_from_function(self.name, self.custom_render)
model = Partial[Model](**possible_args)

kwargs = {}
model = Model.model_validate(possible_args)

# Pluck the kwargs out from the crafted model, as we can't pass the pydantic model as the arguments
# However any "inner" models should retain their pydantic Model nature
for k in model.__dict__.keys():
kwargs[k] = getattr(model, k)
kwargs = {k: getattr(model, k) for k in model.__dict__.keys()}

return self.custom_render(**kwargs)
except FunctionArgumentError:
return None
except ValidationError:
return None

try:
return self.custom_render(**kwargs)
except Exception as e:
#print(f"Exception in custom render for {self.name}.", e)
#print(self.arguments)
# Exception in userland code
# Would be preferable to bubble up, however
# it might be due to us passing a not-quite model
return None

return ChatFunctionComponent(name=self.name, verbage=self.verbage, input=self.arguments)
Expand Down
Loading

0 comments on commit 3d9c477

Please sign in to comment.