Automatically populate LLM args by current conversation states.

This commit is contained in:
hchen2020 2023-09-08 17:05:55 -05:00
parent 128181f3bb
commit ec9faa6c4e
3 changed files with 35 additions and 21 deletions

View file

@ -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<string> RequiredFields { get; set; } = new List<string>();
[JsonPropertyName("redirect_to")]
public string RedirectTo { get; set; }
public string? RedirectTo { get; set; }
[JsonPropertyName("disabled")]
public bool Disabled { get; set; }

View file

@ -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<object>(message.FunctionArgs);
var root = JsonSerializer.Deserialize<JsonElement>(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<IConversationStateService>();
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))

View file

@ -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);
}