Implement Tool Calling (Function Calling)
mainTo allow an LLM to use tools, define a class inheriting from OmniAI::Tool. Use the description and parameter DSL to define the tool's interface. Pass an array of tool instances to the tools: option in client.chat.
Tool Definition Pattern:
description "..."sets the tool's purpose.parameter :name, :type, description: "..."defines inputs.required %i[param1 param2]specifies mandatory parameters.def execute(...)contains the logic to run when the tool is called.
class WeatherTool < OmniAI::Tool
description "Lookup the weather for a lat / lng."
parameter :lat, :number, description: "The latitude of the location."
parameter :lng, :number, description: "The longitude of the location."
parameter :unit, :string, enum: %w[Celsius Fahrenheit], description: "The unit of measurement."
required %i[lat lng]
def execute(lat:, lng:, unit: "Celsius")
"#{rand(20..50)}° #{unit} at lat=#{lat} lng=#{lng}"
end
end
# Usage in chat
client.chat(stream: $stdout, tools: [WeatherTool.new]) do |prompt|
prompt.user 'What is the weather in "London" in Celsius?'
end