Class: Boxcars::ZeroShot

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

Overview

A Train using the zero-shot react method.

Constant Summary collapse

FINAL_ANSWER_ACTION =

the final answer action string

"Final Answer:"
CTEMPLATE =
[
  syst("Answer the following questions as best you can. You have access to the following actions:\n",
       "%<boxcar_descriptions>s\n",
       "Use the following format:\n",
       "Question: the input question you must answer\n",
       "Thought: you should always think about what to do\n",
       "Action: the action to take, should be one from this list: %<boxcar_names>s\n",
       "Action Input: an input question to the action\n",
       "Observation: the result of the action\n",
       "... (this Thought/Action/Action Input/Observation sequence can repeat N times)\n",
       "Thought: I know the final answer\n",
       "Final Answer: the final answer to the original input question\n",
       "%<next_actions>s\n",
       "Remember to start a line with \"Final Answer:\" to give me the final answer.\n",
       "Also make sure to specify a question for the Action Input.\n",
       "Finally, if you can deduct the answer from the question or observation, you can ",
       "start with \"Final Answer:\" and give me the answer.\n",
       "Begin!"),
  # insert thoughts here from previous runs
  hist,
  user("Question: %<input>s"),
  assi("Thought: %<agent_scratchpad>s")
].freeze

Instance Attribute Summary collapse

Attributes inherited from Train

#answer_prefix, #early_stopping_method, #engine_prefix, #final_answer_prefix, #max_iterations, #name_to_boxcar_map, #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, #init_prefixes, #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:, engine: nil, name: 'Zero Shot', description: 'Zero Shot Train', prompt: nil, **kwargs) ⇒ ZeroShot

Returns a new instance of ZeroShot.

Parameters:

  • boxcars (Array<Boxcars::Boxcar>)

    The boxcars to run.

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

    The engine to use for this train.

  • name (String) (defaults to: 'Zero Shot')

    The name of the train. Defaults to ‘Zero Shot’.

  • description (String) (defaults to: 'Zero Shot Train')

    The description of the train. Defaults to ‘Zero Shot Train’.

  • prompt (Boxcars::Prompt) (defaults to: nil)

    The prompt to use. Defaults to the built-in prompt.

  • kwargs (Hash)

    Additional arguments to pass to the train. wants_next_actions: true



16
17
18
19
20
# File 'lib/boxcars/train/zero_shot.rb', line 16

def initialize(boxcars:, engine: nil, name: 'Zero Shot', description: 'Zero Shot Train', prompt: nil, **kwargs)
  @wants_next_actions = kwargs.fetch(:wants_next_actions, false)
  prompt ||= my_prompt
  super(engine:, boxcars:, prompt:, name:, description:, **kwargs)
end

Instance Attribute Details

#boxcarsObject (readonly)

Returns the value of attribute boxcars.



7
8
9
# File 'lib/boxcars/train/zero_shot.rb', line 7

def boxcars
  @boxcars
end

#observation_prefixObject (readonly)

Returns the value of attribute observation_prefix.



7
8
9
# File 'lib/boxcars/train/zero_shot.rb', line 7

def observation_prefix
  @observation_prefix
end

#wants_next_actionsObject

Returns the value of attribute wants_next_actions.



8
9
10
# File 'lib/boxcars/train/zero_shot.rb', line 8

def wants_next_actions
  @wants_next_actions
end

Instance Method Details

#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:



30
31
32
33
34
# File 'lib/boxcars/train/zero_shot.rb', line 30

def extract_boxcar_and_input(text)
  get_action_and_input(engine_output: text)
rescue StandardError => e
  [:error, e.message]
end

#prediction_additional(_inputs) ⇒ Object

Returns Hash The additional variables for this boxcar.

Returns:

  • Hash The additional variables for this boxcar.



23
24
25
# File 'lib/boxcars/train/zero_shot.rb', line 23

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