Automatically populate LLM args by current conversation states.
This commit is contained in:
parent
128181f3bb
commit
ec9faa6c4e
|
|
@ -5,19 +5,19 @@ namespace BotSharp.Abstraction.Routing.Models;
|
||||||
public class RoutingRecord
|
public class RoutingRecord
|
||||||
{
|
{
|
||||||
[JsonPropertyName("agent_id")]
|
[JsonPropertyName("agent_id")]
|
||||||
public string AgentId { get; set; }
|
public string AgentId { get; set; } = string.Empty;
|
||||||
|
|
||||||
[JsonPropertyName("name")]
|
[JsonPropertyName("name")]
|
||||||
public string Name { get; set; }
|
public string Name { get; set; } = string.Empty;
|
||||||
|
|
||||||
[JsonPropertyName("description")]
|
[JsonPropertyName("description")]
|
||||||
public string Description { get; set; }
|
public string Description { get; set; } = string.Empty;
|
||||||
|
|
||||||
[JsonPropertyName("required")]
|
[JsonPropertyName("required")]
|
||||||
public List<string> RequiredFields { get; set; } = new List<string>();
|
public List<string> RequiredFields { get; set; } = new List<string>();
|
||||||
|
|
||||||
[JsonPropertyName("redirect_to")]
|
[JsonPropertyName("redirect_to")]
|
||||||
public string RedirectTo { get; set; }
|
public string? RedirectTo { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("disabled")]
|
[JsonPropertyName("disabled")]
|
||||||
public bool Disabled { get; set; }
|
public bool Disabled { get; set; }
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
using BotSharp.Abstraction.Conversations.Models;
|
|
||||||
using BotSharp.Abstraction.Functions;
|
using BotSharp.Abstraction.Functions;
|
||||||
using BotSharp.Abstraction.Routing.Models;
|
using BotSharp.Abstraction.Routing.Models;
|
||||||
using System.IO;
|
|
||||||
|
|
||||||
namespace BotSharp.Core.Functions;
|
namespace BotSharp.Core.Functions;
|
||||||
|
|
||||||
|
|
@ -63,26 +61,36 @@ public class RouteToAgentFn : IFunctionCallback
|
||||||
agentId = routingRule.AgentId;
|
agentId = routingRule.AgentId;
|
||||||
|
|
||||||
// Check required fields
|
// Check required fields
|
||||||
var jo = JsonSerializer.Deserialize<object>(message.FunctionArgs);
|
var root = JsonSerializer.Deserialize<JsonElement>(message.FunctionArgs);
|
||||||
bool hasMissingField = false;
|
bool hasMissingField = false;
|
||||||
|
string missingFieldName = "";
|
||||||
foreach (var field in routingRule.RequiredFields)
|
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;
|
||||||
message.ExecutionResult = $"missing {field}.";
|
missingFieldName = field;
|
||||||
hasMissingField = true;
|
break;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
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))
|
if (hasMissingField && !string.IsNullOrEmpty(routingRule.RedirectTo))
|
||||||
|
|
|
||||||
|
|
@ -110,6 +110,12 @@ public class ChatCompletionProvider : IChatCompletion
|
||||||
Channel = conversations.Last().Channel
|
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
|
// Execute functions
|
||||||
await onFunctionExecuting(funcContextIn);
|
await onFunctionExecuting(funcContextIn);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue