Class: DSPy::Tools::Toolset::ToolProxy
Overview
Inner class that wraps a method as a tool, compatible with DSPy::Tools::Base interface
Instance Method Summary
collapse
Methods inherited from Base
call_schema, #call_schema, call_schema_object, tool_description, tool_name
Constructor Details
#initialize(instance, method_name, tool_name, description) ⇒ ToolProxy
Returns a new instance of ToolProxy.
100
101
102
103
104
105
|
# File 'lib/dspy/tools/toolset.rb', line 100
def initialize(instance, method_name, tool_name, description)
@instance = instance
@method_name = method_name
@tool_name_override = tool_name
@description_override = description
end
|
Instance Method Details
#call(**kwargs) ⇒ Object
130
131
132
|
# File 'lib/dspy/tools/toolset.rb', line 130
def call(**kwargs)
@instance.send(@method_name, **kwargs)
end
|
#description ⇒ Object
113
114
115
|
# File 'lib/dspy/tools/toolset.rb', line 113
def description
@description_override
end
|
#dynamic_call(args_json) ⇒ Object
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# File 'lib/dspy/tools/toolset.rb', line 135
def dynamic_call(args_json)
schema = @instance.class.schema_for_method(@method_name)
if schema[:properties].empty?
@instance.send(@method_name)
else
args = case args_json
when Hash
args_json
when String
begin
JSON.parse(args_json)
rescue JSON::ParserError
return "Error: Invalid JSON input"
end
else
return "Error: Expected Hash or JSON string"
end
kwargs = {}
method_obj = @instance.class.instance_method(@method_name)
sig_info = T::Utils.signature_for_method(method_obj)
if sig_info
sig_info.kwarg_types.each do |param_name, param_type|
next if param_name == :block
key = param_name.to_s
if args.key?(key)
kwargs[param_name] = coerce_value_to_type(args[key], param_type)
elsif schema[:required].include?(key)
return "Error: Missing required parameter: #{key}"
end
end
sig_info.arg_types.each do |param_name, param_type|
next if param_name == :block
key = param_name.to_s
if args.key?(key)
kwargs[param_name] = coerce_value_to_type(args[key], param_type)
elsif schema[:required].include?(key)
return "Error: Missing required parameter: #{key}"
end
end
end
@instance.send(@method_name, **kwargs)
end
rescue => e
"Error: #{e.message}"
end
|
#name ⇒ Object
108
109
110
|
# File 'lib/dspy/tools/toolset.rb', line 108
def name
@tool_name_override
end
|
#schema ⇒ Object
118
119
120
121
122
123
124
125
126
|
# File 'lib/dspy/tools/toolset.rb', line 118
def schema
schema_obj = @instance.class.schema_for_method(@method_name)
tool_info = {
name: name,
description: description,
parameters: schema_obj
}
JSON.generate(tool_info)
end
|