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

82 lines
2.9 KiB
C#
Raw Normal View History

2023-08-31 11:40:51 +00:00
using BotSharp.Abstraction.Agents.Enums;
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 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-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;
using BotSharp.Abstraction.Routing.Models;
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-10-16 20:07:07 +00:00
public override async Task OnMessageReceived(RoleDialogModel message)
2023-08-31 02:09:38 +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
var context = _services.GetRequiredService<RoutingContext>();
context.IntentName = intentClassifier.Predict(vector);
2023-08-31 22:00:31 +00:00
if (string.IsNullOrEmpty(context.IntentName))
2023-09-09 20:13:19 +00:00
{
return;
}
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
2023-10-16 20:07:07 +00:00
public override async Task OnResponseGenerated(RoleDialogModel message)
2023-09-01 17:42:18 +00:00
{
2023-09-01 18:37:18 +00:00
var routerSettings = _services.GetRequiredService<RoutingSettings>();
2024-01-13 21:17:40 +00:00
bool saveFlag = !routerSettings.AgentIds.Contains(message.CurrentAgentId);
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();
2023-09-02 03:02:27 +00:00
string rawDataDir = Path.Combine(rootDataPath, "raw_data", $"agent.{message.CurrentAgentId}.txt");
var lastThreeDialogs = _dialogs.Where(x => x.Role == AgentRole.User || x.Role == AgentRole.Assistant)
.Select(x => x.Content.Replace('\r', ' ').Replace('\n', ' '))
.TakeLast(3)
.ToArray();
2023-09-01 18:37:18 +00:00
2023-09-02 03:10:41 +00:00
var content = string.Join(' ', lastThreeDialogs) + Environment.NewLine;
2023-09-01 18:37:18 +00:00
if (!File.Exists(rawDataDir))
{
2023-09-02 03:10:41 +00:00
await File.WriteAllTextAsync(rawDataDir, content);
2023-09-01 18:37:18 +00:00
}
else
{
2023-09-02 03:10:41 +00:00
await File.AppendAllTextAsync(rawDataDir, content);
2023-09-01 18:37:18 +00:00
}
2023-09-01 17:42:18 +00:00
}
}
2023-08-31 02:09:38 +00:00
}