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 BotSharp.Plugin.RoutingSpeeder.Settings;
|
2023-08-31 15:48:59 +00:00
|
|
|
using BotSharp.Abstraction.Templating;
|
|
|
|
|
using BotSharp.Plugin.RoutingSpeeder.Providers;
|
2023-08-31 22:00:31 +00:00
|
|
|
using System.Runtime.InteropServices;
|
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;
|
2023-08-31 15:48:59 +00:00
|
|
|
private RouterSpeederSettings _settings;
|
|
|
|
|
public RoutingConversationHook(IServiceProvider service, RouterSpeederSettings settings)
|
2023-08-31 14:52:09 +00:00
|
|
|
{
|
|
|
|
|
_services = service;
|
|
|
|
|
_settings = settings;
|
|
|
|
|
}
|
2023-08-31 02:09:38 +00:00
|
|
|
public override async Task BeforeCompletion(RoleDialogModel message)
|
|
|
|
|
{
|
2023-08-31 15:48:59 +00:00
|
|
|
var intentClassifier = _services.GetRequiredService<IntentClassifier>();
|
|
|
|
|
var vector = intentClassifier.GetTextEmbedding(message.Content);
|
2023-08-31 14:52:09 +00:00
|
|
|
|
2023-08-31 22:00:31 +00:00
|
|
|
// intentClassifier.Train();
|
2023-08-31 02:09:38 +00:00
|
|
|
// Utilize local discriminative model to predict intent
|
2023-08-31 22:00:31 +00:00
|
|
|
var predText = intentClassifier.Predict(vector);
|
|
|
|
|
|
|
|
|
|
message.IntentName = predText;
|
2023-08-31 11:40:51 +00:00
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
}
|