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



7
8
9
10
11
# File 'lib/ruby_llm/tool.rb', line 7

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

  @description = text
end

.nameObject



27
28
29
30
31
32
33
# File 'lib/ruby_llm/tool.rb', line 27

def name
  super
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .downcase
    .delete_suffix('_tool')
end

.param(name, type:, desc: nil, required: true) ⇒ Object



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

def param(name, type:, desc: nil, required: true)
  param = Parameter.new(
    name,
    type: type.to_s,
    description: desc,
    required: required
  )
  parameters[name] = param
end

.parametersObject



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

def parameters
  @parameters ||= {}
end

.to_toolObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ruby_llm/tool.rb', line 35

def to_tool
  tool_instance = new

  def tool_instance.name
    self.class.name
  end

  def tool_instance.description
    self.class.description
  end

  def tool_instance.parameters
    self.class.parameters
  end

  tool_instance
end

Instance Method Details

#call(args) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/ruby_llm/tool.rb', line 54

def call(args)
  RubyLLM.logger.debug "Tool #{name} called with: #{args.inspect}"
  symbolized_args = args.transform_keys(&:to_sym)
  result = execute(**symbolized_args)
  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

#execute(args) ⇒ Object

Raises:

  • (NotImplementedError)


65
66
67
# File 'lib/ruby_llm/tool.rb', line 65

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