BotSharp/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs

127 lines
3.7 KiB
C#
Raw Normal View History

2023-09-19 12:17:50 +00:00
using BotSharp.Abstraction.Agents.Models;
2023-09-23 21:33:05 +00:00
using BotSharp.Abstraction.Functions.Models;
2023-10-28 20:59:26 +00:00
using BotSharp.Abstraction.Planning;
2023-09-19 12:17:50 +00:00
using BotSharp.Abstraction.Routing;
2023-10-29 01:54:10 +00:00
using BotSharp.Abstraction.Routing.Models;
2023-09-20 10:31:50 +00:00
using BotSharp.Abstraction.Routing.Settings;
2023-10-28 20:59:26 +00:00
using System.Drawing;
2023-09-19 12:17:50 +00:00
namespace BotSharp.Core.Routing;
public partial class RoutingService : IRoutingService
2023-09-19 12:17:50 +00:00
{
private readonly IServiceProvider _services;
2023-09-20 10:31:50 +00:00
private readonly RoutingSettings _settings;
private readonly IRouterInstance _routerInstance;
2023-09-19 12:17:50 +00:00
private readonly ILogger _logger;
2023-10-30 16:48:18 +00:00
private Agent _router;
public Agent Router => _router;
2023-09-19 12:17:50 +00:00
public void ResetRecursiveCounter()
{
_currentRecursionDepth = 0;
}
2023-09-20 10:31:50 +00:00
public RoutingService(IServiceProvider services,
RoutingSettings settings,
ILogger<RoutingService> logger,
IRouterInstance routerInstance)
2023-09-19 12:17:50 +00:00
{
_services = services;
2023-09-20 10:31:50 +00:00
_settings = settings;
2023-09-19 12:17:50 +00:00
_logger = logger;
_routerInstance = routerInstance;
2023-09-19 12:17:50 +00:00
}
2023-10-30 16:48:18 +00:00
public async Task<RoleDialogModel> ExecuteOnce(Agent agent, RoleDialogModel message)
2023-09-23 21:33:05 +00:00
{
var handlers = _services.GetServices<IRoutingHandler>();
var handler = handlers.FirstOrDefault(x => x.Name == "route_to_agent");
2023-10-30 16:48:18 +00:00
var dialogs = new List<RoleDialogModel> { message };
handler.SetDialogs(dialogs);
2023-10-30 16:48:18 +00:00
var inst = new FunctionCallFromLlm
2023-09-23 21:33:05 +00:00
{
2023-09-25 22:46:00 +00:00
Function = "route_to_agent",
Question = message.Content,
Reason = message.Content,
2023-09-25 22:46:00 +00:00
AgentName = agent.Name
2023-10-30 16:48:18 +00:00
};
2023-09-23 21:33:05 +00:00
2023-10-30 16:48:18 +00:00
var result = await handler.Handle(this, inst, message);
var response = dialogs.Last();
response.MessageId = message.MessageId;
response.Instruction = inst;
return response;
2023-09-23 21:33:05 +00:00
}
2023-10-30 16:48:18 +00:00
public async Task<RoleDialogModel> InstructLoop(RoleDialogModel message)
2023-09-19 12:17:50 +00:00
{
2023-10-30 16:48:18 +00:00
_router = _routerInstance.Load()
.Router;
RoleDialogModel response = default;
var conv = _services.GetRequiredService<IConversationService>();
var dialogs = conv.GetDialogHistory();
2023-10-29 01:54:10 +00:00
var context = _services.GetRequiredService<RoutingContext>();
2023-10-28 20:59:26 +00:00
var planner = _services.GetRequiredService<IPlaner>();
var executor = _services.GetRequiredService<IExecutor>();
2023-09-22 20:38:58 +00:00
2023-10-30 16:48:18 +00:00
context.Push(_router.Id);
2023-09-19 12:17:50 +00:00
int loopCount = 0;
2023-10-30 16:48:18 +00:00
while (loopCount < 5 && !context.IsEmpty)
2023-09-19 12:17:50 +00:00
{
loopCount++;
2023-10-30 16:48:18 +00:00
var conversation = await GetConversationContent(dialogs);
_router.TemplateDict["conversation"] = conversation;
2023-09-19 12:17:50 +00:00
2023-10-28 20:59:26 +00:00
// Get instruction from Planner
2023-10-30 16:48:18 +00:00
var inst = await planner.GetNextInstruction(_router, message.MessageId);
2023-09-19 12:17:50 +00:00
2023-10-28 20:59:26 +00:00
// Save states
SaveStateByArgs(inst.Arguments);
2023-10-28 20:59:26 +00:00
#if DEBUG
Console.WriteLine($"*** Next Instruction *** {inst}", Color.GreenYellow);
#else
_logger.LogInformation($"*** Next Instruction *** {inst}");
#endif
2023-10-29 01:54:10 +00:00
await planner.AgentExecuting(inst, message);
2023-09-19 12:17:50 +00:00
2023-10-28 20:59:26 +00:00
// Handle instruction by Executor
2023-10-30 16:48:18 +00:00
response = await executor.Execute(this, inst, message, dialogs);
2023-10-28 20:59:26 +00:00
2023-10-30 16:48:18 +00:00
await planner.AgentExecuted(inst, response);
2023-09-19 12:17:50 +00:00
}
2023-10-30 16:48:18 +00:00
return response;
2023-09-19 12:17:50 +00:00
}
protected void SaveStateByArgs(JsonDocument args)
2023-09-20 10:31:50 +00:00
{
if (args == null)
2023-09-24 16:20:02 +00:00
{
return;
}
2023-09-24 16:20:02 +00:00
var stateService = _services.GetRequiredService<IConversationStateService>();
if (args.RootElement is JsonElement root)
2023-09-20 10:31:50 +00:00
{
foreach (JsonProperty property in root.EnumerateObject())
2023-09-24 16:20:02 +00:00
{
if (!string.IsNullOrEmpty(property.Value.ToString()))
2023-09-26 03:04:04 +00:00
{
stateService.SetState(property.Name, property.Value);
}
2023-09-24 16:20:02 +00:00
}
}
2023-09-22 20:38:58 +00:00
}
2023-09-19 12:17:50 +00:00
}