ツール ― モデルに手を持たせる
言語モデルはテキストを書くだけです。今日の天気も、データベースの中の注文も、お店の値段も調べられません。知っているのは、訓練で学んだことだけです。ツールがそれを変えます。Rubyのクラスを説明しておくと、モデルはそれを呼び出すように頼めます。RubyLLMがそれを実行して結果を送り返し、モデルはその結果を使って答えを書きます。
ツールはRubyLLM::Toolのサブクラスで、description(何のためのものか。モデルが読みます)、parameterで書くパラメーター、そしてexecuteメソッドを持ちます:
install_gem "ruby_llm"
require "ruby_llm"
require "chunky_llm" # Chunky Mini、このページの中の小さなモデル
class Weather < RubyLLM::Tool
description "Gets the current weather for a city"
parameter :city, description: "Name of the city, such as Bern"
def execute(city:)
reports = { "Bern" => "sunny, 21 °C", "Lugano" => "rain, 17 °C", "Basel" => "cloudy, 19 °C" }
reports.fetch(city) { { error: "No weather data for #{city}" } }
end
end
chat = RubyLLM.chat.with_tools(Weather)
chat.ask("What's the weather in Bern?").contentこの天気観測所はただのハッシュですが、executeは本物の天気サービスに問い合わせてもかまいません。何が起きたのでしょう? メッセージを見るとわかります:
chat.messages.each do |m|
calls = m.tool_calls.to_h.values.map { |call| "#{call.name}(#{call.arguments})" }
puts "#{m.role.to_s.ljust(9)} #{calls.join(', ')} #{m.content}"
end
nil1つの質問に、メッセージが4つ:
user:質問。assistant:テキストはなく、ツール呼び出しがあります。モデルはcity: "Bern"でweatherを呼ぶように頼んでいます。ツールは説明を見て選び、引数は質問から取りました。tool:RubyLLMがWeather#executeを実行し、その結果をチャットに入れました。assistant:その結果を使って、モデルが答えを書きます。
RubyLLMは、モデルがツールを求めずに答えるまで、このループを自分で回します。ツールに合わない質問では、呼び出しは起きません:
chat.ask("Who created Ruby?").contentモデルは複数のツールを一度に呼ぶことも、1つのツールを何度も呼ぶこともあります。ここでは都市ごとに1回です:
RubyLLM.chat.with_tools(Weather).ask("What's the weather in Basel and Lugano?").contentループを見守る
before_tool_callとafter_tool_resultはコールバックです。チャットがツールのたびにその前後で呼ぶブロックです。アプリはこれを使って「天気を調べています…」と表示したり、モデルが何をしたかを記録したりします:
log = []
watched = RubyLLM.chat.with_tools(Weather)
watched.before_tool_call { |call| log << "calls #{call.name} with #{call.arguments}" }
watched.after_tool_result { |result| log << "gets #{result.inspect}" }
watched.ask("Is it raining in Atlantis?")
logアトランティスに天気はないので、executeは{ error: ... }を返します。想定内の失敗には、これが正しいやり方です。プログラムが落ちる代わりに、モデルがエラーを読んで、ユーザーに伝えられます。
型のあるパラメーター
型がなければ、パラメーターは文字列です。type: :integer、:number、:booleanで、モデルに何を送ればいいかを伝えます。RubyLLMはツールをJSON Schemaで記述します:
class Dice < RubyLLM::Tool
description "Rolls a die and returns the number it shows"
parameter :sides, type: :integer, description: "How many sides the die has"
def execute(sides:)
rand(1..sides)
end
end
puts Dice.new.parameters_schema
RubyLLM.chat.with_tools(Dice).ask("Roll a die with 20 sides!").contentエージェント
同じモデル、同じ指示、同じツールを、アプリのあちこちで使いたい? それならエージェントにまとめましょう。チャットをどう設定するかを書いたクラスです。newで、それが全部そろったチャットができます:
class WeatherDesk < RubyLLM::Agent
instructions "You answer questions about the weather in one short sentence."
tools Weather
end
WeatherDesk.new.ask("Weather in Basel?").content本物のツールは外の世界に手を伸ばします。Faraday(RubyLLMといっしょに入ります)で呼ぶ天気API、Active Recordのモデル、検索など。ツールはモデルが決めた引数で動くので、フォームの入力と同じようにチェックしましょう。何かを削除したり支払ったりするツールにはrequires_approvalを付けます。するとチャットは、人が「はい」と言うのを待ちます。bin/rails generate ruby_llm:tool Weatherはツールのクラスを、ruby_llm:agentはエージェントを書きます。RubyLLMはMCPサーバーのツールも使えますし(RubyLLM::MCP)、モデルにサンドボックスの中でコードを実行させることもできます(code_execution)。
:numberのパラメーター:kilosを持つツールShippingを書いてください。executeは送料を返します。7フランに、1キロごとに2フランを足した額です。それから、このツールを持たせたチャットに「What does shipping a 3 kg parcel cost?」と聞きます。セルの結果はその返事です。install_gem "ruby_llm"
require "ruby_llm"
require "chunky_llm" # Chunky Mini、このページの中の小さなモデル
# class Shipping < RubyLLM::Tool
# description "..."
# parameter :kilos, type: :number, description: "..."
#
# def execute(kilos:)
# end
# end