Last active
October 3, 2023 13:41
-
-
Save Donavan/e09fc8227e6f5472e5a096fe5b084454 to your computer and use it in GitHub Desktop.
JSON Schema Decorator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def __functions(self) -> List[Dict[str, Any]]: | |
""" | |
Extracts JSON schemas from the objects in the toolchest | |
:return: A list of JSON schemas. | |
""" | |
if self.schemas is not None: | |
return self.schemas | |
self.schemas = [] | |
for src_name in self.tool_chest: | |
for name, method in inspect.getmembers(self.tool_chest[src_name], predicate=inspect.ismethod): | |
if hasattr(method, 'schema'): | |
schema = copy.deepcopy(method.schema) | |
schema['name'] = f"{src_name}-{schema['name']}" | |
self.schemas.append(schema) | |
return self.schemas | |
async def __call_function(self, function_id: str, function_args: dict): | |
(toolbelt, function_name) = function_id.split("-", 2) | |
src_obj = self.tool_chest[toolbelt] | |
function_to_call = getattr(src_obj, function_name) | |
return await function_to_call(**function_args) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Callable, Dict, Union | |
def json_schema(description: str, params: Dict[str, Dict[str, Union[str, bool]]]) -> Callable: | |
""" | |
A decorator to attach an OpenAI compatible JSON schema to a function. The schema contains | |
information about the function's name, description, parameters, and required parameters. | |
:param description: A description of the function. | |
:param params: A dictionary containing information about the parameters of the function. | |
:return: The original function with an attached JSON schema. | |
""" | |
def decorator(func: Callable) -> Callable: | |
# Define the structure of parameters | |
parameters = { | |
'type': 'object', | |
'properties': {} | |
} | |
# Keep track of required parameters | |
required = [] | |
# Populate the parameters and required lists | |
for param, info in params.items(): | |
if info.get('required', False): | |
required.append(param) | |
# Populate the properties of each parameter | |
parameters['properties'][param] = { | |
'type': info.get('type', 'string'), | |
'description': info.get('description', '') | |
} | |
# Define the schema with function name, description, and parameters | |
schema = { | |
'name': func.__name__, | |
'description': description, | |
'parameters': parameters | |
} | |
# Add required parameters to the schema if they exist | |
if required: | |
schema['required'] = required | |
# Attach the schema to the original function | |
func.schema = schema | |
# Return the original function | |
return func | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment