Class: LLM::Tool
- Inherits:
-
Object
- Object
- LLM::Tool
- Extended by:
- Function::Registry, Param
- Defined in:
- lib/llm/tool.rb,
lib/llm/tools/ls.rb,
lib/llm/tools/rg.rb,
lib/llm/tools/git.rb,
lib/llm/tools/pwd.rb,
lib/llm/tool/param.rb,
lib/llm/tools/ruby.rb,
lib/llm/tools/chdir.rb,
lib/llm/tools/mkdir.rb,
lib/llm/tools/shell.rb,
lib/llm/tools/utils.rb,
lib/llm/tools/which.rb,
lib/llm/tools/edit-file.rb,
lib/llm/tools/read_file.rb,
lib/llm/tools/write_file.rb
Overview
The LLM::Tool class represents a local tool that can be called by an LLM. Under the hood, it is a wrapper around LLM::Function but allows the definition of a function (also known as a tool) as a class.
Direct Known Subclasses
Chdir, EditFile, Git, Ls, Mkdir, Pwd, ReadFile, Rg, Ruby, Shell, Which, WriteFile
Defined Under Namespace
Modules: Param, Utils Classes: Chdir, EditFile, Git, Ls, Mkdir, Pwd, ReadFile, Rg, Ruby, Shell, Which, WriteFile
Class Method Summary collapse
-
.a2a(a2a, skill) ⇒ Class<LLM::Tool>
Returns a subclass of LLM::Tool.
-
.clear_registry!
Clear the registry.
-
.register(tool) ⇒ Object
private
Registers a tool and its function.
-
.unregister(tool) ⇒ Object
private
Unregister a tool from the registry.
-
.inherited(tool)
Registers the tool as a function when inherited.
-
.set(properties)
Assign multiple tool properties at once.
-
.name(name = nil) ⇒ String
Returns (or sets) the tool name.
-
.registry ⇒ Array<Class<LLM::Tool>>
Returns all registered tool classes with definitions.
-
.find_by_name!(name) ⇒ Class<LLM::Tool>
Finds a registered tool by name.
-
.description(desc = nil) ⇒ String
Returns (or sets) the tool description.
-
.params {|schema| ... } ⇒ LLM::Schema
Returns (or sets) tool parameters.
- .function ⇒ Object private
-
.mcp? ⇒ Boolean
Returns true if the tool is an MCP tool.
-
.a2a? ⇒ Boolean
Returns true if the tool is an A2A tool.
-
.skill? ⇒ Boolean
Returns true if the tool is a skill.
-
.mcp(mcp, tool) ⇒ Class<LLM::Tool>
Returns a subclass of LLM::Tool.
Instance Method Summary collapse
-
#on_cancel ⇒ nil
Called when an in-flight tool run is cancelled.
-
#function ⇒ LLM::Function
Returns a function bound to this tool instance.
-
#mcp? ⇒ Boolean
Returns true if the tool is an MCP tool.
-
#a2a? ⇒ Boolean
Returns true if the tool is an A2A tool.
-
#skill? ⇒ Boolean
Returns true if the tool is a skill.
-
#on_interrupt ⇒ nil
Called when an in-flight tool run is interrupted.
Methods included from Param
Methods included from Function::Registry
clear_registry!, find_by_name, lock, register, registry, registry_key, tool_name, unregister
Class Method Details
.a2a(a2a, skill) ⇒ Class<LLM::Tool>
Returns a subclass of LLM::Tool
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/llm/tool.rb', line 86 def self.a2a(a2a, skill) name = skill.name.gsub(" ", "-") Class.new(LLM::Tool) do name(name) description(skill.description) parameter :input, String, "The input string" required %i[input] define_singleton_method(:inspect) do "<#{LLM::Utils.object_id(self)} name=#{name} (a2a)>" end singleton_class.alias_method :to_s, :inspect define_singleton_method(:a2a?) do true end define_method(:call) do |input:| res = a2a.(input) {task: res} end end end |
.clear_registry!
This method returns an undefined value.
Clear the registry
113 114 115 116 117 118 |
# File 'lib/llm/tool.rb', line 113 def self.clear_registry! lock do @__registry.each_value { LLM::Function.unregister(_1.function) } super end end |
.register(tool) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Registers a tool and its function.
124 125 126 127 |
# File 'lib/llm/tool.rb', line 124 def self.register(tool) super LLM::Function.register(tool.function) end |
.unregister(tool) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Unregister a tool from the registry
133 134 135 136 137 138 139 |
# File 'lib/llm/tool.rb', line 133 def self.unregister(tool) lock do LLM::Function.unregister(tool.function) super tool end end |
.inherited(tool)
This method returns an undefined value.
Registers the tool as a function when inherited
145 146 147 148 149 150 151 |
# File 'lib/llm/tool.rb', line 145 def self.inherited(tool) LLM.lock(:inherited) do tool.instance_eval { @__monitor ||= Monitor.new } tool.function.define(tool) LLM::Tool.register(tool) unless tool.mcp? || tool.a2a? end end |
.set(properties)
This method returns an undefined value.
Assign multiple tool properties at once.
168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/llm/tool.rb', line 168 def self.set(properties) properties.each do if _1.to_s == "parameters" _2.each { |attributes| parameter(*attributes) } else if respond_to?(_1) public_send(_1, _2) else raise KeyError, "key not found: #{_1}" end end end end |
.name(name = nil) ⇒ String
Returns (or sets) the tool name
186 187 188 189 190 |
# File 'lib/llm/tool.rb', line 186 def self.name(name = nil) lock do name ? function.name(name) : function.name end end |
.registry ⇒ Array<Class<LLM::Tool>>
Returns all registered tool classes with definitions.
195 196 197 |
# File 'lib/llm/tool.rb', line 195 def self.registry super.select(&:name) end |
.find_by_name!(name) ⇒ Class<LLM::Tool>
Finds a registered tool by name.
204 205 206 |
# File 'lib/llm/tool.rb', line 204 def self.find_by_name!(name) find_by_name(name) || raise(LLM::NoSuchToolError, "no such tool #{name.inspect}") end |
.description(desc = nil) ⇒ String
Returns (or sets) the tool description
212 213 214 215 216 |
# File 'lib/llm/tool.rb', line 212 def self.description(desc = nil) lock do desc ? function.description(desc) : function.description end end |
.params {|schema| ... } ⇒ LLM::Schema
Returns (or sets) tool parameters
222 223 224 225 226 |
# File 'lib/llm/tool.rb', line 222 def self.params(&) lock do function.tap { _1.params(&) } end end |
.function ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
230 231 232 233 234 |
# File 'lib/llm/tool.rb', line 230 def self.function lock do @function ||= LLM::Function.new(nil) end end |
.mcp? ⇒ Boolean
Returns true if the tool is an MCP tool
239 240 241 |
# File 'lib/llm/tool.rb', line 239 def self.mcp? false end |
.a2a? ⇒ Boolean
Returns true if the tool is an A2A tool
246 247 248 |
# File 'lib/llm/tool.rb', line 246 def self.a2a? false end |
.skill? ⇒ Boolean
Returns true if the tool is a skill
253 254 255 |
# File 'lib/llm/tool.rb', line 253 def self.skill? false end |
.mcp(mcp, tool) ⇒ Class<LLM::Tool>
Returns a subclass of LLM::Tool
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/llm/tool.rb', line 58 def self.mcp(mcp, tool) Class.new(LLM::Tool) do name tool["name"] description tool["description"] params { tool["inputSchema"] || {type: "object", properties: {}} } define_singleton_method(:inspect) do "<#{LLM::Utils.object_id(self)} name=#{tool["name"]} (mcp)>" end singleton_class.alias_method :to_s, :inspect define_singleton_method(:mcp?) do true end define_method(:call) do |**args| mcp.call_tool(tool["name"], args) end end end |
Instance Method Details
#on_cancel ⇒ nil
Called when an in-flight tool run is cancelled.
295 296 297 |
# File 'lib/llm/tool.rb', line 295 def on_cancel on_interrupt end |
#function ⇒ LLM::Function
Returns a function bound to this tool instance.
260 261 262 |
# File 'lib/llm/tool.rb', line 260 def function @function ||= self.class.function.dup.tap { _1.define(self) } end |
#mcp? ⇒ Boolean
Returns true if the tool is an MCP tool
267 268 269 |
# File 'lib/llm/tool.rb', line 267 def mcp? self.class.mcp? end |
#a2a? ⇒ Boolean
Returns true if the tool is an A2A tool
274 275 276 |
# File 'lib/llm/tool.rb', line 274 def a2a? self.class.a2a? end |
#skill? ⇒ Boolean
Returns true if the tool is a skill
281 282 283 |
# File 'lib/llm/tool.rb', line 281 def skill? self.class.skill? end |
#on_interrupt ⇒ nil
Called when an in-flight tool run is interrupted. Tools can override this to implement cooperative cleanup.
289 290 |
# File 'lib/llm/tool.rb', line 289 def on_interrupt end |