Class: LlmHub::Completion::Providers::Google

Inherits:
Base
  • Object
show all
Defined in:
lib/llm_hub/completion/providers/google.rb

Overview

Google Gemini completion provider

Constant Summary collapse

COMPLETIONS_URI =
'https://generativelanguage.googleapis.com/v1beta/models'

Instance Attribute Summary

Attributes inherited from Base

#api_key

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from LlmHub::Common::AbstractMethods

included

Constructor Details

This class inherits a constructor from LlmHub::Completion::Providers::Base

Instance Method Details

#extract_answer(response_body) ⇒ Object



30
31
32
# File 'lib/llm_hub/completion/providers/google.rb', line 30

def extract_answer(response_body)
  response_body&.dig('candidates', 0, 'content', 'parts', 0, 'text')
end

#extract_tokens(response_body) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/llm_hub/completion/providers/google.rb', line 34

def extract_tokens(response_body)
   = response_body&.dig('usageMetadata')
  {
    total_tokens: &.dig('totalTokenCount'),
    prompt_tokens: &.dig('promptTokenCount'),
    completion_tokens: &.dig('candidatesTokenCount')
  }
end

#headersObject



15
16
17
18
19
20
# File 'lib/llm_hub/completion/providers/google.rb', line 15

def headers
  {
    'Content-Type' => 'application/json',
    'x-goog-api-key' => @api_key
  }
end

#request_body(system_prompt, content, _model_name, option_params) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/llm_hub/completion/providers/google.rb', line 22

def request_body(system_prompt, content, _model_name, option_params)
  {
    system_instruction: build_system_instruction(system_prompt),
    contents: build_contents(content),
    generationConfig: build_generation_config(option_params)
  }
end

#url(model_name) ⇒ Object



10
11
12
13
# File 'lib/llm_hub/completion/providers/google.rb', line 10

def url(model_name)
  # Gemini API requires model name in URL
  "#{COMPLETIONS_URI}/#{model_name}:generateContent"
end