Sha256: 89f2d5108a45910f664487381a4c773b9a28beff248774d34eea443a7b5a3103

Contents?: true

Size: 883 Bytes

Versions: 3

Compression:

Stored size: 883 Bytes

Contents

# frozen_string_literal: true

module GenAI
  class Chat
    class Gemini < Base
      include GenAI::Api::Format::Gemini

      USER_ROLE = 'user'
      ASSISTANT_ROLE = 'model'

      private

      def build_history(messages, context, examples)
        history = format_messages(messages.drop(1))
        history.prepend({ role: USER_ROLE, parts: [{text: build_first_message(context, examples, messages.first)}] })
        history
      end

      def build_first_message(context, examples, message)
        chunks = []
        chunks << context if context
        chunks << examples.map { |example| "#{example[:role]}: #{example[:content]}" }.join("\n") if examples.any?
        chunks << message[:content] if message
        chunks.join("\n")
      end

      def append_to_message(message)
        @history.last[:parts][0][:text] << "\n" << 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/gemini.rb
gen-ai-0.4.0.alpha.2 lib/gen_ai/chat/gemini.rb
gen-ai-0.4.0.alpha.1 lib/gen_ai/chat/gemini.rb