Class: LLM::Tool
- Inherits:
-
Object
- Object
- LLM::Tool
- Extended by:
- 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
-
.clear_registry! ⇒ void
Clear the registry.
-
.description(desc = nil) ⇒ String
Returns (or sets) the tool description.
- .function ⇒ Object private
-
.inherited(tool) ⇒ void
Registers the tool as a function when inherited.
- .lock ⇒ Object private
-
.mcp(mcp, tool) ⇒ Class<LLM::Tool>
Returns a subclass of LLM::Tool.
-
.mcp? ⇒ Boolean
Returns true if the tool is an MCP tool.
-
.name(name = nil) ⇒ String
Returns (or sets) the tool name.
-
.params {|schema| ... } ⇒ LLM::Schema
Returns (or sets) tool parameters.
-
.register(tool) ⇒ Object
private
Register a tool in the registry.
-
.registry ⇒ Array<LLM::Tool>
Returns all subclasses of LLM::Tool.
-
.unregister(tool) ⇒ Object
private
Unregister a tool from the registry.
Methods included from Param
Class Method Details
.clear_registry! ⇒ void
This method returns an undefined value.
Clear the registry
79 80 81 82 83 84 |
# File 'lib/llm/tool.rb', line 79 def self.clear_registry! lock do @registry.clear nil end end |
.description(desc = nil) ⇒ String
Returns (or sets) the tool description
132 133 134 135 136 |
# File 'lib/llm/tool.rb', line 132 def self.description(desc = nil) lock do desc ? function.description(desc) : function.description 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.
150 151 152 153 154 |
# File 'lib/llm/tool.rb', line 150 def self.function lock do @function ||= LLM::Function.new(nil) end end |
.inherited(tool) ⇒ void
This method returns an undefined value.
Registers the tool as a function when inherited
110 111 112 113 114 115 116 |
# File 'lib/llm/tool.rb', line 110 def self.inherited(tool) LLM.lock(:inherited) do tool.instance_eval { @__monitor ||= Monitor.new } tool.function.register(tool) LLM::Tool.register(tool) end end |
.lock ⇒ 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.
158 159 160 |
# File 'lib/llm/tool.rb', line 158 def self.lock(&) @__monitor.synchronize(&) end |
.mcp(mcp, tool) ⇒ Class<LLM::Tool>
Returns a subclass of LLM::Tool
41 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 41 def self.mcp(mcp, tool) 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 unregister(klass) end |
.mcp? ⇒ Boolean
Returns true if the tool is an MCP tool
166 167 168 |
# File 'lib/llm/tool.rb', line 166 def self.mcp? false end |
.name(name = nil) ⇒ String
Returns (or sets) the tool name
122 123 124 125 126 |
# File 'lib/llm/tool.rb', line 122 def self.name(name = nil) lock do name ? function.name(name) : function.name end end |
.params {|schema| ... } ⇒ LLM::Schema
Returns (or sets) tool parameters
142 143 144 145 146 |
# File 'lib/llm/tool.rb', line 142 def self.params(&) lock do function.tap { _1.params(&) } 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.
Register a tool in the registry
90 91 92 93 94 |
# File 'lib/llm/tool.rb', line 90 def self.register(tool) lock do @registry << tool end end |
.registry ⇒ Array<LLM::Tool>
This method excludes tools who haven’t defined a name as well as tools defined via MCP.
Returns all subclasses of LLM::Tool
69 70 71 72 73 |
# File 'lib/llm/tool.rb', line 69 def self.registry lock do @registry.select(&:name) end 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
100 101 102 103 104 |
# File 'lib/llm/tool.rb', line 100 def self.unregister(tool) lock do @registry.delete(tool) end end |