Class: RubyLLM::Tool
- Inherits:
-
Object
- Object
- RubyLLM::Tool
- Defined in:
- lib/ruby_llm/tool.rb
Overview
Base class for creating tools that AI models can use. Provides a simple interface for defining parameters and implementing tool behavior.
Example:
class Calculator < RubyLLM::Tool
description "Performs arithmetic calculations"
param :expression, type: :string, desc: "Math expression to evaluate"
def execute(expression:)
eval(expression).to_s
end
end
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.description(text = nil) ⇒ Object
31 32 33 34 35 |
# File 'lib/ruby_llm/tool.rb', line 31 def description(text = nil) return @description unless text @description = text end |
.param(name, **options) ⇒ Object
37 38 39 |
# File 'lib/ruby_llm/tool.rb', line 37 def param(name, **) parameters[name] = Parameter.new(name, **) end |
.parameters ⇒ Object
41 42 43 |
# File 'lib/ruby_llm/tool.rb', line 41 def parameters @parameters ||= {} end |
Instance Method Details
#call(args) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/ruby_llm/tool.rb', line 62 def call(args) RubyLLM.logger.debug "Tool #{name} called with: #{args.inspect}" result = execute(**args.transform_keys(&:to_sym)) RubyLLM.logger.debug "Tool #{name} returned: #{result.inspect}" result rescue StandardError => e RubyLLM.logger.error "Tool #{name} failed with error: #{e.}" { error: e. } end |
#description ⇒ Object
54 55 56 |
# File 'lib/ruby_llm/tool.rb', line 54 def description self.class.description end |
#execute ⇒ Object
72 73 74 |
# File 'lib/ruby_llm/tool.rb', line 72 def execute(...) raise NotImplementedError, 'Subclasses must implement #execute' end |
#name ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/ruby_llm/tool.rb', line 46 def name self.class.name .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .downcase .delete_suffix('_tool') end |
#parameters ⇒ Object
58 59 60 |
# File 'lib/ruby_llm/tool.rb', line 58 def parameters self.class.parameters end |