Skip to content

ToolManager

This module contains the tool manager.

The tool manager is used to manage the toold that can be called by the bot.

Define a function using the OpenAITool model from models.py and add it to the tool manager using the add_tool method.

The tool should return a string.

The tool can optionally take keyword arguments, the keyword arguments should be defined in the OpenAITool model.

Call the tool using the call_function method for synchronous functions or the async_call_function method for asynchronous.

OpenAIToolMapping dataclass

OpenAI tool mapping

This class represents an OpenAI tool mapping.

Parameters:

Name Type Description Default
tool_definition OpenAITool

The description of the tool

required
function Callable

The function to call

required
Source code in src/simple_openai/tool_manager.py
20
21
22
23
24
25
26
27
28
29
30
31
32
@dataclass
class OpenAIToolMapping:
    """OpenAI tool mapping

    This class represents an OpenAI tool mapping.

    Args:
        tool_definition (OpenAITool): The description of the tool
        function (Callable): The function to call
    """

    tool_definition: open_ai_models.OpenAITool
    function: Callable

ToolManager

Tool manager

This class manages the tools that can be called by the bot.

Source code in src/simple_openai/tool_manager.py
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
class ToolManager:
    """Tool manager

    This class manages the tools that can be called by the bot.
    """

    def __init__(self) -> None:
        self._tools: dict[str, OpenAIToolMapping] = {}

    def add_tool(
        self, tool_definition: open_ai_models.OpenAITool, function: Callable
    ) -> None:
        """Add a tool to the tool manager

        Args:
            tool_definition (OpenAITool): The tool definition
            function (Callable): The function to call
        """
        # Add the function to the function manager
        self._tools[tool_definition.function.name] = OpenAIToolMapping(
            tool_definition, function
        )

    def get_json_tool_list(self) -> list[open_ai_models.OpenAITool] | None:
        """Get the list of tools

        Returns:
            list[open_ai_models.OpenAITool] | None: The list of tools or None if there are no tools
        """
        # Get the list of functions
        tools = [tool.tool_definition for tool in self._tools.values()]

        if tools:
            # Return the list of functions
            return tools
        else:
            # Return None
            return None

    def call_function(self, function_name: str, **kwargs: dict[str, Any]) -> str:
        """Call a function

        Args:
            function_name (str): The name of the function to call
            **kwargs: The keyword arguments to pass to the function

        Returns:
            str: The result of the function
        """
        # Check that the function exists
        if function_name not in self._tools:
            # Return text to tell the bot it hallucinated the function
            return f"Tool {function_name} does not exist, please answer the last question again."
        else:
            # Call the function
            return self._tools[function_name].function(**kwargs)

    async def async_call_function(
        self, function_name: str, **kwargs: dict[str, Any]
    ) -> str:
        """Call a function

        Args:
            function_name (str): The name of the function to call
            **kwargs: The keyword arguments to pass to the function

        Returns:
            str: The result of the function
        """
        # Check that the function exists
        if function_name not in self._tools:
            # Return text to tell the bot it hallucinated the function
            return f"Function {function_name} does not exist, please answer the last question again."
        else:
            # Call the function
            return await self._tools[function_name].function(**kwargs)

add_tool(tool_definition, function)

Add a tool to the tool manager

Parameters:

Name Type Description Default
tool_definition OpenAITool

The tool definition

required
function Callable

The function to call

required
Source code in src/simple_openai/tool_manager.py
44
45
46
47
48
49
50
51
52
53
54
55
56
def add_tool(
    self, tool_definition: open_ai_models.OpenAITool, function: Callable
) -> None:
    """Add a tool to the tool manager

    Args:
        tool_definition (OpenAITool): The tool definition
        function (Callable): The function to call
    """
    # Add the function to the function manager
    self._tools[tool_definition.function.name] = OpenAIToolMapping(
        tool_definition, function
    )

async_call_function(function_name, **kwargs) async

Call a function

Parameters:

Name Type Description Default
function_name str

The name of the function to call

required
**kwargs dict[str, Any]

The keyword arguments to pass to the function

{}

Returns:

Name Type Description
str str

The result of the function

Source code in src/simple_openai/tool_manager.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
async def async_call_function(
    self, function_name: str, **kwargs: dict[str, Any]
) -> str:
    """Call a function

    Args:
        function_name (str): The name of the function to call
        **kwargs: The keyword arguments to pass to the function

    Returns:
        str: The result of the function
    """
    # Check that the function exists
    if function_name not in self._tools:
        # Return text to tell the bot it hallucinated the function
        return f"Function {function_name} does not exist, please answer the last question again."
    else:
        # Call the function
        return await self._tools[function_name].function(**kwargs)

call_function(function_name, **kwargs)

Call a function

Parameters:

Name Type Description Default
function_name str

The name of the function to call

required
**kwargs dict[str, Any]

The keyword arguments to pass to the function

{}

Returns:

Name Type Description
str str

The result of the function

Source code in src/simple_openai/tool_manager.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def call_function(self, function_name: str, **kwargs: dict[str, Any]) -> str:
    """Call a function

    Args:
        function_name (str): The name of the function to call
        **kwargs: The keyword arguments to pass to the function

    Returns:
        str: The result of the function
    """
    # Check that the function exists
    if function_name not in self._tools:
        # Return text to tell the bot it hallucinated the function
        return f"Tool {function_name} does not exist, please answer the last question again."
    else:
        # Call the function
        return self._tools[function_name].function(**kwargs)

get_json_tool_list()

Get the list of tools

Returns:

Type Description
list[OpenAITool] | None

list[open_ai_models.OpenAITool] | None: The list of tools or None if there are no tools

Source code in src/simple_openai/tool_manager.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def get_json_tool_list(self) -> list[open_ai_models.OpenAITool] | None:
    """Get the list of tools

    Returns:
        list[open_ai_models.OpenAITool] | None: The list of tools or None if there are no tools
    """
    # Get the list of functions
    tools = [tool.tool_definition for tool in self._tools.values()]

    if tools:
        # Return the list of functions
        return tools
    else:
        # Return None
        return None