Class: Boxcars::JSONEngineBoxcar
- Inherits:
-
EngineBoxcar
- Object
- Boxcar
- EngineBoxcar
- Boxcars::JSONEngineBoxcar
- Defined in:
- lib/boxcars/boxcar/json_engine_boxcar.rb
Overview
For Boxcars that use an engine to do their work.
Instance Attribute Summary collapse
-
#data_description ⇒ Object
A JSON Engine Boxcar is a container for a single tool to run.
-
#important ⇒ Object
A JSON Engine Boxcar is a container for a single tool to run.
-
#symbolize ⇒ Object
A JSON Engine Boxcar is a container for a single tool to run.
-
#wanted_data ⇒ Object
A JSON Engine Boxcar is a container for a single tool to run.
Attributes inherited from EngineBoxcar
#engine, #prompt, #stop, #top_k
Attributes inherited from Boxcar
#description, #name, #parameters, #return_direct
Instance Method Summary collapse
- #default_prompt ⇒ Object
-
#extract_answer(data) ⇒ Result
get answer from parsed JSON.
-
#get_answer(engine_output) ⇒ Result
Parse out the action and input from the engine output.
-
#initialize(prompt: nil, wanted_data: nil, data_description: nil, important: nil, symbolize: false, **kwargs) ⇒ JSONEngineBoxcar
constructor
A new instance of JSONEngineBoxcar.
Methods inherited from EngineBoxcar
#apply, #call, #check_output_keys, #extract_code, #generate, #input_key, #input_keys, #output_key, #output_keys, #predict, #prediction_additional, #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(prompt: nil, wanted_data: nil, data_description: nil, important: nil, symbolize: false, **kwargs) ⇒ JSONEngineBoxcar
Returns a new instance of JSONEngineBoxcar.
16 17 18 19 20 21 22 23 24 |
# File 'lib/boxcars/boxcar/json_engine_boxcar.rb', line 16 def initialize(prompt: nil, wanted_data: nil, data_description: nil, important: nil, symbolize: false, **kwargs) @wanted_data = wanted_data || "summarize the pertinent facts from the input data" @data_description = data_description || "the input data" @important = important the_prompt = prompt || default_prompt kwargs[:description] ||= "JSON Engine Boxcar" @symbolize = symbolize super(prompt: the_prompt, **kwargs) end |
Instance Attribute Details
#data_description ⇒ Object
A JSON Engine Boxcar is a container for a single tool to run.
8 9 10 |
# File 'lib/boxcars/boxcar/json_engine_boxcar.rb', line 8 def data_description @data_description end |
#important ⇒ Object
A JSON Engine Boxcar is a container for a single tool to run.
8 9 10 |
# File 'lib/boxcars/boxcar/json_engine_boxcar.rb', line 8 def important @important end |
#symbolize ⇒ Object
A JSON Engine Boxcar is a container for a single tool to run.
8 9 10 |
# File 'lib/boxcars/boxcar/json_engine_boxcar.rb', line 8 def symbolize @symbolize end |
#wanted_data ⇒ Object
A JSON Engine Boxcar is a container for a single tool to run.
8 9 10 |
# File 'lib/boxcars/boxcar/json_engine_boxcar.rb', line 8 def wanted_data @wanted_data end |
Instance Method Details
#default_prompt ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/boxcars/boxcar/json_engine_boxcar.rb', line 26 def default_prompt stock_prompt = <<~SYSPR I will provide you with %<data_description>s. Your job is to extract information as described below. Your Output must be valid JSON with no lead in or post answer text in the output format below: Output Format: { %<wanted_data>s } SYSPR stock_prompt += "\n\nImportant:\n#{important}\n" if important.present? sprompt = format(stock_prompt, wanted_data: wanted_data, data_description: data_description) ctemplate = [ Boxcar.syst(sprompt), Boxcar.user("%<input>s") ] conv = Conversation.new(lines: ctemplate) ConversationPrompt.new(conversation: conv, input_variables: [:input], other_inputs: [], output_variables: [:answer]) end |
#extract_answer(data) ⇒ Result
get answer from parsed JSON
66 67 68 69 |
# File 'lib/boxcars/boxcar/json_engine_boxcar.rb', line 66 def extract_answer(data) reply = data Result.new(status: :ok, answer: reply, explanation: reply) end |
#get_answer(engine_output) ⇒ Result
Parse out the action and input from the engine output.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/boxcars/boxcar/json_engine_boxcar.rb', line 52 def get_answer(engine_output) json_string = extract_json(engine_output) reply = JSON.parse(json_string, symbolize_names: symbolize) Result.new(status: :ok, answer: reply, explanation: reply) rescue JSON::ParserError => e Boxcars.debug "JSON: #{engine_output}", :red Result.from_error("JSON parsing error: #{e.}") rescue StandardError => e Result.from_error("Unexpected error: #{e.}") end |