_remove_a_key

def _remove_a_key(d: Dict, remove_key: Any):

Remove a key from a dictionary recursively.

_remove_title_recursively

def _remove_title_recursively(data, parent_key = None):

Recursively removes the ‘title’ key from all levels of a nested dictionary, except when ‘title’ is an argument name in the schema.

get_openai_function_schema

def get_openai_function_schema(func: Callable):

Generates a schema dict for an OpenAI function based on its signature.

This function is deprecated and will be replaced by :obj:get_openai_tool_schema() in future versions. It parses the function’s parameters and docstring to construct a JSON schema-like dictionary.

Parameters:

  • func (Callable): The OpenAI function to generate the schema for.

Returns:

Dict[str, Any]: A dictionary representing the JSON schema of the function, including its name, description, and parameter specifications.

get_openai_tool_schema

def get_openai_tool_schema(func: Callable):

Generates an OpenAI JSON schema from a given Python function.

This function creates a schema compatible with OpenAI’s API specifications, based on the provided Python function. It processes the function’s parameters, types, and docstrings, and constructs a schema accordingly.

Note:

  • Each parameter in func must have a type annotation; otherwise, it’s treated as ‘Any’.
  • Variable arguments (*args) and keyword arguments (**kwargs) are not supported and will be ignored.
  • A functional description including a brief and detailed explanation should be provided in the docstring of func.
  • All parameters of func must be described in its docstring.
  • Supported docstring styles: ReST, Google, Numpydoc, and Epydoc.

Parameters:

  • func (Callable): The Python function to be converted into an OpenAI JSON schema.

Returns:

Dict[str, Any]: A dictionary representing the OpenAI JSON schema of the provided function.

See Also: OpenAI API Reference

sanitize_and_enforce_required

def sanitize_and_enforce_required(parameters_dict):

Cleans and updates the function schema to conform with OpenAI’s requirements:

  • Removes invalid ‘default’ fields from the parameters schema.
  • Ensures all fields or function parameters are marked as required.

Parameters:

  • parameters_dict (dict): The dictionary representing the function schema.

Returns:

dict: The updated dictionary with invalid defaults removed and all fields set as required.

generate_docstring

def generate_docstring(code: str, model: Optional[BaseModelBackend] = None):

Generates a docstring for a given function code using LLM.

This function leverages a language model to generate a PEP 8/PEP 257-compliant docstring for a provided Python function. If no model is supplied, a default gpt-4o-mini is used.

Parameters:

  • code (str): The source code of the function.
  • model (Optional[BaseModelBackend]): An optional language model backend instance. If not provided, a default gpt-4o-mini is used.

Returns:

str: The generated docstring.

FunctionTool

class FunctionTool:

An abstraction of a function that OpenAI chat models can call. See https://platform.openai.com/docs/api-reference/chat/create.

By default, the tool schema will be parsed from the func, or you can provide a user-defined tool schema to override.

Parameters:

  • func (Callable): The function to call. The tool schema is parsed from the function signature and docstring by default.
  • openai_tool_schema (Optional[Dict[str, Any]], optional): A user-defined OpenAI tool schema to override the default result. (default: :obj:None)
  • synthesize_schema (Optional[bool], optional): Whether to enable the use of a schema assistant model to automatically synthesize the schema if validation fails or no valid schema is provided. (default: :obj:False)
  • synthesize_schema_model (Optional[BaseModelBackend], optional): An assistant model (e.g., an LLM model) used to synthesize the schema if synthesize_schema is enabled and no valid schema is provided. (default: :obj:None)
  • synthesize_schema_max_retries (int, optional): The maximum number of attempts to retry schema synthesis using the schema assistant model if the previous attempts fail. (default: 2)
  • synthesize_output (Optional[bool], optional): Flag for enabling synthesis output mode, where output is synthesized based on the function’s execution. (default: :obj:False)
  • synthesize_output_model (Optional[BaseModelBackend], optional): Model used for output synthesis in synthesis mode. (default: :obj:None)
  • synthesize_output_format (Optional[Type[BaseModel]], optional): Format for the response when synthesizing output. (default: :obj:None)

init

def __init__(
    self,
    func: Callable,
    openai_tool_schema: Optional[Dict[str, Any]] = None,
    synthesize_schema: Optional[bool] = False,
    synthesize_schema_model: Optional[BaseModelBackend] = None,
    synthesize_schema_max_retries: int = 2,
    synthesize_output: Optional[bool] = False,
    synthesize_output_model: Optional[BaseModelBackend] = None,
    synthesize_output_format: Optional[Type[BaseModel]] = None
):

call

def __call__(self, *args: Any, **kwargs: Any):

is_async

def is_async(self):

validate_openai_tool_schema

def validate_openai_tool_schema(openai_tool_schema: Dict[str, Any]):

Validates the OpenAI tool schema against :obj:ToolAssistantToolsFunction. This function checks if the provided :obj:openai_tool_schema adheres to the specifications required by OpenAI’s :obj:ToolAssistantToolsFunction. It ensures that the function description and parameters are correctly formatted according to JSON Schema specifications.

Parameters:

  • openai_tool_schema (Dict[str, Any]): The OpenAI tool schema to validate.

get_openai_tool_schema

def get_openai_tool_schema(self):

Returns:

Dict[str, Any]: The OpenAI tool schema for this function.

set_openai_tool_schema

def set_openai_tool_schema(self, schema: Dict[str, Any]):

Sets the OpenAI tool schema for this function.

Allows setting a custom OpenAI tool schema for this function.

Parameters:

  • schema (Dict[str, Any]): The OpenAI tool schema to set.

get_openai_function_schema

def get_openai_function_schema(self):

Returns:

Dict[str, Any]: The schema of the function within the OpenAI tool schema.

set_openai_function_schema

def set_openai_function_schema(self, openai_function_schema: Dict[str, Any]):

Sets the schema of the function within the OpenAI tool schema.

Parameters:

  • openai_function_schema (Dict[str, Any]): The function schema to set within the OpenAI tool schema.

get_function_name

def get_function_name(self):

Returns:

str: The name of the function.

set_function_name

def set_function_name(self, name: str):

Sets the name of the function in the OpenAI tool schema.

Parameters:

  • name (str): The name of the function to set.

get_function_description

def get_function_description(self):

Returns:

str: The description of the function.

set_function_description

def set_function_description(self, description: str):

Sets the description of the function in the OpenAI tool schema.

Parameters:

  • description (str): The description for the function.

get_paramter_description

def get_paramter_description(self, param_name: str):

Gets the description of a specific parameter from the function schema.

Parameters:

  • param_name (str): The name of the parameter to get the description.

Returns:

str: The description of the specified parameter.

set_paramter_description

def set_paramter_description(self, param_name: str, description: str):

Sets the description for a specific parameter in the function schema.

Parameters:

  • param_name (str): The name of the parameter to set the description for.
  • description (str): The description for the parameter.

get_parameter

def get_parameter(self, param_name: str):

Gets the schema for a specific parameter from the function schema.

Parameters:

  • param_name (str): The name of the parameter to get the schema.

Returns:

Dict[str, Any]: The schema of the specified parameter.

set_parameter

def set_parameter(self, param_name: str, value: Dict[str, Any]):

Sets the schema for a specific parameter in the function schema.

Parameters:

  • param_name (str): The name of the parameter to set the schema for.
  • value (Dict[str, Any]): The schema to set for the parameter.

synthesize_openai_tool_schema

def synthesize_openai_tool_schema(self, max_retries: Optional[int] = None):

Synthesizes an OpenAI tool schema for the specified function.

This method uses a language model (LLM) to synthesize the OpenAI tool schema for the specified function by first generating a docstring and then creating a schema based on the function’s source code. The schema synthesis and validation process is retried up to max_retries times in case of failure.

Parameters:

  • max_retries (Optional[int], optional): The maximum number of retries for schema synthesis and validation if the process fails. (default: :obj:None)

Returns:

Dict[str, Any]: The synthesis OpenAI tool schema for the function.

synthesize_execution_output

def synthesize_execution_output(
    self,
    args: Optional[tuple[Any, ...]] = None,
    kwargs: Optional[Dict[str, Any]] = None
):

Synthesizes the output of the function based on the provided positional arguments and keyword arguments.

Parameters:

  • args (Optional[tuple]): Positional arguments to pass to the function during synthesis. (default: :obj:None)
  • kwargs (Optional[Dict[str, Any]]): Keyword arguments to pass to the function during synthesis. (default: :obj:None)

Returns:

Any: Synthesized output from the function execution. If no synthesis model is provided, a warning is logged.

parameters

def parameters(self):

Returns:

Dict[str, Any]: the dictionary containing information of parameters of this function.

parameters

def parameters(self, value: Dict[str, Any]):

Setter method for the property :obj:parameters. It will firstly check if the input parameters schema is valid. If invalid, the method will raise :obj:jsonschema.exceptions.SchemaError.

Parameters:

  • value (Dict[str, Any]): the new dictionary value for the function’s parameters.