Class: Boxcars::Boxcar Abstract
- Inherits:
-
Object
- Object
- Boxcars::Boxcar
- Defined in:
- lib/boxcars/boxcar.rb
Overview
Direct Known Subclasses
EngineBoxcar, GoogleSearch, RubyCalculator, URLText, WikipediaSearch
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
-
#return_direct ⇒ Object
readonly
Returns the value of attribute return_direct.
Class Method Summary collapse
-
.assi(*strs) ⇒ Object
helpers for conversation prompt building assistant message.
-
.hist ⇒ Object
history entries.
-
.syst(*strs) ⇒ Object
system message.
-
.user(*strs) ⇒ Object
user message.
Instance Method Summary collapse
-
#apply(input_list:) ⇒ Array<Boxcars::Boxcar>
Apply the boxcar to a list of inputs.
-
#call(inputs:) ⇒ Object
Run the logic of this chain and return the output.
-
#conduct(*args, **kwargs) ⇒ Boxcars::Result
Get an extended answer from the boxcar.
-
#initialize(description:, name: nil, return_direct: false, parameters: nil) ⇒ Boxcar
constructor
A Boxcar is a container for a single tool to run.
-
#input_keys ⇒ Object
Input keys this chain expects.
-
#load(path:) ⇒ Object
load this boxcar from a file.
-
#output_keys ⇒ Object
Output keys this chain expects.
-
#run(*args, **kwargs) ⇒ String
Get an answer from the boxcar.
-
#save(path:) ⇒ Object
save this boxcar to a file.
- #schema ⇒ Object
-
#validate_inputs(inputs:) ⇒ Object
Check that all inputs are present.
-
#validate_outputs(outputs:) ⇒ Object
check that all outputs are present.
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
#description ⇒ Object (readonly)
Returns the value of attribute description.
6 7 8 |
# File 'lib/boxcars/boxcar.rb', line 6 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/boxcars/boxcar.rb', line 6 def name @name end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
6 7 8 |
# File 'lib/boxcars/boxcar.rb', line 6 def parameters @parameters end |
#return_direct ⇒ Object (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 |
.hist ⇒ Object
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.
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.
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.
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_keys ⇒ Object
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_keys ⇒ Object
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.
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 |
#schema ⇒ Object
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.
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
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 |