Allow hook to interrupt completion.

This commit is contained in:
Haiping Chen 2023-08-30 21:09:38 -05:00
parent 4dcb00ca83
commit 20a7676442
13 changed files with 80 additions and 39 deletions

View file

@ -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}

View file

@ -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;
}

View file

@ -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);

View file

@ -30,6 +30,11 @@ public class RoleDialogModel
/// </summary>
public object ExecutionData { get; set; }
/// <summary>
/// Stop conversation completion
/// </summary>
public bool StopCompletion { get; set; }
/// <summary>
/// Channel name
/// </summary>

View file

@ -3,5 +3,4 @@ namespace BotSharp.Abstraction.Plugins;
public class PluginLoaderSettings
{
public string[] Assemblies { get; set; }
public string[] Plugins { get; set; }
}

View file

@ -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;

View file

@ -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

View file

@ -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);
}
});
}

View file

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>10</LangVersion>
<VersionPrefix>0.11.0</VersionPrefix>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
</ItemGroup>
</Project>

View file

@ -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;
}
}

View file

@ -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<IConversationHook, RoutingConversationHook>();
}
}

View file

@ -44,6 +44,7 @@
<ProjectReference Include="..\Plugins\BotSharp.Plugin.MetaAI\BotSharp.Plugin.MetaAI.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.PaddleSharp\BotSharp.Plugin.PaddleSharp.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.Qdrant\BotSharp.Plugin.Qdrant.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.RoutingSpeeder\BotSharp.Plugin.RoutingSpeeder.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.WeChat\BotSharp.Plugin.WeChat.csproj" />
</ItemGroup>

View file

@ -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"
]
}
}