Skip to main content

agora.common.function_schema

copy_func

def copy_func(func: Callable) -> Callable

Create a deep copy of a function.

Arguments:

  • func Callable - The function to be copied.

Returns:

  • Callable - A new function that is a deep copy of the original.

add_annotations_from_docstring

def add_annotations_from_docstring(func: Callable,
known_types: dict = DEFAULT_KNOWN_TYPES
) -> Callable

Add annotations derived from Google-style docstrings to the given function.

Arguments:

  • func Callable - The function to be processed.
  • known_types dict, optional - A dictionary mapping type names to Python types.

Returns:

  • Callable - The function with updated annotations.

schema_from_function

def schema_from_function(func: Callable,
strict: bool = False,
known_types: dict = DEFAULT_KNOWN_TYPES) -> dict

Create an OpenAI-like JSON schema from a function's signature and docstring.

Arguments:

  • func Callable - The function to generate the schema from.
  • strict bool, optional - Enforce strict parsing and annotation requirements.
  • known_types dict, optional - A dictionary mapping type names to Python types.

Returns:

  • dict - A JSON schema representing the function's parameters and return.

generate_docstring

def generate_docstring(
description: str, params: Optional[Dict[str, Tuple[Optional[type],
Optional[str]]]],
returns: Optional[Tuple[Optional[type], Optional[str]]]) -> str

Generate a docstring from a description, parameters, and return type.

Arguments:

  • description str - The description of the function.
  • params Optional[Dict[str, Tuple[Optional[type], Optional[str]]] - A mapping of parameter names to type/description tuples.
  • returns Optional[Tuple[Optional[type], Optional[str]]] - The return type and description.

Returns:

  • str - The generated docstring.

set_params_and_annotations

def set_params_and_annotations(name: str, docstring: str,
params: Dict[str, Tuple[Optional[type],
Optional[str]]],
return_type: Optional[type]) -> Callable

Decorator to set parameters and annotations on a function based on the given schema data.

Arguments:

  • name str - The name of the function.
  • docstring str - The function's docstring.
  • params dict - A mapping of parameter names to type/description tuples.
  • return_type Optional[type] - The function's return type.

Returns:

  • Callable - The wrapped function with updated signature and docstring.