Clean code for OpenAI Live

This commit is contained in:
Haiping Chen 2025-04-05 15:23:02 -05:00
parent 6e482e966b
commit 10de76df2d
15 changed files with 17 additions and 65 deletions

View file

@ -14,7 +14,6 @@ public class RealtimeHubConnection
public string CurrentAgentId { get; set; } = null!;
public string ConversationId { get; set; } = null!;
public string Data { get; set; } = string.Empty;
public string Model { get; set; } = null!;
public Func<string, object> OnModelMessageReceived { get; set; } = null!;
public Func<object> OnModelAudioResponseDone { get; set; } = null!;
public Func<object> OnModelUserInterrupted { get; set; } = null!;

View file

@ -1,5 +1,4 @@
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.MLTasks.Settings;
using BotSharp.Abstraction.Options;
using BotSharp.Core.Infrastructures;
@ -77,17 +76,6 @@ public class RealtimeHub : IRealtimeHub
var agent = await agentService.LoadAgent(conversation.AgentId);
_conn.CurrentAgentId = agent.Id;
// Set model
var model = "gpt-4o-mini-realtime";
if (agent.Profiles.Contains("realtime"))
{
var llmProviderService = _services.GetRequiredService<ILlmProviderService>();
model = llmProviderService.GetProviderModel("openai", "gpt-4o", modelType: LlmModelType.Realtime).Name;
}
_completer.SetModelName(model);
_conn.Model = model;
var routing = _services.GetRequiredService<IRoutingService>();
routing.Context.Push(agent.Id);

View file

@ -12,8 +12,6 @@
<ItemGroup>
<PackageReference Include="OpenAI" />
<PackageReference Include="Refit" />
<PackageReference Include="Refit.HttpClientFactory" />
</ItemGroup>
<ItemGroup>

View file

@ -1,5 +1,3 @@
using BotSharp.Abstraction.Functions.Models;
namespace BotSharp.Plugin.OpenAI.Models.Realtime;
public class RealtimeSessionBody

View file

@ -1,5 +1,3 @@
using BotSharp.Abstraction.Functions.Models;
namespace BotSharp.Plugin.OpenAI.Models.Realtime;
public class RealtimeSessionCreationRequest

View file

@ -1,5 +1,3 @@
using BotSharp.Abstraction.Realtime.Models;
namespace BotSharp.Plugin.OpenAI.Models.Realtime;
public class RealtimeSessionUpdate

View file

@ -1,9 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BotSharp.Plugin.OpenAI.Models;
public class SpeechToTextRequest

View file

@ -1,5 +1,3 @@
using System.Text.Json.Serialization;
namespace BotSharp.Plugin.OpenAI.Models;
public class TextCompletionRequest

View file

@ -1,5 +1,3 @@
using System.Text.Json.Serialization;
namespace BotSharp.Plugin.OpenAI.Models;
public class TextCompletionResponse

View file

@ -6,7 +6,6 @@ using BotSharp.Plugin.OpenAI.Providers.Text;
using BotSharp.Plugin.OpenAI.Providers.Chat;
using BotSharp.Plugin.OpenAI.Providers.Audio;
using Microsoft.Extensions.Configuration;
using Refit;
using BotSharp.Plugin.OpenAI.Providers.Realtime;
namespace BotSharp.Plugin.OpenAI;
@ -36,8 +35,5 @@ public class OpenAiPlugin : IBotSharpPlugin
services.AddScoped<IAudioTranscription, AudioTranscriptionProvider>();
services.AddScoped<IAudioSynthesis, AudioSynthesisProvider>();
services.AddScoped<IRealTimeCompletion, RealTimeCompletionProvider>();
services.AddRefitClient<IOpenAiRealtimeApi>()
.ConfigureHttpClient(c => c.BaseAddress = new Uri("https://api.openai.com"));
}
}

View file

@ -1,4 +1,3 @@
using BotSharp.Abstraction.Files.Utilities;
using OpenAI.Chat;
namespace BotSharp.Plugin.OpenAI.Providers.Chat;

View file

@ -1,11 +0,0 @@
using BotSharp.Abstraction.Realtime.Models;
using BotSharp.Plugin.OpenAI.Models.Realtime;
using Refit;
namespace BotSharp.Plugin.OpenAI.Providers.Realtime;
public interface IOpenAiRealtimeApi
{
[Post("/v1/realtime/sessions")]
Task<RealtimeSession> CreateSessionAsync(RealtimeSessionCreationRequest model, [Authorize("Bearer")] string token);
}

View file

@ -1,17 +1,6 @@
using BotSharp.Abstraction.Conversations.Enums;
using BotSharp.Abstraction.Files.Utilities;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Options;
using BotSharp.Abstraction.Realtime;
using BotSharp.Abstraction.Realtime.Models;
using BotSharp.Abstraction.Routing;
using BotSharp.Core.Infrastructures;
using BotSharp.Plugin.OpenAI.Models.Realtime;
using OpenAI.Chat;
using System.Net.WebSockets;
using System.Text;
using System.Text.Json;
using System.Threading;
namespace BotSharp.Plugin.OpenAI.Providers.Realtime;
@ -27,7 +16,7 @@ public class RealTimeCompletionProvider : IRealTimeCompletion
protected readonly IServiceProvider _services;
protected readonly ILogger<RealTimeCompletionProvider> _logger;
protected string _model = "gpt-4o-mini-realtime-preview-2024-12-17";
protected string _model = "gpt-4o-mini-realtime-preview";
private ClientWebSocket _webSocket;
public RealTimeCompletionProvider(
@ -50,14 +39,17 @@ public class RealTimeCompletionProvider : IRealTimeCompletion
Action<RoleDialogModel> onInputAudioTranscriptionCompleted,
Action onUserInterrupted)
{
var llmProviderService = _services.GetRequiredService<ILlmProviderService>();
_model = llmProviderService.GetProviderModel("openai", "gpt-4o", modelType: LlmModelType.Realtime).Name;
var settingsService = _services.GetRequiredService<ILlmProviderService>();
var settings = settingsService.GetSetting(provider: "openai", conn.Model);
var settings = settingsService.GetSetting(provider: "openai", _model);
_webSocket = new ClientWebSocket();
_webSocket.Options.SetRequestHeader("Authorization", $"Bearer {settings.ApiKey}");
_webSocket.Options.SetRequestHeader("OpenAI-Beta", "realtime=v1");
await _webSocket.ConnectAsync(new Uri($"wss://api.openai.com/v1/realtime?model={conn.Model}"), CancellationToken.None);
await _webSocket.ConnectAsync(new Uri($"wss://api.openai.com/v1/realtime?model={_model}"), CancellationToken.None);
if (_webSocket.State == WebSocketState.Open)
{

View file

@ -1,9 +1,5 @@
using BotSharp.Abstraction.MLTasks.Settings;
using System.Net.Http;
using System.Net.Mime;
using System.Text.Json;
using System.Text;
using BotSharp.Abstraction.Options;
namespace BotSharp.Plugin.OpenAI.Providers.Text;

View file

@ -4,6 +4,9 @@ global using System.Linq;
global using System.IO;
global using System.Threading.Tasks;
global using System.Text.Json.Serialization;
global using System.Text;
global using System.Text.Json;
global using System.Threading;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Logging;
@ -19,5 +22,13 @@ global using BotSharp.Abstraction.Agents;
global using BotSharp.Abstraction.Files;
global using BotSharp.Abstraction.Files.Models;
global using BotSharp.Abstraction.Utilities;
global using BotSharp.Abstraction.Conversations.Enums;
global using BotSharp.Abstraction.Files.Utilities;
global using BotSharp.Abstraction.Functions.Models;
global using BotSharp.Abstraction.MLTasks.Settings;
global using BotSharp.Abstraction.Options;
global using BotSharp.Abstraction.Realtime;
global using BotSharp.Abstraction.Realtime.Models;
global using BotSharp.Core.Infrastructures;
global using BotSharp.Plugin.OpenAI.Models;
global using BotSharp.Plugin.OpenAI.Settings;