Module: GenAI::Api::Format::Gemini

Included in:
Chat::Gemini, Language::Gemini
Defined in:
lib/gen_ai/api/format/gemini.rb

Constant Summary collapse

USER_ROLE =
'user'
ASSISTANT_ROLE =
'model'

Instance Method Summary collapse

Instance Method Details

#chunk_params_from_streaming(chunk) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/gen_ai/api/format/gemini.rb', line 30

def chunk_params_from_streaming(chunk)
  {
    model: 'gemini-pro',
    index: chunk.dig('candidates', 0, 'index'),
    value: chunk.dig('candidates', 0, 'content', 'parts', 0, 'text'),
    raw: chunk
  }
end

#extract_completions(response) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/gen_ai/api/format/gemini.rb', line 22

def extract_completions(response)
  if response.is_a?(Array)
    response.map { |completion| extract_candidates(completion) }
  else
    extract_candidates(response)
  end
end

#format_messages(messages) ⇒ Object



10
11
12
# File 'lib/gen_ai/api/format/gemini.rb', line 10

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

#transform_message(message) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/gen_ai/api/format/gemini.rb', line 14

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