Class: Boxcars::XMLTrain

Inherits:
Train show all
Defined in:
lib/boxcars/train/xml_train.rb

Overview

A Train using XML for prompting and execution.

Direct Known Subclasses

XMLZeroShot

Instance Attribute Summary

Attributes inherited from Train

#answer_prefix, #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

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, #question_text, #return_stopped_response, #should_continue?, #validate_prompt

Methods inherited from EngineBoxcar

#apply, #call, #check_output_keys, #extract_code, #generate, #input_key, #input_keys, #output_key, #output_keys, #predict, #prediction_input, #prediction_variables

Methods inherited from Boxcar

#apply, assi, #call, #conduct, hist, #input_keys, #load, #output_keys, #run, #save, #schema, syst, user, #validate_inputs, #validate_outputs

Constructor Details

#initialize(boxcars:, prompt:, engine: nil, **kwargs) ⇒ XMLTrain

This method is abstract.

A Train will use a engine to run a series of boxcars.

Parameters:

  • boxcars (Array<Boxcars::Boxcar>)

    The boxcars to run.

  • prompt (Boxcars::Prompt)

    The prompt to use.

  • engine (Boxcars::Engine) (defaults to: nil)

    The engine to use for this train.

  • kwargs (Hash)

    Additional arguments including: name, description, top_k, return_direct, and stop



15
16
17
18
# File 'lib/boxcars/train/xml_train.rb', line 15

def initialize(boxcars:, prompt:, engine: nil, **kwargs)
  @using_xml = true
  super
end

Instance Method Details

#boxcars_xmlObject

the xml to describe the boxcars



34
35
36
37
# File 'lib/boxcars/train/xml_train.rb', line 34

def boxcars_xml
  schema = boxcars.map(&:schema).join("\n")
  "<boxcars>\n#{schema}</boxcars>"
end

#build_output(text) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/boxcars/train/xml_train.rb', line 44

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



29
30
31
# File 'lib/boxcars/train/xml_train.rb', line 29

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.

Parameters:

  • text (String)

    The output from the engine.

Returns:



57
58
59
60
61
62
# File 'lib/boxcars/train/xml_train.rb', line 57

def extract_boxcar_and_input(text)
  get_action_and_input(engine_output: build_output(text))
rescue StandardError => e
  Boxcars.debug("Error: #{e.message}", :red)
  [:error, e.message]
end

#init_prefixesObject



20
21
22
23
24
25
26
27
# File 'lib/boxcars/train/xml_train.rb', line 20

def init_prefixes
  @thought_prefix ||= "<thought>"
  @observation_prefix ||= "<observation>"
  @final_answer_prefix ||= "<final_answer>"
  @answer_prefix ||= "<answer>"
  @question_prefix ||= "<question>"
  @output_prefix ||= "<output>"
end

#prediction_additional(_inputs) ⇒ Object

Returns Hash The additional variables for this boxcar.

Returns:

  • Hash The additional variables for this boxcar.



40
41
42
# File 'lib/boxcars/train/xml_train.rb', line 40

def prediction_additional(_inputs)
  { boxcars_xml: boxcars_xml, next_actions: next_actions }.merge super
end