Class: RubyLLM::Tool
- Inherits:
-
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_definition ⇒ Object
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
.desc ⇒ Object
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
|
.parameters ⇒ Object
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_params ⇒ Object
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
|
#description ⇒ Object
79
80
81
|
# File 'lib/ruby_llm/tool.rb', line 79
def description
self.class.description
end
|
#execute ⇒ Object
117
118
119
|
# File 'lib/ruby_llm/tool.rb', line 117
def execute(...)
raise NotImplementedError, 'Subclasses must implement #execute'
end
|
#name ⇒ Object
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
|
#parameters ⇒ Object
83
84
85
|
# File 'lib/ruby_llm/tool.rb', line 83
def parameters
self.class.parameters
end
|
#params_schema ⇒ Object
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_params ⇒ Object
87
88
89
|
# File 'lib/ruby_llm/tool.rb', line 87
def provider_params
self.class.provider_params
end
|