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.



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.

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

#conductBoxcars::Result

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



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

def conduct(*, **)
  Boxcars.info "> Entering #{name}#run", :gray, style: :bold
  rv = depart(*, **)
  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

#runString

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



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

def run(*, **)
  rv = conduct(*, **)
  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")
  "    <tool name=\"\#{name}\" description=\"\#{description}\">\n      <params>\n        \#{params}\n      </params>\n    </tool>\n  SCHEMA\nend\n".freeze

#validate_inputs(inputs:) ⇒ Object

Check that all inputs are present.

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

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