Apply centralized routing by routing table.
This commit is contained in:
parent
36f0f2771b
commit
78b33bd4de
|
|
@ -1,9 +0,0 @@
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
|
|
||||||
namespace BotSharp.Abstraction.Agents.Models;
|
|
||||||
|
|
||||||
public class AgentRoutingArgs
|
|
||||||
{
|
|
||||||
[JsonPropertyName("agent_id")]
|
|
||||||
public string AgentId { get; set; }
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace BotSharp.Abstraction.Agents.Models;
|
||||||
|
|
||||||
|
public class RoutingArgs
|
||||||
|
{
|
||||||
|
[JsonPropertyName("agent_name")]
|
||||||
|
public string AgentName { get; set; }
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return AgentName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
namespace BotSharp.Abstraction.Agents.Models;
|
||||||
|
|
||||||
|
public class RoutingResult
|
||||||
|
{
|
||||||
|
public string Result { get; set; }
|
||||||
|
|
||||||
|
public RoutingResult(string result)
|
||||||
|
{
|
||||||
|
Result = result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace BotSharp.Abstraction.Agents.Models;
|
||||||
|
|
||||||
|
public class RoutingTable
|
||||||
|
{
|
||||||
|
[JsonPropertyName("agent_id")]
|
||||||
|
public string AgentId { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("name")]
|
||||||
|
public string AgentName { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("required")]
|
||||||
|
public List<string> RequiredFields { get; set; }
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return AgentName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -53,18 +53,19 @@ public partial class ConversationService
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn.Content = fn.ExecutionResult;
|
fn.Content = fn.FunctionArgs.Replace("\r", " ").Replace("\n", " ").Trim() + " => " + fn.ExecutionResult;
|
||||||
|
|
||||||
// Agent has been transferred
|
// Agent has been transferred
|
||||||
|
var agentSettings = _services.GetRequiredService<AgentSettings>();
|
||||||
if (fn.CurrentAgentId != preAgentId)
|
if (fn.CurrentAgentId != preAgentId)
|
||||||
{
|
{
|
||||||
var agentSettings = _services.GetRequiredService<AgentSettings>();
|
|
||||||
var agentService = _services.GetRequiredService<IAgentService>();
|
var agentService = _services.GetRequiredService<IAgentService>();
|
||||||
agent = await agentService.LoadAgent(fn.CurrentAgentId);
|
agent = await agentService.LoadAgent(fn.CurrentAgentId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add to dialog history
|
// Add to dialog history
|
||||||
_storage.Append(conversationId, preAgentId, fn);
|
// The server had an error processing your request. Sorry about that!
|
||||||
|
// _storage.Append(conversationId, preAgentId, fn);
|
||||||
|
|
||||||
// After function is executed, pass the result to LLM to get a natural response
|
// After function is executed, pass the result to LLM to get a natural response
|
||||||
wholeDialogs.Add(fn);
|
wholeDialogs.Add(fn);
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ using BotSharp.Abstraction.Agents.Models;
|
||||||
using BotSharp.Abstraction.Conversations.Models;
|
using BotSharp.Abstraction.Conversations.Models;
|
||||||
using BotSharp.Abstraction.Functions;
|
using BotSharp.Abstraction.Functions;
|
||||||
using BotSharp.Abstraction.Functions.Models;
|
using BotSharp.Abstraction.Functions.Models;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
namespace BotSharp.Core.Functions;
|
namespace BotSharp.Core.Functions;
|
||||||
|
|
||||||
|
|
@ -17,20 +19,51 @@ public class RouteToAgentFn : IFunctionCallback
|
||||||
|
|
||||||
public async Task<bool> Execute(RoleDialogModel message)
|
public async Task<bool> Execute(RoleDialogModel message)
|
||||||
{
|
{
|
||||||
var args = JsonSerializer.Deserialize<AgentRoutingArgs>(message.FunctionArgs);
|
var args = JsonSerializer.Deserialize<RoutingArgs>(message.FunctionArgs);
|
||||||
|
var result = new RoutingResult($"Routed to {args.AgentName}");
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(args.AgentId))
|
if (string.IsNullOrEmpty(args.AgentName))
|
||||||
{
|
{
|
||||||
var result = new FunctionExecutionValidationResult("false", "agent_id can't be parsed.");
|
result = new RoutingResult($"Can't find {args.AgentName}");
|
||||||
message.ExecutionResult = JsonSerializer.Serialize(result);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var result = new FunctionExecutionValidationResult("true");
|
var agentSettings = _services.GetRequiredService<AgentSettings>();
|
||||||
message.ExecutionResult = JsonSerializer.Serialize(result);
|
var dbSettings = _services.GetRequiredService<MyDatabaseSettings>();
|
||||||
message.CurrentAgentId = args.AgentId;
|
var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, agentSettings.RouterId, "route.json");
|
||||||
|
var routes = JsonSerializer.Deserialize<RoutingTable[]>(File.ReadAllText(filePath));
|
||||||
|
|
||||||
|
var agent = routes.FirstOrDefault(x => x.AgentName.ToLower() == args.AgentName.ToLower());
|
||||||
|
if (agent == null)
|
||||||
|
{
|
||||||
|
result = new RoutingResult($"Can't find agent {args.AgentName}.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Check required fields
|
||||||
|
var jo = JsonSerializer.Deserialize<object>(message.FunctionArgs);
|
||||||
|
bool hasMissingField = false;
|
||||||
|
foreach (var field in agent.RequiredFields)
|
||||||
|
{
|
||||||
|
if (jo is JsonElement root)
|
||||||
|
{
|
||||||
|
if (!root.EnumerateObject().Any(x => x.Name == field))
|
||||||
|
{
|
||||||
|
result = new RoutingResult($"Please provide {field}.");
|
||||||
|
hasMissingField = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasMissingField)
|
||||||
|
{
|
||||||
|
message.CurrentAgentId = agent.AgentId;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
message.ExecutionResult = JsonSerializer.Serialize(result);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue