Sha256: 807e8216b0cc4fea1a5197f1c6428d1ea152ae580fa7bdf0d3661be13b442f14

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

module GenAI
  class Chat
    class Base < GenAI::Base
      USER_ROLE = 'user'
      ASSISTANT_ROLE = 'assistant'

      attr_reader :history, :default_options

      def initialize(provider:, token:, options: {})
        @history = []
        @default_options = {}
        @model = GenAI::Language.new(provider, token, options: options)
      end

      def start(history: [], context: nil, examples: [])
        @history = build_history(history.map(&:deep_symbolize_keys), context, examples.map(&:deep_symbolize_keys))
      end

      def message(message, options = {}, &block)
        if @history.size == 1 && @history.first[:role] != 'system'
          append_to_message(message)
        else
          append_to_history({ role: USER_ROLE, content: message })
        end

        response = @model.chat(@history.dup, default_options.merge(options).compact, &block)
        append_to_history({ role: ASSISTANT_ROLE, content: response.value })
        response
      end

      private

      def append_to_history(message)
        @history << transform_message(message)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gen-ai-0.4.3 lib/gen_ai/chat/base.rb
gen-ai-0.4.2 lib/gen_ai/chat/base.rb