Class: LLM::Tool
- Inherits:
-
Object
- Object
- LLM::Tool
- Extended by:
- Function::Registry, Param
- Defined in:
- lib/llm/tool.rb,
lib/llm/tool/param.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.
Defined Under Namespace
Modules: Param
Class Method Summary collapse
-
.a2a(a2a, skill) ⇒ Class<LLM::Tool>
Returns a subclass of LLM::Tool.
-
.clear_registry! ⇒ void
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) ⇒ void
Registers the tool as a function when inherited.
-
.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.
-
.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.
-
#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
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/llm/tool.rb', line 70 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! ⇒ void
This method returns an undefined value.
Clear the registry
97 98 99 100 101 102 |
# File 'lib/llm/tool.rb', line 97 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.
108 109 110 111 |
# File 'lib/llm/tool.rb', line 108 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
117 118 119 120 121 122 123 |
# File 'lib/llm/tool.rb', line 117 def self.unregister(tool) lock do LLM::Function.unregister(tool.function) super tool end end |
.inherited(tool) ⇒ void
This method returns an undefined value.
Registers the tool as a function when inherited
129 130 131 132 133 134 135 |
# File 'lib/llm/tool.rb', line 129 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 |
.name(name = nil) ⇒ String
Returns (or sets) the tool name
141 142 143 144 145 |
# File 'lib/llm/tool.rb', line 141 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.
150 151 152 |
# File 'lib/llm/tool.rb', line 150 def self.registry super.select(&:name) end |
.find_by_name!(name) ⇒ Class<LLM::Tool>
Finds a registered tool by name.
159 160 161 |
# File 'lib/llm/tool.rb', line 159 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
167 168 169 170 171 |
# File 'lib/llm/tool.rb', line 167 def self.description(desc = nil) lock do desc ? function.description(desc) : function.description end end |
.params {|schema| ... } ⇒ LLM::Schema
Returns (or sets) tool parameters
177 178 179 180 181 |
# File 'lib/llm/tool.rb', line 177 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.
185 186 187 188 189 |
# File 'lib/llm/tool.rb', line 185 def self.function lock do @function ||= LLM::Function.new(nil) end end |
.mcp? ⇒ Boolean
Returns true if the tool is an MCP tool
194 195 196 |
# File 'lib/llm/tool.rb', line 194 def self.mcp? false end |
.a2a? ⇒ Boolean
Returns true if the tool is an A2A tool
201 202 203 |
# File 'lib/llm/tool.rb', line 201 def self.a2a? false end |
.mcp(mcp, tool) ⇒ Class<LLM::Tool>
Returns a subclass of LLM::Tool
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/llm/tool.rb', line 42 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.
229 230 231 |
# File 'lib/llm/tool.rb', line 229 def on_cancel on_interrupt end |
#function ⇒ LLM::Function
Returns a function bound to this tool instance.
208 209 210 |
# File 'lib/llm/tool.rb', line 208 def function @function ||= self.class.function.dup.tap { _1.define(self) } end |
#mcp? ⇒ Boolean
Returns true if the tool is an MCP tool
215 216 217 |
# File 'lib/llm/tool.rb', line 215 def mcp? self.class.mcp? end |
#on_interrupt ⇒ nil
Called when an in-flight tool run is interrupted. Tools can override this to implement cooperative cleanup.
223 224 |
# File 'lib/llm/tool.rb', line 223 def on_interrupt end |