Class: Boxcars::ConversationPrompt

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

Overview

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

Instance Attribute Summary collapse

Attributes inherited from Prompt

#input_variables, #other_inputs, #output_variables, #template

Instance Method Summary collapse

Constructor Details

#initialize(conversation:, input_variables: nil, other_inputs: nil, output_variables: nil) ⇒ ConversationPrompt

Returns a new instance of ConversationPrompt.

Parameters:

  • conversation (Boxcars::Conversation)

    The conversation 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
# File 'lib/boxcars/conversation_prompt.rb', line 12

def initialize(conversation:, input_variables: nil, other_inputs: nil, output_variables: nil)
  @conversation = conversation
  super(template: template, input_variables: input_variables, other_inputs: other_inputs, output_variables: output_variables)
end

Instance Attribute Details

#conversationObject (readonly)

Returns the value of attribute conversation.



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

def conversation
  @conversation
end

Instance Method Details

#add_history(history) ⇒ Object

add conversation history to the prompt

Parameters:

  • history (Hash)

    The history to add to the prompt.



42
43
44
# File 'lib/boxcars/conversation_prompt.rb', line 42

def add_history(history)
  conversation.add_history(Conversation.new(lines: history))
end

#as_messages(inputs) ⇒ Hash

prompt for chatGPT params

Parameters:

  • inputs (Hash)

    The inputs to use for the prompt.

Returns:

  • (Hash)

    The formatted prompt.



20
21
22
# File 'lib/boxcars/conversation_prompt.rb', line 20

def as_messages(inputs)
  conversation.as_messages(inputs)
end

#as_prompt(inputs:, prefixes: default_prefixes, show_roles: false) ⇒ Hash

prompt for non chatGPT params

Parameters:

  • inputs (Hash)

    The inputs to use for the prompt.

Returns:

  • (Hash)

    The formatted prompt.



27
28
29
# File 'lib/boxcars/conversation_prompt.rb', line 27

def as_prompt(inputs:, prefixes: default_prefixes, show_roles: false)
  { prompt: conversation.as_prompt(inputs: inputs, prefixes: prefixes, show_roles: show_roles) }
end

#default_prefixesObject



51
52
53
# File 'lib/boxcars/conversation_prompt.rb', line 51

def default_prefixes
  conversation.default_prefixes
end

#to_sObject

print the prompt



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

def to_s
  conversation.to_s
end

#with_conversation(conversation) ⇒ Object

tack on the ongoing conversation if present to the prompt



32
33
34
35
36
37
38
# File 'lib/boxcars/conversation_prompt.rb', line 32

def with_conversation(conversation)
  return self unless conversation

  new_prompt = dup
  new_prompt.conversation.add_conversation(conversation)
  new_prompt
end