Class: GeminiAi::Client

Inherits:
Object
  • Object
show all
Includes:
HTTP
Defined in:
lib/gemini-ai/client.rb

Constant Summary collapse

CONFIG_KEYS =
%i[ api_key region file_path version service ].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HTTP

#json_post

Constructor Details

#initialize(config = {}, &faraday_middleware) ⇒ Client

Returns a new instance of Client.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gemini-ai/client.rb', line 19

def initialize(config = {}, &faraday_middleware)
  CONFIG_KEYS.each do |key|
    # Set instance variables like service authentication etc
    instance_variable_set("@#{key}", config[key] || GeminiAi.configuration.send(key))
  end
  @service_version = @version || DEFAULT_SERVICE_VERSION
  case
  when @api_key
    @authentication = :api_key
  when @file_path
    @authentication = :service_account
    @authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
      json_key_io: File.open(@file_path),
      scope: 'https://www.googleapis.com/auth/cloud-platform'
    )
  else
    @authentication = :default_credentials
    @authorizer = Google::Auth.get_application_default
  end

  if @authentication == :service_account || @authentication == :default_credentials
    @project_id = @authorizer.project_id || @authorizer.quota_project_id
    raise Errors::MissingProjectIdError, 'Could not determine project_id, which is required.' if @project_id.nil?
  end
  @faraday_middleware = faraday_middleware
end

Instance Attribute Details

#authenticationObject

Returns the value of attribute authentication.



16
17
18
# File 'lib/gemini-ai/client.rb', line 16

def authentication
  @authentication
end

#authorizerObject

Returns the value of attribute authorizer.



16
17
18
# File 'lib/gemini-ai/client.rb', line 16

def authorizer
  @authorizer
end

#project_idObject

Returns the value of attribute project_id.



16
17
18
# File 'lib/gemini-ai/client.rb', line 16

def project_id
  @project_id
end

#service_versionObject

Returns the value of attribute service_version.



16
17
18
# File 'lib/gemini-ai/client.rb', line 16

def service_version
  @service_version
end

Instance Method Details

#generate_content(payload, model: nil, stream: nil, &callback) ⇒ Object



51
52
53
54
# File 'lib/gemini-ai/client.rb', line 51

def generate_content(payload, model: nil, stream: nil, &callback)
  path = build_request_url('generateContent', model, stream)
  json_post(path: path, parameters: payload, &callback)
end

#stream_generate_content(payload, model: nil, stream: nil, &callback) ⇒ Object



46
47
48
49
# File 'lib/gemini-ai/client.rb', line 46

def stream_generate_content(payload, model: nil, stream: nil, &callback)
  path = build_request_url('streamGenerateContent', model, stream)
  json_post(path: path, parameters: payload, &callback)
end