2023-08-31 11:40:51 +00:00
|
|
|
using BotSharp.Abstraction.Agents.Enums;
|
|
|
|
|
using BotSharp.Abstraction.Agents.Models;
|
2023-08-31 02:09:38 +00:00
|
|
|
using BotSharp.Abstraction.Conversations;
|
|
|
|
|
using BotSharp.Abstraction.Conversations.Models;
|
2023-08-31 14:52:09 +00:00
|
|
|
using BotSharp.Abstraction.MLTasks;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2023-08-31 02:09:38 +00:00
|
|
|
using System.Threading.Tasks;
|
2023-08-31 14:52:09 +00:00
|
|
|
using FastText.NetWrapper;
|
|
|
|
|
using BotSharp.Plugin.RoutingSpeeder.Settings;
|
2023-08-31 02:09:38 +00:00
|
|
|
|
|
|
|
|
namespace BotSharp.Plugin.RoutingSpeeder;
|
|
|
|
|
|
|
|
|
|
public class RoutingConversationHook: ConversationHookBase
|
|
|
|
|
{
|
2023-08-31 14:52:09 +00:00
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
private routerSpeedSettings _settings;
|
|
|
|
|
public RoutingConversationHook(IServiceProvider service, routerSpeedSettings settings)
|
|
|
|
|
{
|
|
|
|
|
_services = service;
|
|
|
|
|
_settings = settings;
|
|
|
|
|
}
|
2023-08-31 02:09:38 +00:00
|
|
|
public override async Task BeforeCompletion(RoleDialogModel message)
|
|
|
|
|
{
|
2023-08-31 14:52:09 +00:00
|
|
|
var embedding = _services.GetServices<ITextEmbedding>()
|
|
|
|
|
.FirstOrDefault(x => x.GetType().FullName.EndsWith(_settings.TextEmbedding));
|
|
|
|
|
|
2023-08-31 02:09:38 +00:00
|
|
|
// Utilize local discriminative model to predict intent
|
2023-08-31 11:40:51 +00:00
|
|
|
message.IntentName = "greeting";
|
|
|
|
|
|
|
|
|
|
// Render by template
|
|
|
|
|
var templateService = _services.GetRequiredService<IResponseTemplateService>();
|
|
|
|
|
var response = await templateService.RenderIntentResponse(_agent.Id, message);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(response))
|
|
|
|
|
{
|
|
|
|
|
message.Content = response;
|
|
|
|
|
message.StopCompletion = true;
|
|
|
|
|
}
|
2023-08-31 02:09:38 +00:00
|
|
|
}
|
|
|
|
|
}
|