BotSharp/src/Plugins/BotSharp.Plugin.RoutingSpeeder/RoutingConversationHook.cs

76 lines
2.7 KiB
C#
Raw Normal View History

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;
using BotSharp.Abstraction.Templating;
using BotSharp.Plugin.RoutingSpeeder.Providers;
2023-08-31 22:00:31 +00:00
using System.Runtime.InteropServices;
2023-09-01 17:42:18 +00:00
using BotSharp.Abstraction.Agents;
using System.IO;
2023-09-01 18:37:18 +00:00
using BotSharp.Abstraction.Routing.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 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)
{
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
}
2023-09-01 17:42:18 +00:00
public override async Task AfterCompletion(RoleDialogModel message)
{
2023-09-01 18:37:18 +00:00
var routerSettings = _services.GetRequiredService<RoutingSettings>();
bool saveFlag = (message.CurrentAgentId != routerSettings.RouterId) && (message.CurrentAgentId != routerSettings.ReasonerId);
2023-09-01 17:42:18 +00:00
2023-09-01 18:37:18 +00:00
if (saveFlag)
2023-09-01 17:42:18 +00:00
{
2023-09-01 18:37:18 +00:00
// save train data
var agentService = _services.CreateScope().ServiceProvider.GetRequiredService<IAgentService>();
var rootDataPath = agentService.GetDataDir();
string rawDataDir = Path.Combine(rootDataPath, "raw_data", $"{message.CurrentAgentId}.txt");
var lastThreeDialogs = _dialogs.Where(x => x.Role == AgentRole.User).Select(x => x.Content).Reverse().Take(3).ToArray();
if (!File.Exists(rawDataDir))
{
await File.WriteAllLinesAsync(rawDataDir, lastThreeDialogs);
}
else
{
await File.AppendAllLinesAsync(rawDataDir, lastThreeDialogs);
}
2023-09-01 17:42:18 +00:00
}
}
2023-08-31 02:09:38 +00:00
}