Class: Boxcars::Boxcar Abstract

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

Overview

This class is abstract.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(description:, name: nil, return_direct: false, parameters: nil) ⇒ Boxcar

A Boxcar is a container for a single tool to run.

Parameters:

  • name (String) (defaults to: nil)

    The name of the boxcar. Defaults to classname.

  • description (String)

    A description of the boxcar.

  • return_direct (Boolean) (defaults to: false)

    If true, return the output of this boxcar directly, without merging it with the inputs.

  • parameters (Hash) (defaults to: nil)

    The parameters for this boxcar.



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

def initialize(description:, name: nil, return_direct: false, parameters: nil)
  @name = name || self.class.name
  @description = description || @name
  @return_direct = return_direct
  @parameters = parameters || { question: { type: :string, description: "the input question", required: true } }
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



6
7
8
# File 'lib/boxcars/boxcar.rb', line 6

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/boxcars/boxcar.rb', line 6

def name
  @name
end

#parametersObject (readonly)

Returns the value of attribute parameters.



6
7
8
# File 'lib/boxcars/boxcar.rb', line 6

def parameters
  @parameters
end

#return_directObject (readonly)

Returns the value of attribute return_direct.



6
7
8
# File 'lib/boxcars/boxcar.rb', line 6

def return_direct
  @return_direct
end

Class Method Details

.assi(*strs) ⇒ Object

helpers for conversation prompt building assistant message



90
91
92
# File 'lib/boxcars/boxcar.rb', line 90

def self.assi(*strs)
  [:assistant, strs.join]
end

.histObject

history entries



105
106
107
# File 'lib/boxcars/boxcar.rb', line 105

def self.hist
  [:history, ""]
end

.syst(*strs) ⇒ Object

system message



95
96
97
# File 'lib/boxcars/boxcar.rb', line 95

def self.syst(*strs)
  [:system, strs.join]
end

.user(*strs) ⇒ Object

user message



100
101
102
# File 'lib/boxcars/boxcar.rb', line 100

def self.user(*strs)
  [:user, strs.join]
end

Instance Method Details

#apply(input_list:) ⇒ Array<Boxcars::Boxcar>

Apply the boxcar to a list of inputs.

Parameters:

  • input_list (Array<Hash>)

    The list of inputs.

Returns:

Raises:

  • (NotImplementedError)


57
58
59
# File 'lib/boxcars/boxcar.rb', line 57

def apply(input_list:)
  raise NotImplementedError
end

#call(inputs:) ⇒ Object

Run the logic of this chain and return the output.

Raises:

  • (NotImplementedError)


50
51
52
# File 'lib/boxcars/boxcar.rb', line 50

def call(inputs:)
  raise NotImplementedError
end

#conduct(*args, **kwargs) ⇒ Boxcars::Result

Get an extended answer from the boxcar. you can pass one or the other, but not both.

Parameters:

  • args (Array)

    The positional arguments to pass to the boxcar.

  • kwargs (Hash)

    The keyword arguments to pass to the boxcar.

Returns:



80
81
82
83
84
85
86
# File 'lib/boxcars/boxcar.rb', line 80

def conduct(*args, **kwargs)
  Boxcars.info "> Entering #{name}#run", :gray, style: :bold
  rv = depart(*args, **kwargs)
  remember_history(rv)
  Boxcars.info "< Exiting #{name}#run", :gray, style: :bold
  rv
end

#input_keysObject

Input keys this chain expects.



21
22
23
# File 'lib/boxcars/boxcar.rb', line 21

def input_keys
  [:question]
end

#load(path:) ⇒ Object

load this boxcar from a file



115
116
117
# File 'lib/boxcars/boxcar.rb', line 115

def load(path:)
  YAML.load_file(path)
end

#output_keysObject

Output keys this chain expects.



26
27
28
# File 'lib/boxcars/boxcar.rb', line 26

def output_keys
  [:answer]
end

#run(*args, **kwargs) ⇒ String

Get an answer from the boxcar. you can pass one or the other, but not both.

Parameters:

  • args (Array)

    The positional arguments to pass to the boxcar.

  • kwargs (Hash)

    The keyword arguments to pass to the boxcar.

Returns:

  • (String)

    The answer to the question.



66
67
68
69
70
71
72
73
# File 'lib/boxcars/boxcar.rb', line 66

def run(*args, **kwargs)
  rv = conduct(*args, **kwargs)
  rv = rv[:answer] if rv.is_a?(Hash) && rv.key?(:answer)
  return rv.answer if rv.is_a?(Result)
  return rv[output_keys[0]] if rv.is_a?(Hash)

  rv
end

#save(path:) ⇒ Object

save this boxcar to a file



110
111
112
# File 'lib/boxcars/boxcar.rb', line 110

def save(path:)
  File.write(path, YAML.dump(self))
end

#schemaObject



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/boxcars/boxcar.rb', line 119

def schema
  params = parameters.map do |name, info|
    "<param name=#{name.to_s.inspect} data-type=#{info[:type].to_s.inspect} required=\"#{info[:required] == true}\" " \
      "description=#{info[:description].inspect} />"
  end.join("\n")
  <<~SCHEMA.freeze
    <tool name="#{name}" description="#{description}">
      <params>
        #{params}
      </params>
    </tool>
  SCHEMA
end

#validate_inputs(inputs:) ⇒ Object

Check that all inputs are present.

Parameters:

  • inputs (Hash)

    The inputs.

Raises:

  • (RuntimeError)

    If the inputs are not the same.



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

def validate_inputs(inputs:)
  missing_keys = input_keys - inputs.keys
  raise "Missing some input keys: #{missing_keys}" if missing_keys.any?

  inputs
end

#validate_outputs(outputs:) ⇒ Object

check that all outputs are present

Parameters:

  • outputs (Array<String>)

    The output keys.

Raises:

  • (RuntimeError)

    If the outputs are not the same.



43
44
45
46
47
# File 'lib/boxcars/boxcar.rb', line 43

def validate_outputs(outputs:)
  return if (outputs - output_keys - ['log']).empty?

  raise "Did not get output keys that were expected, got: #{outputs}. Expected: #{output_keys}"
end