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
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.
34
35
36
|
# File 'lib/ruby_llm/tool.rb', line 34
def params_schema_definition
@params_schema_definition
end
|
Class Method Details
.description(text = nil) ⇒ Object
36
37
38
39
40
|
# File 'lib/ruby_llm/tool.rb', line 36
def description(text = nil)
return @description unless text
@description = text
end
|
.param(name, **options) ⇒ Object
42
43
44
|
# File 'lib/ruby_llm/tool.rb', line 42
def param(name, **options)
parameters[name] = Parameter.new(name, **options)
end
|
.parameters ⇒ Object
46
47
48
|
# File 'lib/ruby_llm/tool.rb', line 46
def parameters
@parameters ||= {}
end
|
.params(schema = nil, &block) ⇒ Object
50
51
52
53
|
# File 'lib/ruby_llm/tool.rb', line 50
def params(schema = nil, &block)
@params_schema_definition = SchemaDefinition.new(schema:, block:)
self
end
|
.provider_params ⇒ Object
60
61
62
|
# File 'lib/ruby_llm/tool.rb', line 60
def provider_params
@provider_params ||= {}
end
|
.with_params(**params) ⇒ Object
55
56
57
58
|
# File 'lib/ruby_llm/tool.rb', line 55
def with_params(**params)
@provider_params = params
self
end
|
Instance Method Details
#call(args) ⇒ Object
101
102
103
104
105
106
|
# File 'lib/ruby_llm/tool.rb', line 101
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
end
|
#description ⇒ Object
76
77
78
|
# File 'lib/ruby_llm/tool.rb', line 76
def description
self.class.description
end
|
#execute ⇒ Object
108
109
110
|
# File 'lib/ruby_llm/tool.rb', line 108
def execute(...)
raise NotImplementedError, 'Subclasses must implement #execute'
end
|
#name ⇒ Object
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/ruby_llm/tool.rb', line 65
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
80
81
82
|
# File 'lib/ruby_llm/tool.rb', line 80
def parameters
self.class.parameters
end
|
#params_schema ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/ruby_llm/tool.rb', line 88
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
end
end
end
|
#provider_params ⇒ Object
84
85
86
|
# File 'lib/ruby_llm/tool.rb', line 84
def provider_params
self.class.provider_params
end
|