Skip to content

Commit 2ea9ecf

Browse files
ngelxalexrudall
authored andcommitted
Added RealTime Session creation with ephemeral API token generation
1 parent d98ff26 commit 2ea9ecf

File tree

8 files changed

+437
-0
lines changed

8 files changed

+437
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Stream chats with the Responses API, transcribe and translate audio with Whisper
9292
- [Translate](#translate)
9393
- [Transcribe](#transcribe)
9494
- [Speech](#speech)
95+
- [Real-Time](#real-time)
9596
- [Usage](#usage)
9697
- [Errors](#errors-1)
9798
- [Development](#development)
@@ -1657,6 +1658,33 @@ File.binwrite('demo.mp3', response)
16571658
# => mp3 file that plays: "This is a speech test!"
16581659
```
16591660

1661+
### Real-Time
1662+
1663+
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.
1664+
1665+
```ruby
1666+
response = client.real_time.create(parameters: { model: "gpt-4o-realtime-preview-2024-12-17" })
1667+
puts "ephemeral key: #{response.dig('client_secret', 'value')}"
1668+
# => "ephemeral key: ek_abc123"
1669+
```
1670+
1671+
Then in the client-side application, make a POST request to the Real-Time API with the ephemeral key and the SDP offer.
1672+
1673+
```js
1674+
const OPENAI_REALTIME_URL = 'https://api.openai.com/v1/realtime/sessions'
1675+
const MODEL = 'gpt-4o-realtime-preview-2024-12-17'
1676+
1677+
const response = await fetch(`${OPENAI_REALTIME_URL}?model=${MODEL}`, {
1678+
method: 'POST',
1679+
headers: {
1680+
'Content-Type': 'application/sdp',
1681+
'Authorization': `Bearer ${ephemeralKey}`,
1682+
'OpenAI-Beta': 'realtime=v1'
1683+
},
1684+
body: offer.sdp
1685+
})
1686+
```
1687+
16601688
### Usage
16611689

16621690
The Usage API provides information about the cost of various OpenAI services within your organization.

lib/openai.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
require_relative "openai/assistants"
1111
require_relative "openai/threads"
1212
require_relative "openai/messages"
13+
require_relative "openai/real_time"
1314
require_relative "openai/runs"
1415
require_relative "openai/run_steps"
1516
require_relative "openai/vector_stores"

lib/openai/client.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ def batches
9292
@batches ||= OpenAI::Batches.new(client: self)
9393
end
9494

95+
def real_time
96+
@real_time ||= OpenAI::RealTime.new(client: self)
97+
end
98+
9599
def moderations(parameters: {})
96100
json_post(path: "/moderations", parameters: parameters)
97101
end

lib/openai/real_time.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module OpenAI
2+
class RealTime
3+
4+
DEFAULT_REALTIME_MODEL = 'gpt-4o-realtime-preview-2024-12-17'
5+
6+
def initialize(client:)
7+
@client = client.beta(realtime: 'v1')
8+
end
9+
10+
# Create a new real-time session with OpenAI.
11+
#
12+
# This method sets up a new session for real-time voice interaction with an OpenAI model.
13+
# It returns session details that can be used to establish a WebRTC connection.
14+
#
15+
# By default, this method uses the 'gpt-4o-realtime-preview-2024-12-17' model unless specified otherwise.
16+
#
17+
# @param parameters [Hash] parameters for the session (see: https://platform.openai.com/docs/api-reference/realtime-sessions/create)
18+
# @return [Hash] Session details including session ID, ICE servers, and other connection information
19+
def create(parameters: {})
20+
parameters = parameters.merge(model: DEFAULT_REALTIME_MODEL) unless parameters[:model]
21+
22+
@client.json_post(path: '/realtime/sessions', parameters: parameters)
23+
end
24+
end
25+
end

spec/fixtures/cassettes/realtime_session_create_custom_model.yml

Lines changed: 112 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/cassettes/realtime_session_create_default.yml

Lines changed: 112 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)