Class: Boxcars::Prompt

Inherits:
Object
  • Object
show all
Defined in:
lib/boxcars/prompt.rb

Overview

used by Boxcars that have engine’s to create a prompt.

Direct Known Subclasses

ConversationPrompt

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template:, input_variables: nil, other_inputs: nil, output_variables: nil) ⇒ Prompt

Returns a new instance of Prompt.

Parameters:

  • template (String)

    The template to use for the prompt.

  • input_variables (Array<Symbol>) (defaults to: nil)

    The input vars to use for the prompt. Defaults to [:input]

  • other_inputs (Array<Symbol>) (defaults to: nil)

    The other input vars to use for the prompt. Defaults to []

  • output_variables (Array<Symbol>) (defaults to: nil)

    The output vars to use for the prompt. Defaults to [:output]



12
13
14
15
16
17
# File 'lib/boxcars/prompt.rb', line 12

def initialize(template:, input_variables: nil, other_inputs: nil, output_variables: nil)
  @template = template
  @input_variables = input_variables || [:input]
  @other_inputs = other_inputs || []
  @output_variables = output_variables || [:output]
end

Instance Attribute Details

#input_variablesObject (readonly)

Returns the value of attribute input_variables.



6
7
8
# File 'lib/boxcars/prompt.rb', line 6

def input_variables
  @input_variables
end

#other_inputsObject (readonly)

Returns the value of attribute other_inputs.



6
7
8
# File 'lib/boxcars/prompt.rb', line 6

def other_inputs
  @other_inputs
end

#output_variablesObject (readonly)

Returns the value of attribute output_variables.



6
7
8
# File 'lib/boxcars/prompt.rb', line 6

def output_variables
  @output_variables
end

#templateObject (readonly)

Returns the value of attribute template.



6
7
8
# File 'lib/boxcars/prompt.rb', line 6

def template
  @template
end

Instance Method Details

#as_messages(inputs) ⇒ Hash

compute the prompt parameters with input substitutions

Parameters:

  • inputs (Hash)

    The inputs to use for the prompt.

Returns:

  • (Hash)

    The formatted prompt { prompt: “…”}



31
32
33
# File 'lib/boxcars/prompt.rb', line 31

def as_messages(inputs)
  { messages: [{ role: :user, content: format(inputs) }] }
end

#as_prompt(inputs: nil, prefixes: nil, show_roles: nil) ⇒ Hash

compute the prompt parameters with input substitutions (used for chatGPT) rubocop:disable Lint/UnusedMethodArgument

Parameters:

  • inputs (Hash) (defaults to: nil)

    The inputs to use for the prompt.

Returns:

  • (Hash)

    The formatted prompt { messages: …}



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

def as_prompt(inputs: nil, prefixes: nil, show_roles: nil)
  { prompt: format(inputs) }
end

#default_prefixesObject



44
45
# File 'lib/boxcars/prompt.rb', line 44

def default_prefixes
end

#with_conversation(conversation) ⇒ Object

tack on the ongoing conversation if present to the prompt



36
37
38
39
40
41
42
# File 'lib/boxcars/prompt.rb', line 36

def with_conversation(conversation)
  return self unless conversation

  new_prompt = dup
  new_prompt.template += "\n\n#{conversation.message_text}"
  new_prompt
end