Skip to content

RealTime session create to retrieve Ephemeral Token #582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -67,6 +67,7 @@ Stream chats with the Responses API, transcribe and translate audio with Whisper
- [Translate](#translate)
- [Transcribe](#transcribe)
- [Speech](#speech)
- [Real-Time](#real-time)
- [Usage](#usage)
- [Errors](#errors-1)
- [Development](#development)
@@ -1587,6 +1588,33 @@ File.binwrite('demo.mp3', response)
# => mp3 file that plays: "This is a speech test!"
```

### Real-Time

The Real-Time API allows you to create a real-time session with an OpenAI model. It responds with a session object, plus a client_secret key which contains a usable ephemeral API token that can be used to authenticate browser clients for the Realtime API.

```ruby
response = client.real_time.create(parameters: { model: "gpt-4o-realtime-preview-2024-12-17" })
puts "ephemeral key: #{response.dig('client_secret', 'value')}"
# => "ephemeral key: ek_abc123"
```

Then in the client-side application, make a POST request to the Real-Time API with the ephemeral key and the SDP offer.

```js
const OPENAI_REALTIME_URL = 'https://api.openai.com/v1/realtime/sessions'
const MODEL = 'gpt-4o-realtime-preview-2024-12-17'

const response = await fetch(`${OPENAI_REALTIME_URL}?model=${MODEL}`, {
method: 'POST',
headers: {
'Content-Type': 'application/sdp',
'Authorization': `Bearer ${ephemeralKey}`,
'OpenAI-Beta': 'realtime=v1'
},
body: offer.sdp
})
```

### Usage
The Usage API provides information about the cost of various OpenAI services within your organization.
To use Admin APIs like Usage, you need to set an OPENAI_ADMIN_TOKEN, which can be generated [here](https://platform.openai.com/settings/organization/admin-keys).
1 change: 1 addition & 0 deletions lib/openai.rb
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
require_relative "openai/assistants"
require_relative "openai/threads"
require_relative "openai/messages"
require_relative "openai/real_time"
require_relative "openai/runs"
require_relative "openai/run_steps"
require_relative "openai/vector_stores"
6 changes: 6 additions & 0 deletions lib/openai/client.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# rubocop:disable Metrics/ClassLength
module OpenAI
class Client
include OpenAI::HTTP
@@ -92,6 +93,10 @@ def batches
@batches ||= OpenAI::Batches.new(client: self)
end

def real_time
@real_time ||= OpenAI::RealTime.new(client: self)
end

def moderations(parameters: {})
json_post(path: "/moderations", parameters: parameters)
end
@@ -132,3 +137,4 @@ def inspect
end
end
end
# rubocop:enable Metrics/ClassLength
26 changes: 26 additions & 0 deletions lib/openai/real_time.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module OpenAI
class RealTime
DEFAULT_REALTIME_MODEL = "gpt-4o-realtime-preview-2024-12-17".freeze

def initialize(client:)
@client = client.beta(realtime: "v1")
end

# Create a new real-time session with OpenAI.
#
# This method sets up a new session for real-time voice interaction with an OpenAI model.
# It returns session details that can be used to establish a WebRTC connection.
#
# By default, this method uses the 'gpt-4o-realtime-preview-2024-12-17' model
# unless specified otherwise.
#
# @param parameters [Hash] parameters for the session (see: https://platform.openai.com/docs/api-reference/realtime-sessions/create)
# @return [Hash] Session details including session ID, ICE servers, and other
# connection information
def create(parameters: {})
parameters = parameters.merge(model: DEFAULT_REALTIME_MODEL) unless parameters[:model]

@client.json_post(path: "/realtime/sessions", parameters: parameters)
end
end
end
112 changes: 112 additions & 0 deletions spec/fixtures/cassettes/realtime_session_create_custom_model.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

112 changes: 112 additions & 0 deletions spec/fixtures/cassettes/realtime_session_create_default.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading