gen_ai_hub.orchestration.models.tools module

gen_ai_hub.orchestration.models.tools.python_type_to_json_type(py_type)

Converts a Python type to a JSON Schema type.

Parameters:

py_type (type) – The Python type to convert.

Returns:

A dictionary representing the JSON Schema type.

Return type:

Dict[str, Any]

class gen_ai_hub.orchestration.models.tools.ChatCompletionTool(type_: str)

Bases: JSONSerializable

Base class for all chat completion tools.

__init__(type_: str)

Initializes a ChatCompletionTool instance.

Parameters:

type (str) – The type of the tool.

to_dict() Dict[str, Any]

Converts the ChatCompletionTool instance to a dictionary representation.

Returns:

A dictionary representation of the ChatCompletionTool instance.

Return type:

Dict[str, Any]

class gen_ai_hub.orchestration.models.tools.FunctionTool(name: str, parameters: dict, strict: bool = False, description: str | None = None, function: Callable | None = None)

Bases: ChatCompletionTool

Represents a function tool for OpenAI-like function calling.

__init__(name: str, parameters: dict, strict: bool = False, description: str | None = None, function: Callable | None = None)

Initializes a FunctionTool instance.

Parameters:
  • name (str) – The name of the function.

  • parameters (dict) – The parameters schema for the function.

  • strict (bool, optional) – Whether to enforce strict parameter checking, defaults to False

  • description (Optional[str], optional) – The description of the function, defaults to None

  • function (Optional[Callable], optional) – The actual callable function, defaults to None

to_dict() Dict[str, Any]

Converts the FunctionTool instance to a dictionary representation.

Returns:

A dictionary representation of the FunctionTool instance.

Return type:

Dict[str, Any]

execute(**kwargs: Any) Any

Execute the function with the provided arguments.

Raises:

ValueError – If the function is not set or if unexpected arguments are provided in strict mode.

Returns:

The result of the function execution.

Return type:

Any

async aexecute(**kwargs: Any) Any

Asynchronously execute the function with the provided arguments.

Raises:

ValueError – If the function is not set or if unexpected arguments are provided in strict mode.

Returns:

The result of the function execution.

Return type:

Any

static from_function(func: Callable, *, description: str | None = None, strict: bool = False) FunctionTool

Create a FunctionTool from a Python function.

Parameters:
  • func (Callable) – The Python function to convert.

  • description (Optional[str], optional) – The description of the function, defaults to None

  • strict (bool, optional) – Whether to enforce strict parameter checking, defaults to False

Raises:

TypeError – If any parameter is missing a type hint.

Returns:

A FunctionTool instance.

Return type:

FunctionTool

gen_ai_hub.orchestration.models.tools.function_tool(func: Callable | None = None, *, description: str | None = None, strict: bool = False) Callable[[Callable], FunctionTool] | FunctionTool

Create a decorator that converts a function into a FunctionTool.

Usage:

@function_tool

def my_func(…): …

@function_tool()

def my_func(…): …

Parameters:
  • func (Optional[Callable], optional) – The function to convert, defaults to None

  • description (Optional[str], optional) – The description of the function, defaults to None

  • strict (bool, optional) – Whether to enforce strict parameter checking, defaults to False

Returns:

A FunctionTool instance or a decorator function.

Return type:

Callable[[Callable], FunctionTool] | FunctionTool