Package: tool2schema
tool2schemaA library to convert Python functions to schemas supported by the OpenAI API.Inspired byjanekb04/py2gptandfastai/lm-hackers.Why tool2schema?The OpenAI API supportsfunction calling. However, to tell GPT what functions it can call, you must send the functionsin a JSON format. Withtool2schema, functions can be automatically converted to the correct JSON schema!InstallationYou can installtool2schemausingpip.pip3installtool2schemaUsageOn all functions that you would like to get JSON schema for, simply add theGPTEnableddecorator.# my_functions.pyfromtool2schemaimportGPTEnabled@GPTEnableddefmy_function1(a:int,b:str="Hello"):"""Example function description.:param a: First parameter:param b: Second parameter"""# Function code here...@GPTEnabled(tags=["tag1"])defmy_function2(a:int,b:str="Hello"):"""Example function description.:param a: First parameter:param b: Second parameter"""# Function code here...tool2schemaprovides some methods to easily retrieve your functions.importmy_functions# Module containing your functionsimporttool2schema# Return functions with GPTEnabled decoratorgpt_enable=tool2schema.FindGPTEnabled(my_functions)# Return all function schemasschemas=tool2schema.FindGPTEnabledSchemas(my_functions)# Return function with given namef=tool2schema.FindGPTEnabledByName(my_functions,"my_function1")# Returns all functions with given tagfs=tool2schema.FindGPTEnabledByTag(my_functions,"tag1")# Saves function schemas to JSON filejson_path=# Path to JSON filetool2schema.SaveGPTEnabled(my_functions,json_path)How it Workstool2schemauses certain features of your function to correctly populate the schema.Parameter type hintsParameter default valuesDocstring with parameter descriptionsThe docstring must be of a specific format. An example function is defined below that utilises all of the above features.defmy_function(a:int,b:str="Hello"):"""Example function description.:param a: First parameter:param b: Second parameter"""To get the schema for this function, simply use theGPTEnableddecorator. The decorator will return a class with some additional attributes but can still be called as a function.The schema of the function be accessed using theschemaattribute.my_function.schema.to_json()This returns the function schema in JSON format.Supported Parameter TypesThe following parameter types are supported:intfloatstrboollistAny other parameter types will be listed asobjectin the schema.EnumerationsIf you want to limit the possible values of a parameter, you can use atyping.Literaltype hint or a
subclass ofenum.Enum. For example, usingtyping.Literal:importtyping@GPTEnableddefmy_function(a:int,b:typing.Literal["yes","no"]):"""Example function description.:param a: First parameter:param b: Second parameter"""# Function code here...Equivalent example usingenum.Enum:fromenumimportEnumclassMyEnum(Enum):YES=0NO=1@GPTEnableddefmy_function(a:int,b:MyEnum):"""Example function description.:param a: First parameter:param b: Second parameter"""# Function code here...In the case ofEnumsubclasses, note that the schema will include the enumeration names rather than the values.
In the example above, the schema will include["YES", "NO"]rather than[0, 1].The@GPTEnableddecorator also allows to invoke the function using the name of the enum member rather than an
instance of the class. For example, you may invokemy_function(1, MyEnum.YES)asmy_function(1, "YES").If the enumeration values are not known at the time of defining the function,
you can add them later using theadd_enummethod.@GPTEnableddefmy_function(a:int,b:str,):"""Example function description.:param a: First parameter:param b: Second parameter"""# Function code here...my_function.schema.add_enum("b",["yes","no"])TagsTheGPTEnableddecorator also supports thetagskeyword argument. This allows you to add tags to your function schema.@GPTEnabled(tags=["tag1","tag2"])defmy_function(a:int,b:str="Hello"):"""Example function description.:param a: First parameter:param b: Second parameter"""# Function code here...The tags can then be accessed using thetagsattribute.my_function.tags# ["tag1", "tag2"]You can check if a function has a certain tag using thehas_tagmethod.my_function.has_tag("tag1")# TrueDisable parts of the schemaYou can provideGPTEnabledwith a number of settings to selectively disable parts of the schema.
For example, to omit certain parameters:@GPTEnabled(ignore_parameters=["b","c"])# b and c will not be included in the schemadefmy_function(a:int,b:str,c:float):# Function code here...The available settings are:ignore_parameters: A list of parameter names to exclude from the schema (defaults to[]).ignore_all_parameters: A boolean value indicating whether to exclude all parameters from the schema
(defaults toFalse). When set to true, all other parameter-related settings (ignore_parametersandignore_parameter_descriptions) will be ignored.ignore_function_description: A boolean value indicating whether to exclude the function description from
the schema (defaults toFalse).ignore_parameter_descriptions: A boolean value indicating whether to exclude all parameter descriptions
from the schema (defaults toFalse).It is also possible to specify the settings globally, so that they apply to all functions
unless explicitly overridden. It can be done by editing the global configuration as follows:importtool2schema# Ignore all parameters named "a" or "b" by defaulttool2schema.CONFIG.ignore_parameters=["a","b"]