Class: Boxcars::XMLTrain
- Inherits:
-
Train
- Object
- Boxcar
- EngineBoxcar
- Train
- Boxcars::XMLTrain
- Defined in:
- lib/boxcars/train/xml_train.rb
Overview
A Train using XML for prompting and execution.
Direct Known Subclasses
Constant Summary
Constants inherited from Boxcar
Boxcar::SCHEMA_KEY_ALIASES, Boxcar::TYPE_ALIASES
Instance Attribute Summary
Attributes inherited from Train
#boxcars, #early_stopping_method, #engine_prefix, #final_answer_prefix, #max_iterations, #name_to_boxcar_map, #observation_prefix, #question_prefix, #return_intermediate_steps, #return_values, #thought_prefix, #using_xml
Attributes inherited from EngineBoxcar
#engine, #prompt, #stop, #top_k
Attributes inherited from Boxcar
#description, #name, #parameters, #return_direct
Instance Method Summary collapse
-
#boxcars_xml ⇒ Object
the xml to describe the boxcars.
- #build_output(text) ⇒ Object
- #close_tag(tag) ⇒ Object
-
#extract_boxcar_and_input(text) ⇒ Array<Boxcars::Boxcar, String>
Extract the boxcar and input from the engine output.
- #init_prefixes ⇒ Object
-
#initialize(boxcars:, prompt:, engine: nil, **kwargs) ⇒ XMLTrain
constructor
abstract
A Train will use a engine to run a series of boxcars.
-
#prediction_additional(_inputs) ⇒ Object
Hash The additional variables for this boxcar.
Methods inherited from Train
#boxcar_descriptions, #boxcar_names, #call, #construct_scratchpad, #finish_boxcar_name, #get_boxcar_result, #get_next_action, #input_keys, #key_and_value_text, #next_actions, #observation_text, #output_keys, #plan, #pre_return, #prepare_for_new_call, #return_stopped_response, #should_continue?
Methods inherited from EngineBoxcar
#apply, #call, #extract_code, #generate, #input_keys, #output_key, #output_keys, #predict, #prediction_variables
Methods inherited from Boxcar
#apply, assi, #call, #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(boxcars:, prompt:, engine: nil, **kwargs) ⇒ XMLTrain
A Train will use a engine to run a series of boxcars.
13 14 15 16 |
# File 'lib/boxcars/train/xml_train.rb', line 13 def initialize(boxcars:, prompt:, engine: nil, **kwargs) @using_xml = true super end |
Instance Method Details
#boxcars_xml ⇒ Object
the xml to describe the boxcars
31 32 33 34 |
# File 'lib/boxcars/train/xml_train.rb', line 31 def boxcars_xml schema = boxcars.map(&:schema).join("\n") "<boxcars>\n#{schema}</boxcars>" end |
#build_output(text) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/boxcars/train/xml_train.rb', line 41 def build_output(text) if text.end_with?("</usetool>") "<data>#{engine_prefix}#{text}</output></data>" elsif text =~ /#{close_tag(thought_prefix)}/ "<data>#{engine_prefix}#{text}</data>" else "<data>#{text}</data>" end end |
#close_tag(tag) ⇒ Object
26 27 28 |
# File 'lib/boxcars/train/xml_train.rb', line 26 def close_tag(tag) tag.to_s.sub("<", "</") if tag.to_s[0] == "<" end |
#extract_boxcar_and_input(text) ⇒ Array<Boxcars::Boxcar, String>
Extract the boxcar and input from the engine output.
54 55 56 57 58 59 |
# File 'lib/boxcars/train/xml_train.rb', line 54 def extract_boxcar_and_input(text) get_action_and_input(engine_output: build_output(text)) rescue StandardError => e Boxcars.debug("Error: #{e.}", :red) [:error, e.] end |
#init_prefixes ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/boxcars/train/xml_train.rb', line 18 def init_prefixes @thought_prefix ||= "<thought>" @observation_prefix ||= "<observation>" @final_answer_prefix ||= "<final_answer>" @question_prefix ||= "<question>" @output_prefix ||= "<output>" end |
#prediction_additional(_inputs) ⇒ Object
Returns Hash The additional variables for this boxcar.
37 38 39 |
# File 'lib/boxcars/train/xml_train.rb', line 37 def prediction_additional(_inputs) { boxcars_xml:, next_actions: }.merge super end |