Class: RubyLLM::Tool

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

Overview

Base class for creating tools that AI models can use

Defined Under Namespace

Classes: Halt, SchemaDefinition

Constant Summary collapse

POSITIONAL_PARAMETER_KINDS =
%i[req opt rest].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.params_schema_definitionObject (readonly)

Returns the value of attribute params_schema_definition.



36
37
38
# File 'lib/ruby_llm/tool.rb', line 36

def params_schema_definition
  @params_schema_definition
end

Class Method Details

.descObject



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

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

  @description = text
end

.description(text = nil) ⇒ Object



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

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

  @description = text
end

.param(name, **options) ⇒ Object



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

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

.parametersObject



49
50
51
# File 'lib/ruby_llm/tool.rb', line 49

def parameters
  @parameters ||= {}
end

.params(schema = nil, &block) ⇒ Object



53
54
55
56
# File 'lib/ruby_llm/tool.rb', line 53

def params(schema = nil, &block)
  @params_schema_definition = SchemaDefinition.new(schema:, block:)
  self
end

.provider_paramsObject



63
64
65
# File 'lib/ruby_llm/tool.rb', line 63

def provider_params
  @provider_params ||= {}
end

.with_params(**params) ⇒ Object



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

def with_params(**params)
  @provider_params = params
  self
end

Instance Method Details

#call(args) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/ruby_llm/tool.rb', line 106

def call(args)
  normalized_args = normalize_args(args)
  validation_error = validate_keyword_arguments(normalized_args)
  return { error: "Invalid tool arguments: #{validation_error}" } if validation_error

  RubyLLM.logger.debug { "Tool #{name} called with: #{normalized_args.inspect}" }
  result = execute(**normalized_args)
  RubyLLM.logger.debug { "Tool #{name} returned: #{result.inspect}" }
  result
end

#descriptionObject



79
80
81
# File 'lib/ruby_llm/tool.rb', line 79

def description
  self.class.description
end

#executeObject

Raises:

  • (NotImplementedError)


117
118
119
# File 'lib/ruby_llm/tool.rb', line 117

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

#nameObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/ruby_llm/tool.rb', line 68

def name
  klass_name = self.class.name
  normalized = klass_name.to_s.dup.force_encoding('UTF-8').unicode_normalize(:nfkd)
  normalized.encode('ASCII', replace: '')
            .gsub(/[^a-zA-Z0-9_-]/, '-')
            .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
            .gsub(/([a-z\d])([A-Z])/, '\1_\2')
            .downcase
            .delete_suffix('_tool')
end

#parametersObject



83
84
85
# File 'lib/ruby_llm/tool.rb', line 83

def parameters
  self.class.parameters
end

#params_schemaObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ruby_llm/tool.rb', line 91

def params_schema
  return @params_schema if defined?(@params_schema)

  @params_schema = begin
    definition = self.class.params_schema_definition
    if definition&.present?
      definition.json_schema
    elsif parameters.any?
      SchemaDefinition.from_parameters(parameters)&.json_schema
    else
      SchemaDefinition.from_parameters(inferred_parameters, allow_empty: true)&.json_schema
    end
  end
end

#provider_paramsObject



87
88
89
# File 'lib/ruby_llm/tool.rb', line 87

def provider_params
  self.class.provider_params
end