Class: Geminai::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/geminai/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, base_url: nil) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
16
# File 'lib/geminai/client.rb', line 9

def initialize(api_key: nil, base_url: nil)
  @api_key = api_key || ENV["GEMINI_API_KEY"]
  @base_url = base_url || "https://generativelanguage.googleapis.com"

  if @api_key.nil? || @api_key.empty?
    raise ArgumentError, "API Key is required"
  end
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



7
8
9
# File 'lib/geminai/client.rb', line 7

def api_key
  @api_key
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



7
8
9
# File 'lib/geminai/client.rb', line 7

def base_url
  @base_url
end

Instance Method Details

#interact(model:, input:, **options) ⇒ Object



18
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/geminai/client.rb', line 18

def interact(model:, input:, **options)
  uri = URI.parse("#{@base_url}/v1beta/interactions")

  body = {
    model: model,
    input: input
  }

  # Infer output shortcut from model name if not explicitly provided
  output = options.delete(:output)
  unless output
    if model.include?("-tts-") || model.include?("-audio")
      output = :audio
    elsif model.include?("-image")
      output = :image
    elsif model.include?("-video") || model.include?("-generate")
      output = :video
    end
  end

  # Handle output shortcut
  if output
    body[:response_format] = {type: output.to_s}
    body[:response_format][:aspect_ratio] = options.delete(:aspect_ratio) if options.key?(:aspect_ratio)
    body[:response_format][:image_size] = options.delete(:image_size) if options.key?(:image_size)
  end

  if (voice = options.delete(:voice))
    body[:generation_config] = {speech_config: [{voice: voice}]}
  end

  # Merge other options, allowing them to override shortcuts if explicitly provided
  options.each do |k, v|
    if body[k].is_a?(Hash) && v.is_a?(Hash)
      body[k] = body[k].merge(v)
    else
      body[k] = v
    end
  end

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = (uri.scheme == "https")

  # Net::HTTP defaults read timeout is 60s, but search grounding or image generation can take longer,
  # let's set a generous timeout.
  http.read_timeout = 120

  request = Net::HTTP::Post.new(uri.request_uri)
  request["Content-Type"] = "application/json"
  request["x-goog-api-key"] = @api_key
  request.body = JSON.generate(body)

  response = http.request(request)

  unless response.code == "200"
    raise(
      ApiError.new("API Request failed with code #{response.code}: #{response.body}", response.code, response.body)
    )
  end

  data = JSON.parse(response.body, symbolize_names: true)
  Interaction.new(data)
end