Sha256: 97f4e2b9bdaf3090f5410d964570e8d779ca5e1d5e146e38240f4879d00d8cde

Contents?: true

Size: 977 Bytes

Versions: 3

Compression:

Stored size: 977 Bytes

Contents

# frozen_string_literal: true

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

      def initialize(provider:, token:, options: {})
        @history = []
        @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 = {})
        if @history.size == 1
          append_to_message(message)
        else
          append_to_history({ role: USER_ROLE, content: message })
        end

        response = @model.chat(@history.dup, options)
        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

3 entries across 3 versions & 1 rubygems

Version Path
gen-ai-0.4.0.alpha.3 lib/gen_ai/chat/base.rb
gen-ai-0.4.0.alpha.2 lib/gen_ai/chat/base.rb
gen-ai-0.4.0.alpha.1 lib/gen_ai/chat/base.rb