Sha256: 8e557cc6db4e2aade05f8fd01b8b2de6efb104cd3796ff628a059870f5c841e2

Contents?: true

Size: 1.79 KB

Versions: 5

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

module Boxcars
  # used by Boxcars that have engine's to create a conversation prompt.
  class ConversationPrompt < Prompt
    attr_reader :conversation

    # @param conversation [Boxcars::Conversation] The conversation to use for the prompt.
    # @param input_variables [Array<Symbol>] The input vars to use for the prompt. Defaults to [:input]
    # @param other_inputs [Array<Symbol>] The other input vars to use for the prompt. Defaults to []
    # @param output_variables [Array<Symbol>] The output vars to use for the prompt. Defaults to [:output]
    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

    # prompt for chatGPT params
    # @param inputs [Hash] The inputs to use for the prompt.
    # @return [Hash] The formatted prompt.
    def as_messages(inputs)
      conversation.as_messages(inputs)
    end

    # prompt for non chatGPT params
    # @param inputs [Hash] The inputs to use for the prompt.
    # @return [Hash] The formatted prompt.
    def as_prompt(inputs)
      { prompt: conversation.as_prompt(inputs) }
    end

    # tack on the ongoing conversation if present to the prompt
    def with_conversation(conversation)
      return self unless conversation

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

    # add conversation history to the prompt
    # @param history [Hash] The history to add to the prompt.
    def add_history(history)
      conversation.add_history(Conversation.new(lines: history))
    end

    # print the prompt
    def to_s
      conversation.to_s
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
boxcars-0.3.5 lib/boxcars/conversation_prompt.rb
boxcars-0.3.4 lib/boxcars/conversation_prompt.rb
boxcars-0.3.3 lib/boxcars/conversation_prompt.rb
boxcars-0.3.2 lib/boxcars/conversation_prompt.rb
boxcars-0.3.1 lib/boxcars/conversation_prompt.rb