From 9f74b04fd783a3869ff9424a61c98729058e4ef9 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Wed, 6 Sep 2023 07:58:02 -0500 Subject: [PATCH] Separate KnoledgeBase as standalone plugin. --- BotSharp.sln | 16 ++++++- Directory.Build.props | 8 ++++ .../Agents}/AgentHookBase.cs | 7 ++- .../Agents/Settings/AgentSettings.cs | 4 +- .../BotSharp.Abstraction.csproj | 5 +- .../Plugins/PluginLoaderSettings.cs | 2 +- .../Routing/Settings/RoutingSettings.cs | 4 +- .../BotSharp.Core/BotSharp.Core.csproj | 7 ++- .../BotSharpServiceCollectionExtensions.cs | 4 -- .../ConversationService.SendMessage.cs | 11 ----- .../PluginLoader.cs | 2 +- src/Infrastructure/BotSharp.Core/Using.cs | 1 - .../BotSharp.OpenAPI/BotSharp.OpenAPI.csproj | 4 +- .../Controllers/KnowledgeController.cs | 3 -- .../ViewModels/Users/UserCreationModel.cs | 8 ++-- .../BotSharp.Plugin.AzureOpenAI.csproj | 5 +- .../BotSharp.Plugin.ChatbotUI.csproj | 5 +- .../BotSharp.Plugin.HuggingFace.csproj | 7 +-- .../BotSharp.Plugin.KnowledgeBase.csproj | 20 ++++++++ .../KnowledgeBaseAgentHook.cs | 26 ++++++++++ .../KnowledgeBasePlugin.cs | 5 +- .../MemVecDb/MemVecDbPlugin.cs | 3 +- .../MemVecDb/MemVectorDatabase.cs | 4 +- .../MemVecDb/VecRecord.cs | 2 +- .../Services/KnowledgeService.cs | 7 +-- .../Services/PigPdf2TextConverter.cs | 6 +-- .../Services/TextChopperService.cs | 3 +- .../BotSharp.Plugin.KnowledgeBase/Using.cs | 23 +++++++++ .../BotSharp.Plugin.LLamaSharp.csproj | 7 +-- .../LlamaAiModel.cs | 30 ++++++++++-- .../Providers/ChatCompletionProvider.cs | 48 ++++++++++++++----- .../Providers/TextCompletionProvider.cs | 2 +- .../BotSharp.Plugin.MetaAI.csproj | 5 +- .../BotSharp.Plugin.MetaMessenger.csproj | 5 +- .../BotSharp.Plugin.PaddleSharp.csproj | 5 +- .../BotSharp.Plugin.Qdrant.csproj | 5 +- .../BotSharp.Plugin.RoutingSpeeder.csproj | 5 +- .../BotSharp.Plugin.WeChat.csproj | 6 ++- src/WebStarter/WebStarter.csproj | 5 +- src/WebStarter/appsettings.json | 8 ++-- .../BotSharp.Plugin.PizzaBot.csproj | 3 +- .../Hooks/PizzaBotAgentHook.cs | 9 ++-- 42 files changed, 234 insertions(+), 111 deletions(-) create mode 100644 Directory.Build.props rename src/Infrastructure/{BotSharp.Core/Agents/Services => BotSharp.Abstraction/Agents}/AgentHookBase.cs (78%) rename src/Infrastructure/BotSharp.Core/{Plugins => Infrastructures}/PluginLoader.cs (98%) create mode 100644 src/Plugins/BotSharp.Plugin.KnowledgeBase/BotSharp.Plugin.KnowledgeBase.csproj create mode 100644 src/Plugins/BotSharp.Plugin.KnowledgeBase/KnowledgeBaseAgentHook.cs rename src/{Infrastructure/BotSharp.Core/Plugins/Knowledges => Plugins/BotSharp.Plugin.KnowledgeBase}/KnowledgeBasePlugin.cs (76%) rename src/{Infrastructure/BotSharp.Core/Plugins => Plugins/BotSharp.Plugin.KnowledgeBase}/MemVecDb/MemVecDbPlugin.cs (74%) rename src/{Infrastructure/BotSharp.Core/Plugins => Plugins/BotSharp.Plugin.KnowledgeBase}/MemVecDb/MemVectorDatabase.cs (97%) rename src/{Infrastructure/BotSharp.Core/Plugins => Plugins/BotSharp.Plugin.KnowledgeBase}/MemVecDb/VecRecord.cs (73%) rename src/{Infrastructure/BotSharp.Core/Plugins/Knowledges => Plugins/BotSharp.Plugin.KnowledgeBase}/Services/KnowledgeService.cs (93%) rename src/{Infrastructure/BotSharp.Core/Plugins/Knowledges => Plugins/BotSharp.Plugin.KnowledgeBase}/Services/PigPdf2TextConverter.cs (89%) rename src/{Infrastructure/BotSharp.Core/Plugins/Knowledges => Plugins/BotSharp.Plugin.KnowledgeBase}/Services/TextChopperService.cs (93%) create mode 100644 src/Plugins/BotSharp.Plugin.KnowledgeBase/Using.cs diff --git a/BotSharp.sln b/BotSharp.sln index 1d78c41a..6b0f9a57 100644 --- a/BotSharp.sln +++ b/BotSharp.sln @@ -51,7 +51,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.PizzaBot", 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}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.RoutingSpeeder", "src\Plugins\BotSharp.Plugin.RoutingSpeeder\BotSharp.Plugin.RoutingSpeeder.csproj", "{631D9C12-86C4-44F0-99C3-D32C0754BF37}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "RAGs", "RAGs", "{4F346DCE-087F-4368-AF88-EE9C720D0E69}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.KnowledgeBase", "src\Plugins\BotSharp.Plugin.KnowledgeBase\BotSharp.Plugin.KnowledgeBase.csproj", "{298AC787-A104-414C-B114-82BE764FBD9C}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -189,6 +193,14 @@ Global {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 + {298AC787-A104-414C-B114-82BE764FBD9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {298AC787-A104-414C-B114-82BE764FBD9C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {298AC787-A104-414C-B114-82BE764FBD9C}.Debug|x64.ActiveCfg = Debug|Any CPU + {298AC787-A104-414C-B114-82BE764FBD9C}.Debug|x64.Build.0 = Debug|Any CPU + {298AC787-A104-414C-B114-82BE764FBD9C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {298AC787-A104-414C-B114-82BE764FBD9C}.Release|Any CPU.Build.0 = Release|Any CPU + {298AC787-A104-414C-B114-82BE764FBD9C}.Release|x64.ActiveCfg = Release|Any CPU + {298AC787-A104-414C-B114-82BE764FBD9C}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -215,6 +227,8 @@ Global {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} + {4F346DCE-087F-4368-AF88-EE9C720D0E69} = {2635EC9B-2E5F-4313-AC21-0B847F31F36C} + {298AC787-A104-414C-B114-82BE764FBD9C} = {4F346DCE-087F-4368-AF88-EE9C720D0E69} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19} diff --git a/Directory.Build.props b/Directory.Build.props new file mode 100644 index 00000000..f25af14c --- /dev/null +++ b/Directory.Build.props @@ -0,0 +1,8 @@ + + + 10.0 + ..\..\..\packages + 0.11.0 + true + + \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentHookBase.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs similarity index 78% rename from src/Infrastructure/BotSharp.Core/Agents/Services/AgentHookBase.cs rename to src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs index eaf28792..6c40d3da 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentHookBase.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs @@ -1,6 +1,6 @@ -using BotSharp.Abstraction.Agents.Models; +using BotSharp.Abstraction.Agents.Settings; -namespace BotSharp.Core.Agents.Services; +namespace BotSharp.Abstraction.Agents; public abstract class AgentHookBase : IAgentHook { @@ -28,6 +28,9 @@ public abstract class AgentHookBase : IAgentHook public virtual bool OnInstructionLoaded(string template, Dictionary dict) { + dict["current_date"] = $"{DateTime.Now:MMM dd, yyyy}"; + dict["current_time"] = $"{DateTime.Now:hh:mm tt}"; + dict["current_weekday"] = $"{DateTime.Now:dddd}"; return true; } diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Settings/AgentSettings.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Settings/AgentSettings.cs index bf242ecc..9cc514bb 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Settings/AgentSettings.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Settings/AgentSettings.cs @@ -2,6 +2,6 @@ namespace BotSharp.Abstraction.Agents.Settings; public class AgentSettings { - public string DataDir { get; set; } - public string TemplateFormat { get; set; } + public string DataDir { get; set; } = string.Empty; + public string TemplateFormat { get; set; } = "liquid"; } diff --git a/src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj b/src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj index fc457ebd..dcb9534f 100644 --- a/src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj +++ b/src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj @@ -3,9 +3,10 @@ netstandard2.1 enable - 10.0 - 0.10.1 + $(LangVersion) + $(PackageVersion) Icon.png + $(GeneratePackageOnBuild) diff --git a/src/Infrastructure/BotSharp.Abstraction/Plugins/PluginLoaderSettings.cs b/src/Infrastructure/BotSharp.Abstraction/Plugins/PluginLoaderSettings.cs index 95b1ea28..36b20d47 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Plugins/PluginLoaderSettings.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Plugins/PluginLoaderSettings.cs @@ -2,5 +2,5 @@ namespace BotSharp.Abstraction.Plugins; public class PluginLoaderSettings { - public string[] Assemblies { get; set; } + public string[] Assemblies { get; set; } = new string[0]; } diff --git a/src/Infrastructure/BotSharp.Abstraction/Routing/Settings/RoutingSettings.cs b/src/Infrastructure/BotSharp.Abstraction/Routing/Settings/RoutingSettings.cs index b9cc51a9..26377274 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Routing/Settings/RoutingSettings.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Routing/Settings/RoutingSettings.cs @@ -5,10 +5,10 @@ public class RoutingSettings /// /// Router Agent Id /// - public string RouterId { get; set; } + public string RouterId { get; set; } = string.Empty; /// /// Reasoner Agent Id /// - public string ReasonerId { get; set; } + public string ReasonerId { get; set; } = string.Empty; } diff --git a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj index 763cbf5b..6392a491 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj +++ b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj @@ -2,9 +2,9 @@ netstandard2.1 - 10.0 - false - 0.10.4 + $(LangVersion) + $(PackageVersion) + $(GeneratePackageOnBuild) @@ -76,7 +76,6 @@ - diff --git a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs index 9196a035..1dda05a6 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs +++ b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs @@ -1,10 +1,8 @@ -using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Functions; using BotSharp.Core.Functions; using BotSharp.Core.Hooks; using BotSharp.Core.Routing; using BotSharp.Core.Templating; -using BotSharp.Core.Plugins.Knowledges.Services; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; using BotSharp.Abstraction.Routing.Settings; @@ -113,7 +111,5 @@ public static class BotSharpServiceCollectionExtensions loader.Load(); services.AddSingleton(loader); - - services.AddSingleton(); } } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs index d9ce0967..11ecb1f2 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs @@ -28,17 +28,6 @@ public partial class ConversationService _storage.Append(_conversationId, agent.Id, lastDialog); - // Get relevant domain knowledge - /*if (_settings.EnableKnowledgeBase) - { - var knowledge = _services.GetRequiredService(); - agent.Knowledges = await knowledge.GetKnowledges(new KnowledgeRetrievalModel - { - AgentId = agentId, - Question = string.Join("\n", wholeDialogs.Select(x => x.Content)) - }); - }*/ - var hooks = _services.GetServices().ToList(); // Before chat completion hook diff --git a/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/PluginLoader.cs similarity index 98% rename from src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs rename to src/Infrastructure/BotSharp.Core/Infrastructures/PluginLoader.cs index 1287a6d9..31011a3a 100644 --- a/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/PluginLoader.cs @@ -4,7 +4,7 @@ using System.Drawing; using System.IO; using System.Reflection; -namespace BotSharp.Core.Plugins; +namespace BotSharp.Core.Infrastructures; public class PluginLoader { diff --git a/src/Infrastructure/BotSharp.Core/Using.cs b/src/Infrastructure/BotSharp.Core/Using.cs index dca1b515..986529dd 100644 --- a/src/Infrastructure/BotSharp.Core/Using.cs +++ b/src/Infrastructure/BotSharp.Core/Using.cs @@ -22,5 +22,4 @@ global using BotSharp.Core.Repository.DbTables; global using BotSharp.Core.Agents.Services; global using BotSharp.Core.Conversations.Services; global using BotSharp.Core.Infrastructures; -global using BotSharp.Core.Plugins; global using BotSharp.Core.Users.Services; \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj b/src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj index f8fbe32d..bbbf3ddf 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj +++ b/src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj @@ -4,7 +4,9 @@ net6.0 enable enable - 0.10.1 + $(LangVersion) + $(PackageVersion) + $(GeneratePackageOnBuild) diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeController.cs index bea5597b..d30a59eb 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeController.cs @@ -1,9 +1,6 @@ using BotSharp.Abstraction.ApiAdapters; using BotSharp.Abstraction.Knowledges.Models; using Microsoft.AspNetCore.Http; -using UglyToad.PdfPig.Content; -using UglyToad.PdfPig; -using BotSharp.Core.Plugins.Knowledges; using BotSharp.Abstraction.Knowledges.Settings; namespace BotSharp.OpenAPI.Controllers; diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserCreationModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserCreationModel.cs index 76fdb443..77fc69c5 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserCreationModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserCreationModel.cs @@ -4,10 +4,10 @@ namespace BotSharp.OpenAPI.ViewModels.Users; public class UserCreationModel { - public string FirstName { get; set; } - public string LastName { get; set; } - public string Email { get; set; } - public string Password { get; set; } + public string FirstName { get; set; } = string.Empty; + public string LastName { get; set; } = string.Empty; + public string Email { get; set; } = string.Empty; + public string Password { get; set; } = string.Empty; public User ToUser() { diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/BotSharp.Plugin.AzureOpenAI.csproj b/src/Plugins/BotSharp.Plugin.AzureOpenAI/BotSharp.Plugin.AzureOpenAI.csproj index 8894d113..bc3a8a0d 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/BotSharp.Plugin.AzureOpenAI.csproj +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/BotSharp.Plugin.AzureOpenAI.csproj @@ -3,8 +3,9 @@ netstandard2.1 enable - 10 - 0.10.1 + $(LangVersion) + $(PackageVersion) + $(GeneratePackageOnBuild) diff --git a/src/Plugins/BotSharp.Plugin.ChatbotUI/BotSharp.Plugin.ChatbotUI.csproj b/src/Plugins/BotSharp.Plugin.ChatbotUI/BotSharp.Plugin.ChatbotUI.csproj index 86e8c9ce..1ce0ac4d 100644 --- a/src/Plugins/BotSharp.Plugin.ChatbotUI/BotSharp.Plugin.ChatbotUI.csproj +++ b/src/Plugins/BotSharp.Plugin.ChatbotUI/BotSharp.Plugin.ChatbotUI.csproj @@ -3,8 +3,9 @@ netstandard2.1 enable - 10 - 0.10.1 + $(LangVersion) + $(PackageVersion) + $(GeneratePackageOnBuild) diff --git a/src/Plugins/BotSharp.Plugin.HuggingFace/BotSharp.Plugin.HuggingFace.csproj b/src/Plugins/BotSharp.Plugin.HuggingFace/BotSharp.Plugin.HuggingFace.csproj index 1f7fd6a1..c76256da 100644 --- a/src/Plugins/BotSharp.Plugin.HuggingFace/BotSharp.Plugin.HuggingFace.csproj +++ b/src/Plugins/BotSharp.Plugin.HuggingFace/BotSharp.Plugin.HuggingFace.csproj @@ -1,10 +1,11 @@ - + netstandard2.1 enable - 10 - 0.9.0 + $(LangVersion) + $(PackageVersion) + $(GeneratePackageOnBuild) diff --git a/src/Plugins/BotSharp.Plugin.KnowledgeBase/BotSharp.Plugin.KnowledgeBase.csproj b/src/Plugins/BotSharp.Plugin.KnowledgeBase/BotSharp.Plugin.KnowledgeBase.csproj new file mode 100644 index 00000000..a66f1f28 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/BotSharp.Plugin.KnowledgeBase.csproj @@ -0,0 +1,20 @@ + + + + netstandard2.1 + enable + $(LangVersion) + $(PackageVersion) + $(GeneratePackageOnBuild) + + + + + + + + + + + + diff --git a/src/Plugins/BotSharp.Plugin.KnowledgeBase/KnowledgeBaseAgentHook.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/KnowledgeBaseAgentHook.cs new file mode 100644 index 00000000..7163bb39 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/KnowledgeBaseAgentHook.cs @@ -0,0 +1,26 @@ +using System; +namespace BotSharp.Plugin.KnowledgeBase; + +public class KnowledgeBaseAgentHook : AgentHookBase +{ + public KnowledgeBaseAgentHook(IServiceProvider services, AgentSettings settings) + : base(services, settings) + { + } + + public override bool OnInstructionLoaded(string template, Dictionary dict) + { + // Get relevant domain knowledge + /*if (_settings.EnableKnowledgeBase) + { + var knowledge = _services.GetRequiredService(); + agent.Knowledges = await knowledge.GetKnowledges(new KnowledgeRetrievalModel + { + AgentId = agentId, + Question = string.Join("\n", wholeDialogs.Select(x => x.Content)) + }); + }*/ + + return base.OnInstructionLoaded(template, dict); + } +} diff --git a/src/Infrastructure/BotSharp.Core/Plugins/Knowledges/KnowledgeBasePlugin.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/KnowledgeBasePlugin.cs similarity index 76% rename from src/Infrastructure/BotSharp.Core/Plugins/Knowledges/KnowledgeBasePlugin.cs rename to src/Plugins/BotSharp.Plugin.KnowledgeBase/KnowledgeBasePlugin.cs index 8f862ca4..bc36b09f 100644 --- a/src/Infrastructure/BotSharp.Core/Plugins/Knowledges/KnowledgeBasePlugin.cs +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/KnowledgeBasePlugin.cs @@ -1,8 +1,6 @@ -using BotSharp.Abstraction.Knowledges.Settings; -using BotSharp.Core.Plugins.Knowledges.Services; using Microsoft.Extensions.Configuration; -namespace BotSharp.Core.Plugins.Knowledges; +namespace BotSharp.Plugin.KnowledgeBase; public class KnowledgeBasePlugin : IBotSharpPlugin { @@ -14,5 +12,6 @@ public class KnowledgeBasePlugin : IBotSharpPlugin services.AddScoped(); services.AddScoped(); + services.AddSingleton(); } } diff --git a/src/Infrastructure/BotSharp.Core/Plugins/MemVecDb/MemVecDbPlugin.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/MemVecDb/MemVecDbPlugin.cs similarity index 74% rename from src/Infrastructure/BotSharp.Core/Plugins/MemVecDb/MemVecDbPlugin.cs rename to src/Plugins/BotSharp.Plugin.KnowledgeBase/MemVecDb/MemVecDbPlugin.cs index 01110674..114527af 100644 --- a/src/Infrastructure/BotSharp.Core/Plugins/MemVecDb/MemVecDbPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/MemVecDb/MemVecDbPlugin.cs @@ -1,7 +1,6 @@ -using BotSharp.Abstraction.VectorStorage; using Microsoft.Extensions.Configuration; -namespace BotSharp.Core.Plugins.MemVecDb; +namespace BotSharp.Plugin.KnowledgeBase.MemVecDb; public class MemVecDbPlugin : IBotSharpPlugin { diff --git a/src/Infrastructure/BotSharp.Core/Plugins/MemVecDb/MemVectorDatabase.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/MemVecDb/MemVectorDatabase.cs similarity index 97% rename from src/Infrastructure/BotSharp.Core/Plugins/MemVecDb/MemVectorDatabase.cs rename to src/Plugins/BotSharp.Plugin.KnowledgeBase/MemVecDb/MemVectorDatabase.cs index 5aafbacf..38104726 100644 --- a/src/Infrastructure/BotSharp.Core/Plugins/MemVecDb/MemVectorDatabase.cs +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/MemVecDb/MemVectorDatabase.cs @@ -1,9 +1,7 @@ -using BotSharp.Abstraction.VectorStorage; -using Tensorflow; using Tensorflow.NumPy; using static Tensorflow.Binding; -namespace BotSharp.Core.Plugins.MemVecDb; +namespace BotSharp.Plugin.KnowledgeBase.MemVecDb; public class MemVectorDatabase : IVectorDb { diff --git a/src/Infrastructure/BotSharp.Core/Plugins/MemVecDb/VecRecord.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/MemVecDb/VecRecord.cs similarity index 73% rename from src/Infrastructure/BotSharp.Core/Plugins/MemVecDb/VecRecord.cs rename to src/Plugins/BotSharp.Plugin.KnowledgeBase/MemVecDb/VecRecord.cs index b5938c4d..563f0c98 100644 --- a/src/Infrastructure/BotSharp.Core/Plugins/MemVecDb/VecRecord.cs +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/MemVecDb/VecRecord.cs @@ -1,4 +1,4 @@ -namespace BotSharp.Core.Plugins.MemVecDb; +namespace BotSharp.Plugin.KnowledgeBase.MemVecDb; public class VecRecord { diff --git a/src/Infrastructure/BotSharp.Core/Plugins/Knowledges/Services/KnowledgeService.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.cs similarity index 93% rename from src/Infrastructure/BotSharp.Core/Plugins/Knowledges/Services/KnowledgeService.cs rename to src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.cs index 3a627ec9..15a38046 100644 --- a/src/Infrastructure/BotSharp.Core/Plugins/Knowledges/Services/KnowledgeService.cs +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.cs @@ -1,9 +1,4 @@ -using BotSharp.Abstraction.Knowledges.Models; -using BotSharp.Abstraction.Knowledges.Settings; -using BotSharp.Abstraction.MLTasks; -using BotSharp.Abstraction.VectorStorage; - -namespace BotSharp.Core.Plugins.Knowledges.Services; +namespace BotSharp.Plugin.KnowledgeBase.Services; public class KnowledgeService : IKnowledgeService { diff --git a/src/Infrastructure/BotSharp.Core/Plugins/Knowledges/Services/PigPdf2TextConverter.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/PigPdf2TextConverter.cs similarity index 89% rename from src/Infrastructure/BotSharp.Core/Plugins/Knowledges/Services/PigPdf2TextConverter.cs rename to src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/PigPdf2TextConverter.cs index c19ddc77..a8875ec1 100644 --- a/src/Infrastructure/BotSharp.Core/Plugins/Knowledges/Services/PigPdf2TextConverter.cs +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/PigPdf2TextConverter.cs @@ -1,12 +1,8 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; using Microsoft.AspNetCore.Http; using UglyToad.PdfPig; using UglyToad.PdfPig.Content; -namespace BotSharp.Core.Plugins.Knowledges.Services; +namespace BotSharp.Plugin.KnowledgeBase.Services; public class PigPdf2TextConverter : IPdf2TextConverter { diff --git a/src/Infrastructure/BotSharp.Core/Plugins/Knowledges/Services/TextChopperService.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/TextChopperService.cs similarity index 93% rename from src/Infrastructure/BotSharp.Core/Plugins/Knowledges/Services/TextChopperService.cs rename to src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/TextChopperService.cs index 6033e66e..96dc021d 100644 --- a/src/Infrastructure/BotSharp.Core/Plugins/Knowledges/Services/TextChopperService.cs +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/TextChopperService.cs @@ -1,7 +1,6 @@ -using BotSharp.Abstraction.Knowledges.Models; using System.Text.RegularExpressions; -namespace BotSharp.Core.Plugins.Knowledges.Services; +namespace BotSharp.Plugin.KnowledgeBase.Services; public class TextChopperService : ITextChopper { diff --git a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Using.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Using.cs new file mode 100644 index 00000000..60db13a6 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Using.cs @@ -0,0 +1,23 @@ +global using System; +global using System.IO; +global using System.Collections.Generic; +global using System.Text; +global using System.Threading.Tasks; +global using System.Linq; +global using System.Text.Json; +global using Microsoft.Extensions.DependencyInjection; +global using Microsoft.Extensions.Logging; +global using BotSharp.Abstraction.Plugins; +global using BotSharp.Abstraction.Agents; +global using BotSharp.Abstraction.Conversations; +global using BotSharp.Abstraction.Knowledges; +global using BotSharp.Abstraction.Users; +global using BotSharp.Abstraction.Utilities; +global using BotSharp.Abstraction.Conversations.Models; +global using BotSharp.Abstraction.Agents.Settings; +global using BotSharp.Abstraction.Conversations.Settings; +global using BotSharp.Abstraction.Knowledges.Settings; +global using BotSharp.Abstraction.VectorStorage; +global using BotSharp.Abstraction.Knowledges.Models; +global using BotSharp.Abstraction.MLTasks; +global using BotSharp.Plugin.KnowledgeBase.Services; \ No newline at end of file diff --git a/src/Plugins/BotSharp.Plugin.LLamaSharp/BotSharp.Plugin.LLamaSharp.csproj b/src/Plugins/BotSharp.Plugin.LLamaSharp/BotSharp.Plugin.LLamaSharp.csproj index c4f3e435..df8c0e41 100644 --- a/src/Plugins/BotSharp.Plugin.LLamaSharp/BotSharp.Plugin.LLamaSharp.csproj +++ b/src/Plugins/BotSharp.Plugin.LLamaSharp/BotSharp.Plugin.LLamaSharp.csproj @@ -3,12 +3,13 @@ netstandard2.1 enable - 10 - 0.9.0 + $(LangVersion) + $(PackageVersion) + $(GeneratePackageOnBuild) - + diff --git a/src/Plugins/BotSharp.Plugin.LLamaSharp/LlamaAiModel.cs b/src/Plugins/BotSharp.Plugin.LLamaSharp/LlamaAiModel.cs index e32ee1e6..6f3e840c 100644 --- a/src/Plugins/BotSharp.Plugin.LLamaSharp/LlamaAiModel.cs +++ b/src/Plugins/BotSharp.Plugin.LLamaSharp/LlamaAiModel.cs @@ -1,5 +1,6 @@ using BotSharp.Plugin.LLamaSharp.Settings; using LLama; +using LLama.Abstractions; using LLama.Common; namespace BotSharp.Plugins.LLamaSharp; @@ -9,13 +10,25 @@ public class LlamaAiModel private readonly LlamaSharpSettings _settings; public LlamaSharpSettings Settings => _settings; - LLamaModel _model; + LLamaWeights _model; - public LLamaModel Model => _model; + public LLamaWeights Model => _model; + + ModelParams _params; + public ModelParams Params => _params; + + private ILLamaExecutor _statelessExecutor; public LlamaAiModel(LlamaSharpSettings settings) { _settings = settings; + + _params = new ModelParams(_settings.ModelPath) + { + ContextSize = _settings.MaxContextLength, + Seed = 1337, + GpuLayerCount = _settings.NumberOfGpuLayer + }; } @@ -26,8 +39,15 @@ public class LlamaAiModel return; } - _model = new LLamaModel(new ModelParams(_settings.ModelPath, - contextSize: _settings.MaxContextLength, - gpuLayerCount: _settings.NumberOfGpuLayer)); + _model = LLamaWeights.LoadFromFile(_params); + } + + public ILLamaExecutor GetStatelessExecutor() + { + if (_statelessExecutor == null) + { + _statelessExecutor = new StatelessExecutor(_model, _params); + } + return _statelessExecutor; } } diff --git a/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/ChatCompletionProvider.cs index 59e60cb1..e0b13564 100644 --- a/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/ChatCompletionProvider.cs @@ -1,10 +1,13 @@ +using BotSharp.Abstraction.Agents.Enums; using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Conversations.Models; +using BotSharp.Abstraction.Conversations.Settings; using BotSharp.Abstraction.MLTasks; using BotSharp.Plugins.LLamaSharp; using LLama; using LLama.Common; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; @@ -15,9 +18,13 @@ namespace BotSharp.Plugin.LLamaSharp.Providers; public class ChatCompletionProvider : IChatCompletion { private readonly IServiceProvider _services; - public ChatCompletionProvider(IServiceProvider services) + private readonly ILogger _logger; + + public ChatCompletionProvider(IServiceProvider services, + ILogger logger) { _services = services; + _logger = logger; } public string GetChatCompletions(Agent agent, List conversations, Func onMessageReceived) @@ -30,29 +37,42 @@ public class ChatCompletionProvider : IChatCompletion Func onMessageReceived, Func onFunctionExecuting) { - var content = string.Join("\n", conversations.Select(x => $"{x.Role}: {x.Content.Replace("user:", "User:")}")).Trim(); - content += "\nBob: "; + var content = string.Join("\n", conversations.Select(x => $"{x.Role}: {x.Content}")).Trim(); + content += $"\n{AgentRole.Assistant}: "; var llama = _services.GetRequiredService(); llama.LoadModel(); - var executor = new StatelessExecutor(llama.Model); + var executor = llama.GetStatelessExecutor(); + var inferenceParams = new InferenceParams() { Temperature = 1.0f, - AntiPrompts = new List { "User:" }, + AntiPrompts = new List { $"{AgentRole.User}:", "\n", "?" }, MaxTokens = 256 }; string totalResponse = ""; var prompt = agent.Instruction + content; - await foreach (var response in executor.InferAsync(prompt, inferenceParams)) + + var convSetting = _services.GetRequiredService(); + if (convSetting.ShowVerboseLog) + { + _logger.LogInformation(prompt); + } + + foreach (var response in executor.Infer(prompt, inferenceParams)) { Console.Write(response); totalResponse += response; } - await onMessageReceived(new RoleDialogModel("assistant", totalResponse)); + foreach (var anti in inferenceParams.AntiPrompts) + { + totalResponse = totalResponse.Replace(anti, "").Trim(); + } + + await onMessageReceived(new RoleDialogModel(AgentRole.Assistant, totalResponse)); return true; } @@ -60,13 +80,19 @@ public class ChatCompletionProvider : IChatCompletion public async Task GetChatCompletionsStreamingAsync(Agent agent, List conversations, Func onMessageReceived) { string totalResponse = ""; - var content = string.Join("\n", conversations.Select(x => $"{x.Role}: {x.Content.Replace("user:", "")}")).Trim(); - content += "\nassistant: "; + var content = string.Join("\n", conversations.Select(x => $"{x.Role}: {x.Content}")).Trim(); + content += $"\n{AgentRole.Assistant}: "; var llama = _services.GetRequiredService(); llama.LoadModel(); - var executor = new StatelessExecutor(llama.Model); - var inferenceParams = new InferenceParams() { Temperature = 1.0f, AntiPrompts = new List { "user:" }, MaxTokens = 64 }; + var executor = new StatelessExecutor(llama.Model, llama.Params); + var inferenceParams = new InferenceParams() { Temperature = 1.0f, AntiPrompts = new List { $"{AgentRole.User}:" }, MaxTokens = 64 }; + + var convSetting = _services.GetRequiredService(); + if (convSetting.ShowVerboseLog) + { + _logger.LogInformation(agent.Instruction); + } foreach (var response in executor.Infer(agent.Instruction, inferenceParams)) { diff --git a/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/TextCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/TextCompletionProvider.cs index b0540c28..62009ef7 100644 --- a/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/TextCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/TextCompletionProvider.cs @@ -22,7 +22,7 @@ public class TextCompletionProvider : ITextCompletion var llama = _services.GetRequiredService(); llama.LoadModel(); - var executor = new InstructExecutor(llama.Model); + var executor = new InstructExecutor(llama.Model.CreateContext(llama.Params)); var inferenceParams = new InferenceParams() { Temperature = 0.5f, MaxTokens = 128 }; string totalResponse = ""; diff --git a/src/Plugins/BotSharp.Plugin.MetaAI/BotSharp.Plugin.MetaAI.csproj b/src/Plugins/BotSharp.Plugin.MetaAI/BotSharp.Plugin.MetaAI.csproj index 6b57677e..fd3b01ef 100644 --- a/src/Plugins/BotSharp.Plugin.MetaAI/BotSharp.Plugin.MetaAI.csproj +++ b/src/Plugins/BotSharp.Plugin.MetaAI/BotSharp.Plugin.MetaAI.csproj @@ -3,8 +3,9 @@ netstandard2.1 enable - 10 - 0.10.1 + $(LangVersion) + $(PackageVersion) + $(GeneratePackageOnBuild) diff --git a/src/Plugins/BotSharp.Plugin.MetaMessenger/BotSharp.Plugin.MetaMessenger.csproj b/src/Plugins/BotSharp.Plugin.MetaMessenger/BotSharp.Plugin.MetaMessenger.csproj index 30132b08..289cdcc3 100644 --- a/src/Plugins/BotSharp.Plugin.MetaMessenger/BotSharp.Plugin.MetaMessenger.csproj +++ b/src/Plugins/BotSharp.Plugin.MetaMessenger/BotSharp.Plugin.MetaMessenger.csproj @@ -2,8 +2,9 @@ netstandard2.1 - 10 - 0.10.1 + $(LangVersion) + $(PackageVersion) + $(GeneratePackageOnBuild) diff --git a/src/Plugins/BotSharp.Plugin.PaddleSharp/BotSharp.Plugin.PaddleSharp.csproj b/src/Plugins/BotSharp.Plugin.PaddleSharp/BotSharp.Plugin.PaddleSharp.csproj index 3ad42c16..84b31e0e 100644 --- a/src/Plugins/BotSharp.Plugin.PaddleSharp/BotSharp.Plugin.PaddleSharp.csproj +++ b/src/Plugins/BotSharp.Plugin.PaddleSharp/BotSharp.Plugin.PaddleSharp.csproj @@ -3,8 +3,9 @@ netstandard2.1 enable - 10 - 0.9.0 + $(LangVersion) + $(PackageVersion) + $(GeneratePackageOnBuild) diff --git a/src/Plugins/BotSharp.Plugin.Qdrant/BotSharp.Plugin.Qdrant.csproj b/src/Plugins/BotSharp.Plugin.Qdrant/BotSharp.Plugin.Qdrant.csproj index 9a7edfcc..763b75a2 100644 --- a/src/Plugins/BotSharp.Plugin.Qdrant/BotSharp.Plugin.Qdrant.csproj +++ b/src/Plugins/BotSharp.Plugin.Qdrant/BotSharp.Plugin.Qdrant.csproj @@ -3,8 +3,9 @@ netstandard2.1 enable - 10 - 0.9.0 + $(LangVersion) + $(PackageVersion) + $(GeneratePackageOnBuild) diff --git a/src/Plugins/BotSharp.Plugin.RoutingSpeeder/BotSharp.Plugin.RoutingSpeeder.csproj b/src/Plugins/BotSharp.Plugin.RoutingSpeeder/BotSharp.Plugin.RoutingSpeeder.csproj index d463bacb..bd74b74d 100644 --- a/src/Plugins/BotSharp.Plugin.RoutingSpeeder/BotSharp.Plugin.RoutingSpeeder.csproj +++ b/src/Plugins/BotSharp.Plugin.RoutingSpeeder/BotSharp.Plugin.RoutingSpeeder.csproj @@ -3,8 +3,9 @@ netstandard2.1 enable - 10 - 0.10.2 + $(LangVersion) + $(PackageVersion) + $(GeneratePackageOnBuild) diff --git a/src/Plugins/BotSharp.Plugin.WeChat/BotSharp.Plugin.WeChat.csproj b/src/Plugins/BotSharp.Plugin.WeChat/BotSharp.Plugin.WeChat.csproj index 06c78a77..59ae162a 100644 --- a/src/Plugins/BotSharp.Plugin.WeChat/BotSharp.Plugin.WeChat.csproj +++ b/src/Plugins/BotSharp.Plugin.WeChat/BotSharp.Plugin.WeChat.csproj @@ -2,7 +2,9 @@ netstandard2.1 - true + $(LangVersion) + $(PackageVersion) + $(GeneratePackageOnBuild) @@ -12,7 +14,7 @@ https://github.com/Oceania2018/botsharp-channel-weixin Apache 2.0 botsharp, wechat, wexin, chatbot - 0.9.0 + $(PackageVersion) diff --git a/src/WebStarter/WebStarter.csproj b/src/WebStarter/WebStarter.csproj index 0cb02ead..16feae65 100644 --- a/src/WebStarter/WebStarter.csproj +++ b/src/WebStarter/WebStarter.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -27,7 +27,7 @@ - + @@ -40,6 +40,7 @@ + diff --git a/src/WebStarter/appsettings.json b/src/WebStarter/appsettings.json index 72502bf7..96631e57 100644 --- a/src/WebStarter/appsettings.json +++ b/src/WebStarter/appsettings.json @@ -33,9 +33,9 @@ "LlamaSharp": { "Interactive": true, - "ModelPath": "C:/Users/haipi/Downloads/llama-2-7b-chat.ggmlv3.q3_K_S.bin", + "ModelPath": "C:/Users/haipi/Downloads/llama-2-7b-guanaco-qlora.Q4_K_S.gguf", "MaxContextLength": 1024, - "NumberOfGpuLayer": 10 + "NumberOfGpuLayer": 15 }, "AzureOpenAi": { @@ -105,10 +105,12 @@ "BotSharp.Core", "BotSharp.Plugin.AzureOpenAI", "BotSharp.Plugin.MetaAI", + "BotSharp.Plugin.LLamaSharp", + "BotSharp.Plugin.KnowledgeBase", "BotSharp.Plugin.Qdrant", "BotSharp.Plugin.PaddleSharp", "BotSharp.Plugin.WeChat", - "BotSharp.Plugin.RoutingSpeeder", + // "BotSharp.Plugin.RoutingSpeeder", "BotSharp.Plugin.PizzaBot" ] } diff --git a/tests/BotSharp.Plugin.PizzaBot/BotSharp.Plugin.PizzaBot.csproj b/tests/BotSharp.Plugin.PizzaBot/BotSharp.Plugin.PizzaBot.csproj index a451378c..43ec9965 100644 --- a/tests/BotSharp.Plugin.PizzaBot/BotSharp.Plugin.PizzaBot.csproj +++ b/tests/BotSharp.Plugin.PizzaBot/BotSharp.Plugin.PizzaBot.csproj @@ -3,7 +3,8 @@ netstandard2.1 enable - 10 + $(LangVersion) + ..\..\packages diff --git a/tests/BotSharp.Plugin.PizzaBot/Hooks/PizzaBotAgentHook.cs b/tests/BotSharp.Plugin.PizzaBot/Hooks/PizzaBotAgentHook.cs index 8ee49399..040b4cd7 100644 --- a/tests/BotSharp.Plugin.PizzaBot/Hooks/PizzaBotAgentHook.cs +++ b/tests/BotSharp.Plugin.PizzaBot/Hooks/PizzaBotAgentHook.cs @@ -1,17 +1,16 @@ +using BotSharp.Abstraction.Agents; + namespace BotSharp.Plugin.PizzaBot.Hooks; public class PizzaBotAgentHook : AgentHookBase { public PizzaBotAgentHook(IServiceProvider services, AgentSettings settings) - : base(services, settings) + : base(services, settings) { } public override bool OnInstructionLoaded(string template, Dictionary dict) { - dict["current_date"] = $"{DateTime.Now:MMM dd, yyyy}"; - dict["current_time"] = $"{DateTime.Now:hh:mm tt}"; - dict["current_weekday"] = $"{DateTime.Now:dddd}"; - return true; + return base.OnInstructionLoaded(template, dict); } }