Class: Boxcars::JSONEngineBoxcar

Inherits:
EngineBoxcar show all
Defined in:
lib/boxcars/boxcar/json_engine_boxcar.rb

Overview

For Boxcars that use an engine to do their work.

Instance Attribute Summary collapse

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 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.

Parameters:

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

    The prompt to use for this boxcar with sane defaults.

  • wanted_data (String) (defaults to: nil)

    The data to extract from.

  • data_description (String) (defaults to: nil)

    The description of the data.

  • important (String) (defaults to: nil)

    Any important instructions you want to give the LLM.

  • symbolize (Boolean) (defaults to: false)

    Symbolize the JSON results if true

  • kwargs (Hash)

    Additional arguments



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_descriptionObject

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

#importantObject

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

#symbolizeObject

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_dataObject

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_promptObject



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

Parameters:

  • data (Hash)

    The data to extract from.

Returns:



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.

Parameters:

  • engine_output (String)

    The output from the engine.

Returns:



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.message}")
rescue StandardError => e
  Result.from_error("Unexpected error: #{e.message}")
end