From 1bccb15058db19c261941387ecf231302b870342 Mon Sep 17 00:00:00 2001 From: Haiping Chen <101423@smsassist.com> Date: Mon, 11 Mar 2024 13:26:49 -0500 Subject: [PATCH] Syn agent template dict once states has been changed. --- .../Routing/IRoutingService.cs | 2 +- .../Agents/AgentConversationHook.cs | 26 +++++++++++++++++++ .../BotSharp.Core/Agents/AgentPlugin.cs | 1 + .../Agents/Services/AgentService.LoadAgent.cs | 1 - .../HumanInterventionNeededHandler.cs | 12 +++------ .../Routing/RoutingService.InvokeFunction.cs | 6 +++-- .../Hooks/StreamingLogHook.cs | 5 ---- 7 files changed, 36 insertions(+), 17 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Core/Agents/AgentConversationHook.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingService.cs b/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingService.cs index 74845e1f9..771223ef6 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingService.cs @@ -31,7 +31,7 @@ public interface IRoutingService List GetHandlers(Agent router); void ResetRecursiveCounter(); Task InvokeAgent(string agentId, List dialogs); - Task InvokeFunction(string name, RoleDialogModel message); + Task InvokeFunction(string name, RoleDialogModel message, bool restoreOriginalFunctionName = true); Task InstructLoop(RoleDialogModel message); /// diff --git a/src/Infrastructure/BotSharp.Core/Agents/AgentConversationHook.cs b/src/Infrastructure/BotSharp.Core/Agents/AgentConversationHook.cs new file mode 100644 index 000000000..0a06e8110 --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Agents/AgentConversationHook.cs @@ -0,0 +1,26 @@ + +namespace BotSharp.Core.Agents; + +public class AgentConversationHook : ConversationHookBase +{ + private readonly IServiceProvider _services; + + public AgentConversationHook(IServiceProvider services) + { + _services = services; + } + + public override async Task OnStateChanged(string name, string preValue, string currentValue) + { + // Apply new states to agent TemplateDict + var routing = _services.GetRequiredService(); + var agentId = routing.GetCurrentAgentId(); + if (string.IsNullOrEmpty(agentId)) + { + return; + } + var agentService = _services.GetRequiredService(); + var agent = agentService.LoadAgent(agentId).Result; + agent.TemplateDict[name] = currentValue; + } +} diff --git a/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs b/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs index 7df5fa02a..92323fb42 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs @@ -21,6 +21,7 @@ public void RegisterDI(IServiceCollection services, IConfiguration config) { services.AddScoped(); services.AddScoped(); + services.AddScoped(); services.AddScoped(provider => { diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs index a6910603f..cb91a5a73 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs @@ -1,4 +1,3 @@ -using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Templating; namespace BotSharp.Core.Agents.Services; diff --git a/src/Infrastructure/BotSharp.Core/Routing/Handlers/HumanInterventionNeededHandler.cs b/src/Infrastructure/BotSharp.Core/Routing/Handlers/HumanInterventionNeededHandler.cs index 34f1952a4..39a50a309 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Handlers/HumanInterventionNeededHandler.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Handlers/HumanInterventionNeededHandler.cs @@ -12,7 +12,7 @@ public class HumanInterventionNeededHandler : RoutingHandlerBase, IRoutingHandle { new ParameterPropertyDef("reason", "why need customer service"), new ParameterPropertyDef("summary", "the whole conversation summary with important information"), - new ParameterPropertyDef("response", "response content to user") + new ParameterPropertyDef("response", "tell the user that you are being transferred to customer service") }; public HumanInterventionNeededHandler(IServiceProvider services, ILogger logger, RoutingSettings settings) @@ -23,13 +23,9 @@ public HumanInterventionNeededHandler(IServiceProvider services, ILogger Handle(IRoutingService routing, FunctionCallFromLlm inst, RoleDialogModel message) { - var response = new RoleDialogModel(AgentRole.Assistant, inst.Response) - { - CurrentAgentId = message.CurrentAgentId, - MessageId = message.MessageId, - StopCompletion = true, - FunctionName = inst.Function - }; + var response = RoleDialogModel.From(message, + role: AgentRole.Assistant, + content: inst.Response); _dialogs.Add(response); diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeFunction.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeFunction.cs index 6ef40c580..6f70e0638 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeFunction.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeFunction.cs @@ -4,7 +4,7 @@ namespace BotSharp.Core.Routing; public partial class RoutingService { - public async Task InvokeFunction(string name, RoleDialogModel message) + public async Task InvokeFunction(string name, RoleDialogModel message, bool restoreOriginalFunctionName = true) { var function = _services.GetServices().FirstOrDefault(x => x.Name == name); if (function == null) @@ -56,7 +56,9 @@ public async Task InvokeFunction(string name, RoleDialogModel message) } // restore original function name - if (!message.StopCompletion && message.FunctionName != originalFunctionName) + if (!message.StopCompletion && + message.FunctionName != originalFunctionName && + restoreOriginalFunctionName) { message.FunctionName = originalFunctionName; } diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs index f06020808..254520e78 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs @@ -133,11 +133,6 @@ public override async Task OnResponseGenerated(RoleDialogModel message) log += $"\r\n```json\r\n{richContent}\r\n```"; } - if (!string.IsNullOrEmpty(message.FunctionName)) - { - log += $"\r\n\r\n**{message.FunctionName}**"; - } - var input = new ContentLogInputModel(conv.ConversationId, message) { Name = agent?.Name,