Sha256: 88b43a0e1bb6a7fec97ba89b47eb0066a3b01bcce8aa6f3d28c8888ec1a03eef

Contents?: true

Size: 816 Bytes

Versions: 2

Compression:

Stored size: 816 Bytes

Contents

# frozen_string_literal: true

module GenAI
  module Api
    module Format
      module Gemini
        USER_ROLE = 'user'
        ASSISTANT_ROLE = 'model'

        def format_messages(messages)
          messages.map { |message| transform_message(message.deep_symbolize_keys) }
        end

        def transform_message(message)
          if message.keys == %i[role content]
            { role: role_for(message), parts: [text: message[:content]] }
          else
            message
          end
        end

        def extract_completions(response)
          response['candidates'].map { |candidate| candidate.dig('content', 'parts', 0, 'text') }
        end

        private

        def role_for(message)
          message[:role] == 'user' ? USER_ROLE : ASSISTANT_ROLE
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gen-ai-0.4.1 lib/gen_ai/api/format/gemini.rb
gen-ai-0.4.0 lib/gen_ai/api/format/gemini.rb