class Toolbox
Manages a collection of tools and dispatches calls to them.
Definitions
def initialize
Initializes a new, empty toolbox.
Implementation
def initialize
@tools = {}
end
attr :tools
Signature
-
attribute
Hash
The registered tools by name.
def register(name, schema, &block)
Registers a new tool with the given name, schema, and block.
Signature
-
parameter
name
String
The name of the tool.
-
parameter
schema
Hash
The schema describing the tool's function.
-
parameter
block
Proc
The implementation of the tool.
Implementation
def register(name, schema, &block)
@tools[name] = Tool.new(name, schema, &block)
end
def call(message)
Calls a registered tool with the given message.
Signature
-
parameter
message
Hash
The message containing the function call.
-
returns
Hash
The tool's response message.
Implementation
def call(message)
function = message[:function]
name = function[:name]
if tool = @tools[name]
arguments = function[:arguments]
result = tool.call(arguments)
return {
role: "tool",
tool_name: name,
content: result.to_json,
}
else
raise ArgumentError.new("Unknown tool: #{name}")
end
rescue => error
return {
role: "tool",
tool_name: name,
error: error.inspect,
}
end
def explain
Signature
-
returns
Array(Hash)
The explanations for all registered tools.
Implementation
def explain
@tools.values.map(&:explain)
end