Tools Module
tools
Tool registration and management for OpenAI function calling.
This module provides a registry for AI tools with automatic schema generation, validation, and execution capabilities for use with OpenAI's function calling API.
ToolError
Bases: Exception
Exception raised for errors in tool registration or execution.
This exception is raised when there are issues with tool registration, schema generation, or during tool execution.
ToolRegistry
Registry for AI tools with automatic schema generation.
This class implements a singleton pattern to maintain a global registry of tools that can be called by the OpenAI API. It handles tool registration, schema generation, and tool execution.
Source code in openai_toolchain/tools.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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
|
__new__()
Create a new instance or return the existing singleton instance.
Returns:
Name | Type | Description |
---|---|---|
ToolRegistry |
ToolRegistry
|
The singleton instance of ToolRegistry. |
Source code in openai_toolchain/tools.py
call_tool(name, arguments, non_ai_params=None)
Call a registered tool by name with the given arguments.
This method executes a registered tool with the provided arguments and returns the result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the tool to call. |
required |
arguments
|
Dict[str, Any]
|
A dictionary of arguments to pass to the tool. |
required |
non_ai_params
|
Optional[Dict[str, Any]]
|
Optional dictionary of non-AI parameters to pass to the tool. These parameters are not part of the AI's function calling interface but can be used to pass system-level dependencies. |
None
|
Returns:
Type | Description |
---|---|
Any
|
The result of the tool execution. |
Raises:
Type | Description |
---|---|
ToolError
|
If the tool is not found or if there's an error during execution. |
Source code in openai_toolchain/tools.py
clear()
get_openai_tools()
Gets all registered tools in OpenAI function calling format.
This method converts all registered tools to the format expected by the
OpenAI API for function calling. The resulting list can be passed directly
to the OpenAI API's tools
parameter.
Returns:
Name | Type | Description |
---|---|---|
list |
List[Dict[str, Any]]
|
A list of tools in OpenAI function calling format. Each tool is a dictionary with 'type' and 'function' keys, where 'function' contains the tool's name, description, and parameters schema. |
Example
@tool_registry.register ... def get_weather(location: str) -> str: ... return f"Weather in {location}: Sunny" tools = tool_registry.get_openai_tools() print(tools[0]['function']['name']) get_weather
Source code in openai_toolchain/tools.py
get_tool(name)
Get a registered tool by name.
This method retrieves a tool's metadata and function from the registry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the tool to retrieve. |
required |
Returns:
Type | Description |
---|---|
Optional[Dict[str, Any]]
|
dict or None: The tool's metadata and function, or None if not found. |
Source code in openai_toolchain/tools.py
register(func=None, *, name=None, **kwargs)
Register a function as a tool.
This method can be used as a decorator with or without arguments. It registers the function with the tool registry and generates the necessary OpenAPI schema for OpenAI function calling.
Examples:
>>> @tool_registry.register
... def my_tool(param: str) -> str:
... return f"Processed {param}"
>>> @tool_registry.register(name="custom_name")
... def another_tool():
... pass
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Optional[Callable[..., Any]]
|
The function to register (automatically passed when used as decorator). |
None
|
name
|
Optional[str]
|
Optional custom name for the tool. If not provided, the function's name will be used. |
None
|
**kwargs
|
Any
|
Additional metadata to include with the tool registration. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
Callable |
Union[Callable[[F], F], F]
|
The decorated function or a decorator function if called without a function argument. |
Raises:
Type | Description |
---|---|
ToolError
|
If there's an issue with the tool registration. |
Source code in openai_toolchain/tools.py
tool(func_or_name=None, **kwargs)
Register a function as a tool with the global registry.
This decorator can be used with or without arguments to register a function as a tool with the global tool registry.
Examples: