diff --git a/BotSharp.sln b/BotSharp.sln index 95a2b229..1d78c41a 100644 --- a/BotSharp.sln +++ b/BotSharp.sln @@ -49,6 +49,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.LLamaSharp" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.PizzaBot", "tests\BotSharp.Plugin.PizzaBot\BotSharp.Plugin.PizzaBot.csproj", "{A1118A2C-C6D7-4E22-9462-964AEC7CC46E}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tools", "Tools", "{51AFE054-AE99-497D-A593-69BAEFB5106F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.RoutingSpeeder", "src\Plugins\BotSharp.Plugin.RoutingSpeeder\BotSharp.Plugin.RoutingSpeeder.csproj", "{631D9C12-86C4-44F0-99C3-D32C0754BF37}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -177,6 +181,14 @@ Global {A1118A2C-C6D7-4E22-9462-964AEC7CC46E}.Release|Any CPU.Build.0 = Release|Any CPU {A1118A2C-C6D7-4E22-9462-964AEC7CC46E}.Release|x64.ActiveCfg = Release|Any CPU {A1118A2C-C6D7-4E22-9462-964AEC7CC46E}.Release|x64.Build.0 = Release|Any CPU + {631D9C12-86C4-44F0-99C3-D32C0754BF37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {631D9C12-86C4-44F0-99C3-D32C0754BF37}.Debug|Any CPU.Build.0 = Debug|Any CPU + {631D9C12-86C4-44F0-99C3-D32C0754BF37}.Debug|x64.ActiveCfg = Debug|Any CPU + {631D9C12-86C4-44F0-99C3-D32C0754BF37}.Debug|x64.Build.0 = Debug|Any CPU + {631D9C12-86C4-44F0-99C3-D32C0754BF37}.Release|Any CPU.ActiveCfg = Release|Any CPU + {631D9C12-86C4-44F0-99C3-D32C0754BF37}.Release|Any CPU.Build.0 = Release|Any CPU + {631D9C12-86C4-44F0-99C3-D32C0754BF37}.Release|x64.ActiveCfg = Release|Any CPU + {631D9C12-86C4-44F0-99C3-D32C0754BF37}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -201,6 +213,8 @@ Global {7E63F5F8-4EA0-498B-ABFE-2BBE4D7DDBA7} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749} {46B7B54F-1425-4C9D-824A-9B826855D249} = {D5293208-2BEF-42FC-A64C-5954F61720BA} {A1118A2C-C6D7-4E22-9462-964AEC7CC46E} = {32FAFFFE-A4CB-4FEE-BF7C-84518BBC6DCC} + {51AFE054-AE99-497D-A593-69BAEFB5106F} = {2635EC9B-2E5F-4313-AC21-0B847F31F36C} + {631D9C12-86C4-44F0-99C3-D32C0754BF37} = {51AFE054-AE99-497D-A593-69BAEFB5106F} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19} diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs index 4e8de8b3..b207bbcb 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs @@ -1,5 +1,3 @@ -using BotSharp.Abstraction.Conversations.Models; - namespace BotSharp.Abstraction.Conversations; public abstract class ConversationHookBase : IConversationHook @@ -35,7 +33,7 @@ public abstract class ConversationHookBase : IConversationHook return Task.CompletedTask; } - public virtual Task BeforeCompletion() + public virtual Task BeforeCompletion(RoleDialogModel message) { return Task.CompletedTask; } diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs index 6b2fdccb..b677da3f 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs @@ -1,6 +1,3 @@ -using BotSharp.Abstraction.Conversations.Models; -using BotSharp.Abstraction.MLTasks; - namespace BotSharp.Abstraction.Conversations; public interface IConversationHook @@ -22,7 +19,7 @@ public interface IConversationHook Task OnStateLoaded(ConversationState state); Task OnStateChanged(string name, string preValue, string currentValue); - Task BeforeCompletion(); + Task BeforeCompletion(RoleDialogModel message); Task OnFunctionExecuting(RoleDialogModel message); Task OnFunctionExecuted(RoleDialogModel message); Task AfterCompletion(RoleDialogModel message); diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs index e80c9427..063a5432 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs @@ -30,6 +30,11 @@ public class RoleDialogModel /// public object ExecutionData { get; set; } + /// + /// Stop conversation completion + /// + public bool StopCompletion { get; set; } + /// /// Channel name /// diff --git a/src/Infrastructure/BotSharp.Abstraction/Plugins/PluginLoaderSettings.cs b/src/Infrastructure/BotSharp.Abstraction/Plugins/PluginLoaderSettings.cs index f23b70f8..95b1ea28 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Plugins/PluginLoaderSettings.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Plugins/PluginLoaderSettings.cs @@ -3,5 +3,4 @@ namespace BotSharp.Abstraction.Plugins; public class PluginLoaderSettings { public string[] Assemblies { get; set; } - public string[] Plugins { get; set; } } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.GetChatCompletionsAsyncRecursively.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.GetChatCompletionsAsyncRecursively.cs index cad2a556..21e8950c 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.GetChatCompletionsAsyncRecursively.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.GetChatCompletionsAsyncRecursively.cs @@ -1,12 +1,7 @@ using BotSharp.Abstraction.Agents.Enums; using BotSharp.Abstraction.Agents.Models; -using BotSharp.Abstraction.Conversations.Models; using BotSharp.Abstraction.MLTasks; using BotSharp.Abstraction.Templating; -using BotSharp.Core.Templating; -using System.IO; -using Tensorflow.Keras.Layers.Rnn; -using static System.Net.Mime.MediaTypeNames; namespace BotSharp.Core.Conversations.Services; diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs index 322b549f..d44eb56a 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs @@ -66,7 +66,16 @@ public partial class ConversationService .SetConversation(converation); await hook.OnDialogsLoaded(wholeDialogs); - await hook.BeforeCompletion(); + await hook.BeforeCompletion(lastDialog); + + // Interrupted by hook + if (lastDialog.StopCompletion) + { + var response = new RoleDialogModel(AgentRole.Assistant, lastDialog.Content); + await onMessageReceived(response); + _storage.Append(conversationId, agent.Id, response); + return true; + } } // reasoning diff --git a/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs b/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs index 60bc3d3d..1287a6d9 100644 --- a/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs +++ b/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs @@ -40,15 +40,8 @@ public class PluginLoader foreach (var module in modules) { - if (_settings.Plugins.Contains(module.GetType().Name)) - { - module.RegisterDI(_services, _config); - Console.WriteLine($"Loaded plugin {module.GetType().Name} from {assemblyName}.", Color.Green); - } - else - { - Console.WriteLine($"Skipped plugin {module.GetType().Name} from {assemblyName}.", Color.Yellow); - } + module.RegisterDI(_services, _config); + Console.WriteLine($"Loaded plugin {module.GetType().Name} from {assemblyName}.", Color.Green); } _modules.AddRange(modules); @@ -62,7 +55,7 @@ public class PluginLoader public void Configure(IApplicationBuilder app) { - if(_modules.Count == 0) + if (_modules.Count == 0) { Console.WriteLine($"No plugin loaded. Please check whether the Load() method is called.", Color.Yellow); } @@ -71,10 +64,7 @@ public class PluginLoader { if (module.GetType().GetInterface(nameof(IBotSharpAppPlugin)) != null) { - if (_settings.Plugins.Contains(module.GetType().Name)) - { - (module as IBotSharpAppPlugin).Configure(app); - } + (module as IBotSharpAppPlugin).Configure(app); } }); } diff --git a/src/Plugins/BotSharp.Plugin.RoutingSpeeder/BotSharp.Plugin.RoutingSpeeder.csproj b/src/Plugins/BotSharp.Plugin.RoutingSpeeder/BotSharp.Plugin.RoutingSpeeder.csproj new file mode 100644 index 00000000..79a54b27 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.RoutingSpeeder/BotSharp.Plugin.RoutingSpeeder.csproj @@ -0,0 +1,14 @@ + + + + netstandard2.1 + enable + 10 + 0.11.0 + + + + + + + diff --git a/src/Plugins/BotSharp.Plugin.RoutingSpeeder/RoutingConversationHook.cs b/src/Plugins/BotSharp.Plugin.RoutingSpeeder/RoutingConversationHook.cs new file mode 100644 index 00000000..50d74fe8 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.RoutingSpeeder/RoutingConversationHook.cs @@ -0,0 +1,15 @@ +using BotSharp.Abstraction.Conversations; +using BotSharp.Abstraction.Conversations.Models; +using System.Threading.Tasks; + +namespace BotSharp.Plugin.RoutingSpeeder; + +public class RoutingConversationHook: ConversationHookBase +{ + public override async Task BeforeCompletion(RoleDialogModel message) + { + // Utilize local discriminative model to predict intent + message.Content = "response content"; + message.StopCompletion = true; + } +} diff --git a/src/Plugins/BotSharp.Plugin.RoutingSpeeder/RoutingSpeederPlugin.cs b/src/Plugins/BotSharp.Plugin.RoutingSpeeder/RoutingSpeederPlugin.cs new file mode 100644 index 00000000..28c856ba --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.RoutingSpeeder/RoutingSpeederPlugin.cs @@ -0,0 +1,14 @@ +using BotSharp.Abstraction.Conversations; +using BotSharp.Abstraction.Plugins; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace BotSharp.Plugin.RoutingSpeeder; + +public class RoutingSpeederPlugin : IBotSharpPlugin +{ + public void RegisterDI(IServiceCollection services, IConfiguration config) + { + services.AddScoped(); + } +} diff --git a/src/WebStarter/WebStarter.csproj b/src/WebStarter/WebStarter.csproj index 311957bb..0cb02ead 100644 --- a/src/WebStarter/WebStarter.csproj +++ b/src/WebStarter/WebStarter.csproj @@ -44,6 +44,7 @@ + diff --git a/src/WebStarter/appsettings.json b/src/WebStarter/appsettings.json index 41ef16d5..42ef6319 100644 --- a/src/WebStarter/appsettings.json +++ b/src/WebStarter/appsettings.json @@ -105,18 +105,8 @@ "BotSharp.Plugin.Qdrant", "BotSharp.Plugin.PaddleSharp", "BotSharp.Plugin.WeChat", + "BotSharp.Plugin.RoutingSpeeder", "BotSharp.Plugin.PizzaBot" - ], - "Plugins": [ - "KnowledgeBasePlugin", - "MemVecDbPlugin", - "LLamaSharpPlugin", - "AzureOpenAiPlugin", - "MetaAiPlugin", - "QdrantPlugin", - "PaddleSharpPlugin", - "WeChatPlugin", - "PizzaBotPlugin" ] } }