Class: RubyLLM::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/tool.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.description(text = nil) ⇒ Object



17
18
19
20
21
# File 'lib/ruby_llm/tool.rb', line 17

def description(text = nil)
  return @description unless text

  @description = text
end

.param(name, **options) ⇒ Object



23
24
25
# File 'lib/ruby_llm/tool.rb', line 23

def param(name, **options)
  parameters[name] = Parameter.new(name, **options)
end

.parametersObject



27
28
29
# File 'lib/ruby_llm/tool.rb', line 27

def parameters
  @parameters ||= {}
end

Instance Method Details

#call(args) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/ruby_llm/tool.rb', line 48

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.message}"
  { error: e.message }
end

#descriptionObject



40
41
42
# File 'lib/ruby_llm/tool.rb', line 40

def description
  self.class.description
end

#executeObject

Raises:

  • (NotImplementedError)


58
59
60
# File 'lib/ruby_llm/tool.rb', line 58

def execute(...)
  raise NotImplementedError, 'Subclasses must implement #execute'
end

#nameObject



32
33
34
35
36
37
38
# File 'lib/ruby_llm/tool.rb', line 32

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

#parametersObject



44
45
46
# File 'lib/ruby_llm/tool.rb', line 44

def parameters
  self.class.parameters
end