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

123 lines
4 KiB
C#
Raw Normal View History

2023-09-19 12:17:50 +00:00
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Routing.Models;
2023-09-20 10:31:50 +00:00
using BotSharp.Abstraction.Routing.Settings;
using BotSharp.Abstraction.Templating;
2023-09-22 20:38:58 +00:00
using System.Runtime.InteropServices;
2023-09-19 12:17:50 +00:00
namespace BotSharp.Core.Routing;
public class RoutingService : IRoutingService
{
private readonly IServiceProvider _services;
2023-09-20 10:31:50 +00:00
private readonly RoutingSettings _settings;
2023-09-19 12:17:50 +00:00
private readonly ILogger _logger;
private List<RoleDialogModel> _dialogs;
public List<RoleDialogModel> Dialogs => _dialogs;
2023-09-20 10:31:50 +00:00
public RoutingService(IServiceProvider services,
RoutingSettings settings,
ILogger<RoutingService> logger)
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;
}
2023-09-22 20:38:58 +00:00
public async Task<RoleDialogModel> Enter(Agent router, List<RoleDialogModel> wholeDialogs)
2023-09-19 12:17:50 +00:00
{
_dialogs = new List<RoleDialogModel>();
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-09-22 20:38:58 +00:00
var message = wholeDialogs.Last().Content;
foreach (var dialog in wholeDialogs.TakeLast(20))
2023-09-19 12:17:50 +00:00
{
2023-09-21 13:08:27 +00:00
router.Instruction += $"\r\n{dialog.Role}: {dialog.Content}";
2023-09-19 12:17:50 +00:00
}
2023-09-22 20:38:58 +00:00
var handlers = _services.GetServices<IRoutingHandler>();
var handler = handlers.FirstOrDefault(x => x.Name == "get_next_instruction");
handler.SetRouter(router);
handler.SetDialogs(wholeDialogs);
2023-09-19 12:17:50 +00:00
int loopCount = 0;
2023-09-22 20:38:58 +00:00
while (!result.StopCompletion && loopCount < 5)
2023-09-19 12:17:50 +00:00
{
loopCount++;
2023-09-22 20:38:58 +00:00
var inst = await handler.GetNextInstructionFromReasoner($"What's the next step to achieve user's goal?");
inst.Question = inst.Question ?? message;
2023-09-19 12:17:50 +00:00
2023-09-22 20:38:58 +00:00
handler = handlers.FirstOrDefault(x => x.Name == inst.Function);
if (handler == null)
{
handler = handlers.FirstOrDefault(x => x.Name == "get_next_instruction");
router.Instruction += $"\r\n{AgentRole.System}: the function must be one of [{string.Join(",", GetHandlers().Select(x => x.Name))}].";
continue;
}
handler.SetRouter(router);
handler.SetDialogs(wholeDialogs);
2023-09-19 12:17:50 +00:00
2023-09-22 20:38:58 +00:00
result = await handler.Handle(inst);
2023-09-19 12:17:50 +00:00
2023-09-22 20:38:58 +00:00
message = result.Content.Replace("\r\n", " ");
router.Instruction += $"\r\n{result.Role}: {message}";
2023-09-19 12:17:50 +00:00
2023-09-22 20:38:58 +00:00
result.StopCompletion = !_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
}
2023-09-20 10:31:50 +00:00
public Agent LoadRouter()
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var router = new Agent()
{
Id = _settings.RouterId,
2023-09-21 16:40:54 +00:00
Name = _settings.RouterName,
Description = _settings.Description
2023-09-20 10:31:50 +00:00
};
var agents = db.Agents.Where(x => !x.Disabled && x.AllowRouting).ToArray();
var dict = new Dictionary<string, object>();
dict["routing_records"] = agents.Select(x => new RoutingItem
{
AgentId = x.Id,
Description = x.Description,
Name = x.Name,
RequiredFields = x.RoutingRules.Where(x => x.Required)
.Select(x => x.Field)
.ToArray()
}).ToArray();
2023-09-22 20:38:58 +00:00
dict["routing_handlers"] = GetHandlers();
2023-09-20 10:31:50 +00:00
var render = _services.GetRequiredService<ITemplateRender>();
2023-09-21 15:10:24 +00:00
router.Instruction = render.Render(PromptConst.ROUTER_PROMPT, dict);
2023-09-20 10:31:50 +00:00
return router;
}
2023-09-22 20:38:58 +00:00
private List<RoutingHandlerDef> GetHandlers()
{
return _services.GetServices<IRoutingHandler>()
.Where(x => x.IsReasoning == _settings.EnableReasoning)
.Where(x => !string.IsNullOrEmpty(x.Description))
.Select(x => new RoutingHandlerDef
{
Name = x.Name,
Description = x.Description,
Parameters = x.Parameters
}).ToList();
}
2023-09-19 12:17:50 +00:00
}