![]() |
VOOZH | about |
dotnet add package SIPSorcery.OpenAI.Realtime --version 10.0.10
NuGet\Install-Package SIPSorcery.OpenAI.Realtime -Version 10.0.10
<PackageReference Include="SIPSorcery.OpenAI.Realtime" Version="10.0.10" />
<PackageVersion Include="SIPSorcery.OpenAI.Realtime" Version="10.0.10" />Directory.Packages.props
<PackageReference Include="SIPSorcery.OpenAI.Realtime" />Project file
paket add SIPSorcery.OpenAI.Realtime --version 10.0.10
#r "nuget: SIPSorcery.OpenAI.Realtime, 10.0.10"
#:package SIPSorcery.OpenAI.Realtime@10.0.10
#addin nuget:?package=SIPSorcery.OpenAI.Realtime&version=10.0.10Install as a Cake Addin
#tool nuget:?package=SIPSorcery.OpenAI.Realtime&version=10.0.10Install as a Cake Tool
This repository contains a .NET library for interacting with OpenAI's real-time WebRTC API. It provides helper classes to negotiate peer connections, send and receive OPUS audio frames and exchange control messages over a data channel.
RTCPeerConnection (WebRTC) with OpenAI using a REST based signalling helper.DataChannelMessenger class to assist with sending session updates, function call results and response prompts.Install the library from NuGet:
dotnet add package SIPSorcery.OpenAI.WebRTC
See GetStarted example for full source.
using SIPSorcery.OpenAIWebRTC;
using SIPSorcery.OpenAIWebRTC.Models;
// Create the new WebRTC end point provided by this library (nothing starts yet).
var webrtcEndPoint = new WebRTCEndPoint(openAiKey, logger);
// Initialise default Windows audio devices and wire up event handlers to the WebRTC end point.
var windowsAudioEp = InitialiseWindowsAudioEndPoint();
webrtcEndPoint.ConnectAudioEndPoint(windowsAudioEp);
// Tell the WebRTC end point to start the connection attempt to the OpenAI Realtime WebRTC end point.
var negotiateConnectResult = await webrtcEndPoint.StartConnect();
// Wait for the connection to establish and then optionally update the session, start a conversation
// and process data channel messages.
webrtcEndPoint.OnPeerConnectionConnected += () =>
{
Log.Logger.Information("WebRTC peer connection established.");
var voice = RealtimeVoicesEnum.verse;
// Optionally send a session update message to adjust the session parameters.
var sessionUpdateResult = webrtcEndPoint.DataChannelMessenger.SendSessionUpdate(
voice,
"Keep it short.",
transcriptionModel: TranscriptionModelEnum.Whisper1);
if (sessionUpdateResult.IsLeft)
{
Log.Logger.Error($"Failed to send rsession update message: {sessionUpdateResult.LeftAsEnumerable().First()}");
}
// Trigger the conversation by sending a response create message.
var result = webrtcEndPoint.DataChannelMessenger.SendResponseCreate(voice, "Say Hi!");
if (result.IsLeft)
{
Log.Logger.Error($"Failed to send response create message: {result.LeftAsEnumerable().First()}");
}
};
webrtcEndPoint.OnDataChannelMessage += (dc, message) =>
{
var log = message switch
{
RealtimeServerEventSessionUpdated sessionUpdated => $"Session updated: {sessionUpdated.ToJson()}",
RealtimeServerEventConversationItemInputAudioTranscriptionDelta inputDelta => $"ME β: {inputDelta.Delta?.Trim()}",
RealtimeServerEventConversationItemInputAudioTranscriptionCompleted inputTranscript => $"ME β
: {inputTranscript.Transcript?.Trim()}",
RealtimeServerEventResponseAudioTranscriptDelta responseDelta => $"AI β: {responseDelta.Delta?.Trim()}",
RealtimeServerEventResponseAudioTranscriptDone responseTranscript => $"AI β
: {responseTranscript.Transcript?.Trim()}",
_ => $"Received {message.Type} -> {message.GetType().Name}"
};
if (log != string.Empty)
{
Log.Information(log);
}
};
Example Output:
[20:45:29 INF] AI β
: Hello! How can I assist you today?
[20:45:40 INF] ME β
: Tell me a nursery rhyme and use as many emojis as you can in the transcription.
[20:45:44 INF] AI β
: πΌπΆ Humpty Dumpty sat on a wall, π₯β¬οΈπ Humpty Dumpty had a great fall. π₯π₯β€΅οΈ All the king's horses ππ and all the king's men π¨ββοΈπ couldn't put Humpty together again! π₯ββοΈπ£
[20:46:06 INF] AI β
: You're welcome! π Anytime!
[20:46:06 INF] ME β
: Thank you.
See BrowserBridge example for full source.
using SIPSorcery.OpenAIWebRTC;
using SIPSorcery.OpenAIWebRTC.Models;
// Set up an ASP.NET web socket to listen for connections.
// The web socket is NOT used for the connection to OpenAI. It's a convenience signalling channel to allow the browser
// to establish a WebRTC connection with the ASP.NET app.
app.Map("/ws", async (HttpContext context,
[FromServices] IWebRTCEndPoint openAiWebRTCEndPoint) =>
{
Log.Debug("Web socket client connection established.");
if (context.WebSockets.IsWebSocketRequest)
{
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
// Create the ASP.NET WebRTC peer to connect to the browser.
var webSocketPeer = new WebRTCWebSocketPeerAspNet(
webSocket,
CreateBrowserPeerConnection,
null,
RTCSdpType.offer);
// Start the WebRTC connection attempt to the browser.
var browserPeerTask = webSocketPeer.Run();
// Start the attempt to connect to the OpenAI WebRTC end point in parallel with the
// browser connection which is already underway.
SetOpenAIPeerEventHandlers(openAiWebRTCEndPoint);
var openAiPeerTask = openAiWebRTCEndPoint.StartConnect(config);
// Wait for both WebRTC connections to establish.
await Task.WhenAll(browserPeerTask, openAiPeerTask);
// Wire up the event handlers to connect the browser's audio to the openAIWebRTCEndPoint instance.
// This is the equivalent of connecting the audio to local Windows audio devices but in this case
// the Browser audio stream is being wired up to the OpenAI audio stream.
// It's much simpler to connect the browser directly to OpenAI, and this library is not needed for that.
// The advantage of having an ASP.NET app in the middle is for things like capturing the audio transcription
// or using local functions that can be handled by the ASP.NET app.
ConnectPeers(webSocketPeer.RTCPeerConnection, openAiWebRTCEndPoint);
Log.Debug("Web socket closing with WebRTC peer connection in state {state}.", webSocketPeer.RTCPeerConnection?.connectionState);
}
else
{
// Not a WebSocket request
context.Response.StatusCode = StatusCodes.Status400BadRequest;
}
});
Several sample applications demonstrating different scenarios are available in the examples folder:
Each example folder contains its own README with usage instructions.
Distributed under the BSD 3βClause license with an additional BDS BYβNCβSA restriction. See LICENSE.md for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
-v10.0.10: Updated for main sipsorcery library release.
-v10.0.9: Updated for main sipsorcery library release.
-v10.0.8: Fixes for OpenAI model changes.
-v10.0.7: Updated for main sipsorcery library release.
-v10.0.6: Updated for main sipsorcery library release.
-v10.0.5: Updated for main sipsorcery library release.
-v8.0.4: Added gpt-4o-realtime-preview-2025-06-03 model option.
-v8.0.3: Stable release.
-v8.0.2-pre: Synchronised realtime models with OpenAI spec.
-v8.0.1-pre: Initial version.