diff --git a/src/Infrastructure/BotSharp.Abstraction/MLTasks/IRealTimeCompletion.cs b/src/Infrastructure/BotSharp.Abstraction/MLTasks/IRealTimeCompletion.cs index 68a96dfa..efbb3cec 100644 --- a/src/Infrastructure/BotSharp.Abstraction/MLTasks/IRealTimeCompletion.cs +++ b/src/Infrastructure/BotSharp.Abstraction/MLTasks/IRealTimeCompletion.cs @@ -22,7 +22,6 @@ public interface IRealTimeCompletion Task SendEventToModel(object message); Task Disconnect(); - Task CreateSession(Agent agent, List conversations); Task UpdateSession(RealtimeHubConnection conn, bool interruptResponse = true); Task InsertConversationItem(RoleDialogModel message); Task RemoveConversationItem(string itemId); diff --git a/src/Infrastructure/BotSharp.Core.Realtime/Hooks/RealtimeConversationHook.cs b/src/Infrastructure/BotSharp.Core.Realtime/Hooks/RealtimeConversationHook.cs index efa7d38c..9ff13082 100644 --- a/src/Infrastructure/BotSharp.Core.Realtime/Hooks/RealtimeConversationHook.cs +++ b/src/Infrastructure/BotSharp.Core.Realtime/Hooks/RealtimeConversationHook.cs @@ -31,9 +31,6 @@ public class RealtimeConversationHook : ConversationHookBase, IConversationHook return; } - // Clear cache to force to rebuild the agent instruction - Utilities.ClearCache(); - var routing = _services.GetRequiredService(); message.Role = AgentRole.Function; @@ -60,6 +57,9 @@ public class RealtimeConversationHook : ConversationHookBase, IConversationHook } else { + // Clear cache to force to rebuild the agent instruction + Utilities.ClearCache(); + // Update session for changed states var instruction = await hub.Completer.UpdateSession(hub.HubConn); await hub.Completer.InsertConversationItem(message); diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/RealtimeController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/RealtimeController.cs index 8773e35b..f8851cb3 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/RealtimeController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/RealtimeController.cs @@ -1,6 +1,4 @@ -using BotSharp.Abstraction.Realtime.Models; using BotSharp.Abstraction.Routing; -using BotSharp.Core.Infrastructures; namespace BotSharp.OpenAPI.Controllers; @@ -15,21 +13,6 @@ public class RealtimeController : ControllerBase _services = services; } - /// - /// Create an ephemeral API token for use in client-side applications with the Realtime API. - /// - /// - [HttpGet("/agent/{agentId}/realtime/session")] - public async Task CreateSession(string agentId) - { - var completion = CompletionProvider.GetRealTimeCompletion(_services, provider: "openai", modelId: "gpt-4o"); - - var agentService = _services.GetRequiredService(); - var agent = await agentService.LoadAgent(agentId); - - return await completion.CreateSession(agent, []); - } - [HttpPost("/agent/{agentId}/function/{functionName}/execute")] public async Task ExecuteFunction(string agentId, string functionName, [FromBody] JsonDocument args) { diff --git a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Realtime/RealTimeCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Realtime/RealTimeCompletionProvider.cs index 3d7fcc97..fe540e69 100644 --- a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Realtime/RealTimeCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Realtime/RealTimeCompletionProvider.cs @@ -228,31 +228,6 @@ public class GoogleRealTimeProvider : IRealTimeCompletion //todo Send Audio Chunks to Model, Botsharp RealTime Implementation seems to be incomplete } - public async Task CreateSession(Agent agent, List conversations) - { - var (prompt, request) = PrepareOptions(_chatClient, agent, conversations); - - var config = request.GenerationConfig; - - // Output Modality can either be text or audio - config.ResponseModalities = new List([Modality.AUDIO]); - - await _client.SendSetupAsync(new BidiGenerateContentSetup() - { - GenerationConfig = config, - Model = Model, - SystemInstruction = request.SystemInstruction, - Tools = request.Tools?.ToArray(), - }); - - return new RealtimeSession() - { - Id = _client.ConnectionId.ToString(), - Model = _model, - Voice = "default" - }; - } - public async Task UpdateSession(RealtimeHubConnection conn, bool interruptResponse = true) { var convService = _services.GetRequiredService(); diff --git a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs index da31e946..c18a5708 100644 --- a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs @@ -40,10 +40,10 @@ public class RealTimeCompletionProvider : IRealTimeCompletion Action onUserInterrupted) { var llmProviderService = _services.GetRequiredService(); - _model = llmProviderService.GetProviderModel("openai", "gpt-4o", modelType: LlmModelType.Realtime).Name; + _model = llmProviderService.GetProviderModel(Provider, "gpt-4o", modelType: LlmModelType.Realtime).Name; var settingsService = _services.GetRequiredService(); - var settings = settingsService.GetSetting(provider: "openai", _model); + var settings = settingsService.GetSetting(Provider, _model); _webSocket = new ClientWebSocket(); _webSocket.Options.SetRequestHeader("Authorization", $"Bearer {settings.ApiKey}"); @@ -276,41 +276,6 @@ public class RealTimeCompletionProvider : IRealTimeCompletion await _webSocket.SendAsync(new ArraySegment(buffer), WebSocketMessageType.Text, true, CancellationToken.None); } - public async Task CreateSession(Agent agent, List conversations) - { - var contentHooks = _services.GetServices().ToList(); - - var client = ProviderHelper.GetClient(Provider, _model, _services); - var chatClient = client.GetChatClient(_model); - var (prompt, messages, options) = PrepareOptions(agent, conversations); - - var instruction = messages.FirstOrDefault()?.Content.FirstOrDefault()?.Text ?? agent.Description; - - var args = new RealtimeSessionCreationRequest - { - Model = _model, - Instructions = instruction, - ToolChoice = "auto", - Tools = options.Tools.Select(x => - { - var fn = new FunctionDef - { - Name = x.FunctionName, - Description = x.FunctionDescription - }; - fn.Parameters = JsonSerializer.Deserialize(x.FunctionParameters); - return fn; - }).ToArray(), - }; - - var settingsService = _services.GetRequiredService(); - var settings = settingsService.GetSetting(Provider, args.Model ?? _model); - - var api = _services.GetRequiredService(); - var session = await api.CreateSessionAsync(args, settings.ApiKey); - return session; - } - public async Task UpdateSession(RealtimeHubConnection conn, bool interruptResponse = true) { var convService = _services.GetRequiredService();