Skip to content

Commit 90d5c90

Browse files
authored
Merge pull request #582 from ngelx/main
RealTime session create to retrieve Ephemeral Token
2 parents d98ff26 + 7942205 commit 90d5c90

File tree

7 files changed

+310
-0
lines changed

7 files changed

+310
-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+
### Realtime
1662+
1663+
The [Realtime API](https://platform.openai.com/docs/guides/realtime) allows you to create a live speech-to-speech 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 a WebRTC connection](https://platform.openai.com/docs/guides/realtime#connect-with-webrtc).
1664+
1665+
```ruby
1666+
response = client.realtime.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 Javascript 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/realtime"
1314
require_relative "openai/runs"
1415
require_relative "openai/run_steps"
1516
require_relative "openai/vector_stores"

lib/openai/client.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# rubocop:disable Metrics/ClassLength
12
module OpenAI
23
class Client
34
include OpenAI::HTTP
@@ -92,6 +93,10 @@ def batches
9293
@batches ||= OpenAI::Batches.new(client: self)
9394
end
9495

96+
def realtime
97+
@realtime ||= OpenAI::Realtime.new(client: self)
98+
end
99+
95100
def moderations(parameters: {})
96101
json_post(path: "/moderations", parameters: parameters)
97102
end
@@ -132,3 +137,4 @@ def inspect
132137
end
133138
end
134139
end
140+
# rubocop:enable Metrics/ClassLength

lib/openai/realtime.rb

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

spec/fixtures/cassettes/realtime_session_create.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_with_params.yml

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

spec/openai/client/realtime_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
RSpec.describe OpenAI::Realtime do
2+
let(:client) { OpenAI::Client.new }
3+
let(:realtime) { client.realtime }
4+
5+
describe "#create" do
6+
it "uses the specified model" do
7+
model = "gpt-4o-realtime-preview-2024-12-18"
8+
VCR.use_cassette("realtime_session_create") do
9+
response = realtime.create(parameters: { model: model })
10+
expect(response["model"]).to eq(model)
11+
end
12+
end
13+
14+
context "with additional parameters" do
15+
it "sends all parameters to the API" do
16+
parameters = {
17+
model: "gpt-4o-realtime-preview-2024-12-17",
18+
voice: "alloy",
19+
instructions: "You are a helpful assistant."
20+
}
21+
22+
VCR.use_cassette("realtime_session_create_with_params") do
23+
response = realtime.create(parameters: parameters)
24+
expect(response["model"]).to eq(parameters[:model])
25+
expect(response["voice"]).to eq(parameters[:voice])
26+
expect(response["instructions"]).to eq(parameters[:instructions])
27+
end
28+
end
29+
end
30+
end
31+
end

0 commit comments

Comments
 (0)