Class: Google::Genai::Live

Inherits:
Object
  • Object
show all
Defined in:
lib/google/genai/live.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_client) ⇒ Live

Returns a new instance of Live.



10
11
12
# File 'lib/google/genai/live.rb', line 10

def initialize(api_client)
  @api_client = api_client
end

Instance Method Details

#connect(model:, config: nil) ⇒ 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
# File 'lib/google/genai/live.rb', line 18

def connect(model:, config: nil)
  raise "Live API is not supported for Vertex AI yet" if @api_client.vertexai

  base_url = "wss://generativelanguage.googleapis.com"
  version = @api_client.instance_variable_get(:@http_options)&.[](:api_version) || 'v1beta'
  api_key = @api_client.api_key
  
  uri = "#{base_url}/ws/google.ai.generativelanguage.#{version}.GenerativeService.BidiGenerateContent?key=#{api_key}"
  
  ws = WebSocket::Client::Simple.connect(uri)
  
  session = Session.new(ws)
  
  setup_message = {
    setup: {
      model: "models/#{model}",
      generationConfig: config
    }
  }
  ws.send(setup_message.to_json)
  
  # Block until the connection is open and initial setup is confirmed.
  # This is a simplified way to handle the async nature of websockets in a sync method.
  # A more robust solution would involve a proper event loop.
  sleep 0.1 until ws.open?
  
  yield session
ensure
  ws.close if ws&.open?
end

#musicObject



14
15
16
# File 'lib/google/genai/live.rb', line 14

def music
  @music ||= LiveMusic.new(@api_client)
end