From 17bb86b311880806f74205ba3c69d8af8a8c7d23 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Fri, 7 Mar 2025 18:40:28 -0600 Subject: [PATCH] Return instruction for realtime response.create. --- .../MLTasks/IRealTimeCompletion.cs | 2 +- .../Routing/Models/RoutingArgs.cs | 4 ---- .../Hooks/RealtimeConversationHook.cs | 14 +++++++------- .../Realtime/RealTimeCompletionProvider.cs | 4 +++- .../util-twilio-hangup_phone_call.fn.liquid | 4 +++- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/MLTasks/IRealTimeCompletion.cs b/src/Infrastructure/BotSharp.Abstraction/MLTasks/IRealTimeCompletion.cs index 2d0150ece..075d72916 100644 --- a/src/Infrastructure/BotSharp.Abstraction/MLTasks/IRealTimeCompletion.cs +++ b/src/Infrastructure/BotSharp.Abstraction/MLTasks/IRealTimeCompletion.cs @@ -23,7 +23,7 @@ Task Connect(RealtimeHubConnection conn, Task Disconnect(); Task CreateSession(Agent agent, List conversations); - Task UpdateSession(RealtimeHubConnection conn, bool turnDetection = true); + Task UpdateSession(RealtimeHubConnection conn, bool turnDetection = true); Task InsertConversationItem(RoleDialogModel message); Task RemoveConversationItem(string itemId); Task TriggerModelInference(string? instructions = null); diff --git a/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingArgs.cs b/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingArgs.cs index 5b1c6a2a0..bc9a6fa8f 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingArgs.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingArgs.cs @@ -12,10 +12,6 @@ public class RoutingArgs [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? NextActionReason { get; set; } - [JsonPropertyName("reason")] - [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public string? Reason { get; set; } - [JsonPropertyName("conversation_end")] public bool ConversationEnd { get; set; } diff --git a/src/Infrastructure/BotSharp.Core.Realtime/Hooks/RealtimeConversationHook.cs b/src/Infrastructure/BotSharp.Core.Realtime/Hooks/RealtimeConversationHook.cs index e7478e29b..3d45ecfd9 100644 --- a/src/Infrastructure/BotSharp.Core.Realtime/Hooks/RealtimeConversationHook.cs +++ b/src/Infrastructure/BotSharp.Core.Realtime/Hooks/RealtimeConversationHook.cs @@ -36,12 +36,12 @@ public async Task OnFunctionExecuted(RoleDialogModel message) if (message.FunctionName == "route_to_agent") { var inst = JsonSerializer.Deserialize(message.FunctionArgs ?? "{}") ?? new(); - message.Content = $"Connected to agent of {inst.AgentName}"; + message.Content = $"I'm your AI assistant '{inst.AgentName}' to help with: '{inst.NextActionReason}'"; hub.HubConn.CurrentAgentId = routing.Context.GetCurrentAgentId(); - await hub.Completer.UpdateSession(hub.HubConn); + var instruction = await hub.Completer.UpdateSession(hub.HubConn); await hub.Completer.InsertConversationItem(message); - await hub.Completer.TriggerModelInference($"Guide the user through the next steps of the process as this Agent ({inst.AgentName}), following its instructions and operational procedures."); + await hub.Completer.TriggerModelInference($"{instruction}\r\n\r\nAssist user task: {inst.NextActionReason}"); } else if (message.FunctionName == "util-routing-fallback_to_router") { @@ -49,16 +49,16 @@ public async Task OnFunctionExecuted(RoleDialogModel message) message.Content = $"Returned to Router due to {inst.Reason}"; hub.HubConn.CurrentAgentId = routing.Context.GetCurrentAgentId(); - await hub.Completer.UpdateSession(hub.HubConn); + var instruction = await hub.Completer.UpdateSession(hub.HubConn); await hub.Completer.InsertConversationItem(message); - await hub.Completer.TriggerModelInference($"Check with user whether to proceed the new request: {inst.Reason}"); + await hub.Completer.TriggerModelInference(instruction); } else { // Update session for changed states - await hub.Completer.UpdateSession(hub.HubConn); + var instruction = await hub.Completer.UpdateSession(hub.HubConn); await hub.Completer.InsertConversationItem(message); - await hub.Completer.TriggerModelInference("Reply based on the function's output."); + await hub.Completer.TriggerModelInference(instruction); } } } diff --git a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs index 323d0b624..1d44bc8d7 100644 --- a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs @@ -285,7 +285,7 @@ public async Task CreateSession(Agent agent, List UpdateSession(RealtimeHubConnection conn, bool turnDetection = true) { var convService = _services.GetRequiredService(); var conv = await convService.GetConversation(conn.ConversationId); @@ -347,6 +347,8 @@ await HookEmitter.Emit(_services, async hook => }); await SendEventToModel(sessionUpdate); + + return instruction; } public async Task InsertConversationItem(RoleDialogModel message) diff --git a/src/Plugins/BotSharp.Plugin.Twilio/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/util-twilio-hangup_phone_call.fn.liquid b/src/Plugins/BotSharp.Plugin.Twilio/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/util-twilio-hangup_phone_call.fn.liquid index cc8360be3..4a6b5034e 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/util-twilio-hangup_phone_call.fn.liquid +++ b/src/Plugins/BotSharp.Plugin.Twilio/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/util-twilio-hangup_phone_call.fn.liquid @@ -1 +1,3 @@ -** Please call util-twilio-hangup_phone_call if user wants to end the phone call. \ No newline at end of file +{% if channel == 'phone' %} +** Please call util-twilio-hangup_phone_call if user wants to end the phone call. +{% endif %} \ No newline at end of file