Connect
Connect to Live without query parameters. Send session.start with model and session configuration, then wait for session.started. Follow the Live prompting guide before writing instructions.
WebSocket wss://api.openai.com/v1/live/sessions
Connect
require "openai"
require "async"
# The session model is sent in the first event.
client = OpenAI::Client.new
Sync do |task|
# Stop waiting if the peer never sends the expected events.
task.with_timeout(60) do
# The block always releases this session's transport.
client.live.connect() do |connection|
connection.send_event(type: "session.start", session: { model: "gpt-live-1" })
complete = false
close_requested = false
connection.each do |event|
case event
when OpenAI::Live::ErrorEvent
raise "Live reported a session error."
when OpenAI::Live::SessionStartedEvent
# Wait for startup before closing the session this example owns.
puts "Session ready: #{event.session.id} #{event.session.model}"
connection.send_event(type: "session.close")
close_requested = true
when OpenAI::Live::SessionClosedEvent
unless close_requested && event.reason == :close_requested
raise "Session ended unexpectedly: #{event.reason}; usage=#{event.usage}"
end
puts "Final usage: #{event.usage}"
complete = true
break
end
end
raise "Connection closed before session.closed; final usage unconfirmed" unless complete
end
end
end