Class: RubyLLM::Tool
- Inherits:
-
Object
- Object
- RubyLLM::Tool
- Defined in:
- lib/ruby_llm/tool.rb
Overview
Represents a tool/function that can be called by an LLM
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#handler ⇒ Object
readonly
Returns the value of attribute handler.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
Class Method Summary collapse
Instance Method Summary collapse
- #call(args) ⇒ Object
-
#initialize(name:, description:, parameters: {}, &block) ⇒ Tool
constructor
A new instance of Tool.
Constructor Details
#initialize(name:, description:, parameters: {}, &block) ⇒ Tool
Returns a new instance of Tool.
41 42 43 44 45 46 47 48 |
# File 'lib/ruby_llm/tool.rb', line 41 def initialize(name:, description:, parameters: {}, &block) @name = name @description = description @parameters = parameters @handler = block validate! end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description.
6 7 8 |
# File 'lib/ruby_llm/tool.rb', line 6 def description @description end |
#handler ⇒ Object (readonly)
Returns the value of attribute handler.
6 7 8 |
# File 'lib/ruby_llm/tool.rb', line 6 def handler @handler end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/ruby_llm/tool.rb', line 6 def name @name end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
6 7 8 |
# File 'lib/ruby_llm/tool.rb', line 6 def parameters @parameters end |
Class Method Details
.from_method(method_object, description: nil, parameter_descriptions: {}) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ruby_llm/tool.rb', line 8 def self.from_method(method_object, description: nil, parameter_descriptions: {}) method_params = {} method_object.parameters.each do |param_type, param_name| next unless %i[req opt key keyreq].include?(param_type) method_params[param_name] = { type: 'string', description: parameter_descriptions[param_name] || param_name.to_s.tr('_', ' '), required: %i[req keyreq].include?(param_type) } end new( name: method_object.name.to_s, description: description || "Executes the #{method_object.name} operation", parameters: method_params ) do |args| # Create an instance if it's an instance method instance = if method_object.owner.instance_methods.include?(method_object.name) method_object.owner.new else method_object.owner end # Call the method with the arguments if args.is_a?(Hash) instance.method(method_object.name).call(**args) else instance.method(method_object.name).call(args) end end end |
Instance Method Details
#call(args) ⇒ Object
50 51 52 53 |
# File 'lib/ruby_llm/tool.rb', line 50 def call(args) validated_args = validate_args!(args) handler.call(validated_args) end |