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
-
.mcp? ⇒ Boolean
Returns true if the tool is an MCP 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(mcp, tool) ⇒
Class<LLM::Tool>
Returns a subclass of LLM::Tool.
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
.mcp? ⇒ Boolean
Returns true if the tool is an MCP tool
171 172 173 |
# File 'lib/llm/tool.rb', line 171 def self.mcp? false end |
.clear_registry! ⇒ void
This method returns an undefined value.
Clear the registry
74 75 76 77 78 79 |
# File 'lib/llm/tool.rb', line 74 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.
85 86 87 88 |
# File 'lib/llm/tool.rb', line 85 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
94 95 96 97 98 99 100 |
# File 'lib/llm/tool.rb', line 94 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
106 107 108 109 110 111 112 |
# File 'lib/llm/tool.rb', line 106 def self.inherited(tool) LLM.lock(:inherited) do tool.instance_eval { @__monitor ||= Monitor.new } tool.function.register(tool) LLM::Tool.register(tool) unless lock { @mcp } end end |
.name(name = nil) ⇒ String
Returns (or sets) the tool name
118 119 120 121 122 |
# File 'lib/llm/tool.rb', line 118 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.
127 128 129 |
# File 'lib/llm/tool.rb', line 127 def self.registry super.select(&:name) end |
.find_by_name!(name) ⇒ Class<LLM::Tool>
Finds a registered tool by name.
136 137 138 |
# File 'lib/llm/tool.rb', line 136 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
144 145 146 147 148 |
# File 'lib/llm/tool.rb', line 144 def self.description(desc = nil) lock do desc ? function.description(desc) : function.description end end |
.params {|schema| ... } ⇒ LLM::Schema
Returns (or sets) tool parameters
154 155 156 157 158 |
# File 'lib/llm/tool.rb', line 154 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.
162 163 164 165 166 |
# File 'lib/llm/tool.rb', line 162 def self.function lock do @function ||= LLM::Function.new(nil) end 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 62 63 64 65 66 67 68 69 |
# File 'lib/llm/tool.rb', line 42 def self.mcp(mcp, tool) lock do @mcp = true klass = Class.new(LLM::Tool) do name tool["name"] description tool["description"] params { tool["inputSchema"] || {type: "object", properties: {}} } define_singleton_method(:inspect) do "<LLM::Tool:0x#{object_id.to_s(16)} 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 @mcp = false register(klass) klass ensure @mcp = false end end |