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;
|
2023-08-31 15:48:59 +00:00
|
|
|
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;
|
2023-10-02 20:16:35 +00:00
|
|
|
using BotSharp.Abstraction.Routing.Models;
|
2024-02-28 16:21:14 +00:00
|
|
|
using BotSharp.Abstraction.Routing;
|
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-10-16 20:07:07 +00:00
|
|
|
public override async Task OnMessageReceived(RoleDialogModel message)
|
2023-08-31 02:09:38 +00:00
|
|
|
{
|
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
|
2024-02-28 16:21:14 +00:00
|
|
|
var context = _services.GetRequiredService<IRoutingContext>();
|
2023-10-02 20:16:35 +00:00
|
|
|
context.IntentName = intentClassifier.Predict(vector);
|
2023-08-31 22:00:31 +00:00
|
|
|
|
2023-10-02 20:16:35 +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-26 04:32:48 +00:00
|
|
|
bool saveFlag = _agent.Type != AgentType.Routing;
|
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
|
|
|
}
|