Class: Boxcars::RubyCalculator

Inherits:
Boxcar
  • Object
show all
Defined in:
lib/boxcars/boxcar/ruby_calculator.rb

Overview

A Boxcar that executes Ruby code to do math.

Constant Summary collapse

CALCDESC =

Default description for this boxcar.

"will run a ruby calculation to answer a math question"

Constants inherited from Boxcar

Boxcar::SCHEMA_KEY_ALIASES, Boxcar::TYPE_ALIASES

Instance Attribute Summary

Attributes inherited from Boxcar

#description, #name, #parameters, #return_direct

Instance Method Summary collapse

Methods inherited from Boxcar

#apply, assi, #conduct, #conduct_result, hist, #input_keys, #output_keys, #parameters_json_schema, #run, #run_result, #schema, syst, #tool_call_name, #tool_definition, #tool_spec, user, #validate_inputs, #validate_outputs

Constructor Details

#initialize(**kwargs) ⇒ RubyCalculator

Returns a new instance of RubyCalculator.

Parameters:

  • kwargs (Hash)

    Any other keyword arguments to pass to the parent class.



11
12
13
14
15
16
17
# File 'lib/boxcars/boxcar/ruby_calculator.rb', line 11

def initialize(**kwargs)
  kwargs[:name] ||= "RubyCalculator"
  kwargs[:description] ||= CALCDESC
  kwargs[:parameters] ||= default_params

  super
end

Instance Method Details

#call(inputs:) ⇒ Hash

Execute one Ruby calculator request using the normalized Boxcar input contract.

Parameters:

  • inputs (Hash)

    Expected to contain ‘:question` (or `“question”`).

Returns:

  • (Hash)

    ‘{ answer: Boxcars::Result }`.



32
33
34
35
36
37
# File 'lib/boxcars/boxcar/ruby_calculator.rb', line 32

def call(inputs:)
  question = inputs[:question]
  code = "puts(#{question})"
  ruby_executor = Boxcars::RubyREPL.new
  { answer: ruby_executor.call(code:) }
end

#default_paramsHash

Default JSON-like parameter description for tool wiring.

Returns:

  • (Hash)

    Parameter descriptor hash keyed by ‘:question`.



21
22
23
24
25
26
27
# File 'lib/boxcars/boxcar/ruby_calculator.rb', line 21

def default_params
  { question: {
      type: :string,
      description: "a Ruby programming string that will compute the answer to a math question",
      required: true
      } }
end