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

131 lines
3.5 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-09-19 12:17:50 +00:00
using BotSharp.Abstraction.Routing;
2023-09-20 10:31:50 +00:00
using BotSharp.Abstraction.Routing.Settings;
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;
private List<RoleDialogModel> _dialogs;
2023-09-25 22:46:00 +00:00
public List<RoleDialogModel> Dialogs {
get
{
if (_dialogs == null)
{
var conv = _services.GetRequiredService<IConversationService>();
_dialogs = conv.GetDialogHistory();
}
return _dialogs;
}
}
2023-09-19 12:17:50 +00:00
public void ResetRecursiveCounter()
{
_currentRecursionDepth = 0;
}
2023-10-19 17:24:00 +00:00
public void RefreshDialogs()
{
_dialogs = null;
}
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-09-23 21:33:05 +00:00
public async Task<RoleDialogModel> ExecuteOnce(Agent agent)
{
2023-09-25 22:46:00 +00:00
var message = Dialogs.Last().Content;
2023-09-23 21:33:05 +00:00
var handlers = _services.GetServices<IRoutingHandler>();
var handler = handlers.FirstOrDefault(x => x.Name == "route_to_agent");
2023-09-25 22:46:00 +00:00
handler.SetDialogs(Dialogs);
var result = await handler.Handle(this, new FunctionCallFromLlm
2023-09-23 21:33:05 +00:00
{
2023-09-25 22:46:00 +00:00
Function = "route_to_agent",
Question = message,
Reason = message,
AgentName = agent.Name
2023-09-23 21:33:05 +00:00
});
return result;
}
2023-09-25 22:46:00 +00:00
public async Task<RoleDialogModel> InstructLoop()
2023-09-19 12:17:50 +00:00
{
2023-10-20 03:47:14 +00:00
_routerInstance.Load();
var router = _routerInstance.Router;
2023-09-22 20:38:58 +00:00
var result = new RoleDialogModel(AgentRole.Assistant, "Can you repeat your request again?")
{
CurrentAgentId = router.Id
};
2023-09-19 12:17:50 +00:00
2023-10-27 15:18:48 +00:00
var inputMsg = Dialogs.Last();
2023-09-19 12:17:50 +00:00
2023-09-22 20:38:58 +00:00
var handlers = _services.GetServices<IRoutingHandler>();
2023-09-19 12:17:50 +00:00
int loopCount = 0;
2023-09-23 21:33:05 +00:00
var stop = false;
while (!stop && loopCount < 5)
2023-09-19 12:17:50 +00:00
{
loopCount++;
var inst = await GetNextInstruction();
2023-10-27 15:18:48 +00:00
inst.MessageId = inputMsg.MessageId;
inst.Question = inst.Question ?? inputMsg.Content;
2023-09-19 12:17:50 +00:00
var handler = handlers.FirstOrDefault(x => x.Name == inst.Function);
2023-09-22 20:38:58 +00:00
if (handler == null)
{
handler = handlers.FirstOrDefault(x => x.Name == "get_next_instruction");
continue;
}
handler.SetRouter(router);
2023-09-25 22:46:00 +00:00
handler.SetDialogs(Dialogs);
2023-09-19 12:17:50 +00:00
result = await handler.Handle(this, inst);
2023-10-27 15:18:48 +00:00
result.MessageId = inputMsg.MessageId;
2023-09-19 12:17:50 +00:00
2023-09-23 21:33:05 +00:00
stop = !_settings.EnableReasoning;
2023-09-19 12:17:50 +00:00
}
2023-09-22 20:38:58 +00:00
return result;
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
}