From ec9faa6c4e59e779596fe2c0b029fdeeddf0c405 Mon Sep 17 00:00:00 2001 From: hchen2020 <101423@smsassist.com> Date: Fri, 8 Sep 2023 17:05:55 -0500 Subject: [PATCH] Automatically populate LLM args by current conversation states. --- .../Routing/Models/RoutingRecord.cs | 8 ++-- .../BotSharp.Core/Functions/RouteToAgentFn.cs | 42 +++++++++++-------- .../Providers/ChatCompletionProvider.cs | 6 +++ 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingRecord.cs b/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingRecord.cs index 73fd4d1e..22578515 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingRecord.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingRecord.cs @@ -5,19 +5,19 @@ namespace BotSharp.Abstraction.Routing.Models; public class RoutingRecord { [JsonPropertyName("agent_id")] - public string AgentId { get; set; } + public string AgentId { get; set; } = string.Empty; [JsonPropertyName("name")] - public string Name { get; set; } + public string Name { get; set; } = string.Empty; [JsonPropertyName("description")] - public string Description { get; set; } + public string Description { get; set; } = string.Empty; [JsonPropertyName("required")] public List RequiredFields { get; set; } = new List(); [JsonPropertyName("redirect_to")] - public string RedirectTo { get; set; } + public string? RedirectTo { get; set; } [JsonPropertyName("disabled")] public bool Disabled { get; set; } diff --git a/src/Infrastructure/BotSharp.Core/Functions/RouteToAgentFn.cs b/src/Infrastructure/BotSharp.Core/Functions/RouteToAgentFn.cs index 4991dc99..bc3ca00a 100644 --- a/src/Infrastructure/BotSharp.Core/Functions/RouteToAgentFn.cs +++ b/src/Infrastructure/BotSharp.Core/Functions/RouteToAgentFn.cs @@ -1,7 +1,5 @@ -using BotSharp.Abstraction.Conversations.Models; using BotSharp.Abstraction.Functions; using BotSharp.Abstraction.Routing.Models; -using System.IO; namespace BotSharp.Core.Functions; @@ -63,26 +61,36 @@ public class RouteToAgentFn : IFunctionCallback agentId = routingRule.AgentId; // Check required fields - var jo = JsonSerializer.Deserialize(message.FunctionArgs); + var root = JsonSerializer.Deserialize(message.FunctionArgs); bool hasMissingField = false; + string missingFieldName = ""; foreach (var field in routingRule.RequiredFields) { - if (jo is JsonElement root) + if (!root.EnumerateObject().Any(x => x.Name == field)) { - if (!root.EnumerateObject().Any(x => x.Name == field)) - { - message.ExecutionResult = $"missing {field}."; - hasMissingField = true; - break; - } - else if (root.EnumerateObject().Any(x => x.Name == field) && - string.IsNullOrEmpty(root.EnumerateObject().FirstOrDefault(x => x.Name == field).Value.ToString())) - { - message.ExecutionResult = $"missing {field}."; - hasMissingField = true; - break; - } + message.ExecutionResult = $"missing {field}."; + hasMissingField = true; + missingFieldName = field; + break; } + else if (root.EnumerateObject().Any(x => x.Name == field) && + string.IsNullOrEmpty(root.EnumerateObject().FirstOrDefault(x => x.Name == field).Value.ToString())) + { + message.ExecutionResult = $"missing {field}."; + hasMissingField = true; + missingFieldName = field; + break; + } + } + + // Check if states contains the field according conversation context. + var states = _services.GetRequiredService(); + if (!string.IsNullOrEmpty(states.GetState(missingFieldName))) + { + var value = states.GetState(missingFieldName); + message.FunctionArgs = message.FunctionArgs.Substring(0, message.FunctionArgs.Length - 1) + $", \"{missingFieldName}\": \"{value}\"" + "}"; + hasMissingField = false; + missingFieldName = ""; } if (hasMissingField && !string.IsNullOrEmpty(routingRule.RedirectTo)) diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs index cd0f2206..af615eec 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs @@ -110,6 +110,12 @@ public class ChatCompletionProvider : IChatCompletion Channel = conversations.Last().Channel }; + // Somethings LLM will generate a function name with agent name. + if (!string.IsNullOrEmpty(funcContextIn.FunctionName)) + { + funcContextIn.FunctionName = funcContextIn.FunctionName.Split('.').Last(); + } + // Execute functions await onFunctionExecuting(funcContextIn); }