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_intelligence_conversation(inputs: nil) ⇒ Intelligence::Conversation

Convert the prompt to an Intelligence::Conversation

Parameters:

  • inputs (Hash) (defaults to: nil)

    The inputs to use for the prompt

Returns:

  • (Intelligence::Conversation)

    The converted conversation



53
54
55
56
57
58
59
60
# File 'lib/boxcars/prompt.rb', line 53

def as_intelligence_conversation(inputs: nil)
  conversation = Intelligence::Conversation.new
  user_msg = Intelligence::Message.new(:user)
  user_msg << Intelligence::MessageContent::Text.new(text: format(inputs))
  conversation.messages << user_msg

  conversation
end

#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



47
48
# File 'lib/boxcars/prompt.rb', line 47

def default_prefixes
end

#format(inputs) ⇒ String

format the prompt with the input variables

Parameters:

  • inputs (Hash)

    The inputs to use for the prompt.

Returns:

  • (String)

    The formatted prompt.

Raises:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/boxcars/prompt.rb', line 66

def format(inputs)
  # Ensure all input keys are symbols for consistent lookup
  symbolized_inputs = inputs.transform_keys(&:to_sym)

  # Use sprintf for templates like "hi %<name>s"
  # Ensure that all keys expected by the template are present in symbolized_inputs
  template_keys = @template.scan(/%<(\w+)>s/).flatten.map(&:to_sym)
  missing_keys = template_keys - symbolized_inputs.keys
  raise ::KeyError, "missing keys: #{missing_keys.join(', ')}" if missing_keys.any?

  # Perform the substitution
  @template % symbolized_inputs
rescue ::KeyError => e
  first_line = e.message.to_s.split("\n").first
  Boxcars.error "Prompt format error: #{first_line}" # Changed message slightly for clarity
  raise KeyError, "Prompt format error: #{first_line}"
rescue ArgumentError => e # Catch sprintf errors e.g. "too many arguments for format string"
  first_line = e.message.to_s.split("\n").first
  Boxcars.error "Prompt format error: #{first_line}"
  raise ArgumentError, "Prompt format error: #{first_line}"
end

#with_conversation(conversation) ⇒ Object

tack on the ongoing conversation if present to the prompt



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

def with_conversation(conversation)
  return self unless conversation

  Prompt.new(
    template: "#{template}\n\n#{conversation.message_text}",
    input_variables: input_variables,
    other_inputs: other_inputs,
    output_variables: output_variables
  )
end