diff --git a/src/Infrastructure/BotSharp.Abstraction/Plugins/IBotSharpAppPlugin.cs b/src/Infrastructure/BotSharp.Abstraction/Plugins/IBotSharpAppPlugin.cs index d4106b7a..53190263 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Plugins/IBotSharpAppPlugin.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Plugins/IBotSharpAppPlugin.cs @@ -1,7 +1,4 @@ using Microsoft.AspNetCore.Builder; -using System; -using System.Collections.Generic; -using System.Text; namespace BotSharp.Abstraction.Plugins { diff --git a/src/Infrastructure/BotSharp.Abstraction/Plugins/IBotSharpPlugin.cs b/src/Infrastructure/BotSharp.Abstraction/Plugins/IBotSharpPlugin.cs index dd5deb12..d3e0a8f3 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Plugins/IBotSharpPlugin.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Plugins/IBotSharpPlugin.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Plugins.Models; +using BotSharp.Abstraction.Settings; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -12,6 +13,9 @@ public interface IBotSharpPlugin string Id { get; } string Name => ""; string Description => ""; + SettingsMeta Settings => new SettingsMeta(""); + object GetNewSettingsInstance() => new object(); + bool MaskSettings(object settings) => true; string? IconUrl => null; /// diff --git a/src/Infrastructure/BotSharp.Abstraction/Plugins/Models/PluginDef.cs b/src/Infrastructure/BotSharp.Abstraction/Plugins/Models/PluginDef.cs index 5274c556..7b3eface 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Plugins/Models/PluginDef.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Plugins/Models/PluginDef.cs @@ -10,7 +10,11 @@ public class PluginDef public string? IconUrl { get; set; } [JsonPropertyName("agent_ids")] - public string[] AgentIds { get; set; } + public string[] AgentIds { get; set; } = new string[0]; + + [JsonPropertyName("settings_name")] + public string SettingsName => Module?.Settings?.Name; + public IBotSharpPlugin Module { get; set; } public bool Enabled { get; set; } diff --git a/src/Infrastructure/BotSharp.Abstraction/Settings/ISettingService.cs b/src/Infrastructure/BotSharp.Abstraction/Settings/ISettingService.cs new file mode 100644 index 00000000..9ac3646e --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Settings/ISettingService.cs @@ -0,0 +1,13 @@ +using System.Text.Json; + +namespace BotSharp.Abstraction.Settings; + +/// +/// This settings service is used to cache, monitor changes and encrypt settings in the system and user level +/// +public interface ISettingService +{ + T Bind(string path) where T : new(); + + object GetDetail(string settingName, bool mask = false); +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Settings/SettingsMeta.cs b/src/Infrastructure/BotSharp.Abstraction/Settings/SettingsMeta.cs new file mode 100644 index 00000000..b330a997 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Settings/SettingsMeta.cs @@ -0,0 +1,11 @@ +namespace BotSharp.Abstraction.Settings; + +public class SettingsMeta +{ + public string Name { get; set; } + + public SettingsMeta(string name) + { + Name = name; + } +} diff --git a/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs b/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs new file mode 100644 index 00000000..d9a0405e --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs @@ -0,0 +1,29 @@ +using BotSharp.Abstraction.MLTasks; +using BotSharp.Abstraction.Settings; +using Microsoft.Extensions.Configuration; + +namespace BotSharp.Core.Agents; + +public class AgentPlugin : IBotSharpPlugin +{ + public string Id => "f4b367f8-4945-476a-90a7-c3bb8e6d6e49"; + public string Name => "Agent"; + + public SettingsMeta Settings => + new SettingsMeta("Agent"); + + public object GetNewSettingsInstance() => + new AgentSettings(); + + public void RegisterDI(IServiceCollection services, IConfiguration config) + { + services.AddScoped(); + services.AddScoped(); + + services.AddScoped(provider => + { + var settingService = provider.GetRequiredService(); + return settingService.Bind("Agent"); + }); + } +} diff --git a/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs b/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs index 8437fbb5..b3b2a629 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs +++ b/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs @@ -1,27 +1,10 @@ using BotSharp.Abstraction.Functions; using BotSharp.Abstraction.Repositories; -using BotSharp.Core.Routing; -using BotSharp.Core.Templating; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; -using BotSharp.Abstraction.Routing.Settings; -using BotSharp.Abstraction.Templating; -using BotSharp.Core.Instructs; -using BotSharp.Abstraction.Instructs; using BotSharp.Abstraction.Routing; -using BotSharp.Core.Routing.Hooks; -using BotSharp.Abstraction.Routing.Models; using BotSharp.Core.Plugins; -using BotSharp.Abstraction.Evaluations.Settings; -using BotSharp.Abstraction.Evaluations; -using BotSharp.Core.Evaluatings; -using BotSharp.Core.Evaluations; -using BotSharp.Abstraction.MLTasks.Settings; -using BotSharp.Abstraction.Planning; -using BotSharp.Core.Planning; -using BotSharp.Abstraction.MLTasks; -using BotSharp.Abstraction.Messaging; -using BotSharp.Core.Messaging; +using BotSharp.Abstraction.Settings; namespace BotSharp.Core; @@ -29,90 +12,10 @@ public static class BotSharpCoreExtensions { public static IServiceCollection AddBotSharpCore(this IServiceCollection services, IConfiguration config) { + services.AddScoped(); services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - - var agentSettings = new AgentSettings(); - config.Bind("Agent", agentSettings); - services.AddSingleton((IServiceProvider x) => agentSettings); - - var convsationSettings = new ConversationSetting(); - config.Bind("Conversation", convsationSettings); - services.AddSingleton((IServiceProvider x) => convsationSettings); - - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - - var databaseSettings = new DatabaseBasicSettings(); - config.Bind("Database", databaseSettings); - services.AddSingleton((IServiceProvider x) => databaseSettings); - - var myDatabaseSettings = new BotSharpDatabaseSettings(); - config.Bind("Database", myDatabaseSettings); - services.AddSingleton((IServiceProvider x) => myDatabaseSettings); - - var llmProviders = new List(); - config.Bind("LlmProviders", llmProviders); - services.AddSingleton((IServiceProvider x) => - { - foreach (var llmProvider in llmProviders) - { - Console.WriteLine($"Loaded LlmProvider {llmProvider.Provider} settings with {llmProvider.Models.Count} models."); - } - return llmProviders; - }); RegisterPlugins(services, config); - - // Register template render - services.AddSingleton(); - services.AddScoped(); - - // Register router - var routingSettings = new RoutingSettings(); - config.Bind("Router", routingSettings); - services.AddSingleton((IServiceProvider x) => routingSettings); - - services.AddScoped(); - services.AddScoped(); - services.AddScoped(provider => - { - if (routingSettings.Planner == nameof(HFPlanner)) - return provider.GetRequiredService(); - else - return provider.GetRequiredService(); - }); - - services.AddScoped(); - services.AddScoped(); - - if (myDatabaseSettings.Default == "FileRepository") - { - services.AddScoped(); - } - - services.AddScoped(); - services.AddScoped(); - - services.AddScoped(); - - // Rich content messaging - services.AddScoped(); - - // Evaluation - var evalSetting = new EvaluatorSetting(); - config.Bind("Evaluator", evalSetting); - services.AddSingleton((IServiceProvider x) => evalSetting); - - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - - services.AddScoped(); - return services; } @@ -154,7 +57,11 @@ public static class BotSharpCoreExtensions { var pluginSettings = new PluginSettings(); config.Bind("PluginLoader", pluginSettings); - services.AddSingleton(pluginSettings); + services.AddScoped(provider => + { + var settingService = provider.GetRequiredService(); + return settingService.Bind("PluginLoader"); + }); var loader = new PluginLoader(services, config, pluginSettings); loader.Load(assembly => diff --git a/src/Infrastructure/BotSharp.Core/Conversations/ConversationPlugin.cs b/src/Infrastructure/BotSharp.Core/Conversations/ConversationPlugin.cs new file mode 100644 index 00000000..a846aa7d --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Conversations/ConversationPlugin.cs @@ -0,0 +1,48 @@ +using BotSharp.Abstraction.Instructs; +using BotSharp.Abstraction.Messaging; +using BotSharp.Abstraction.Planning; +using BotSharp.Abstraction.Settings; +using BotSharp.Abstraction.Templating; +using BotSharp.Core.Instructs; +using BotSharp.Core.Messaging; +using BotSharp.Core.Planning; +using BotSharp.Core.Templating; +using Microsoft.Extensions.Configuration; + +namespace BotSharp.Core.Conversations; + +public class ConversationPlugin : IBotSharpPlugin +{ + public string Id => "99e9b971-a9f1-4273-84da-876d2873d192"; + public string Name => "Conversation"; + + public SettingsMeta Settings => + new SettingsMeta("Conversation"); + public object GetNewSettingsInstance() => + new ConversationSetting(); + + public void RegisterDI(IServiceCollection services, IConfiguration config) + { + services.AddScoped(provider => + { + var settingService = provider.GetRequiredService(); + return settingService.Bind("Conversation"); + }); + + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + + // Rich content messaging + services.AddScoped(); + + // Register template render + services.AddSingleton(); + services.AddScoped(); + + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + } +} diff --git a/src/Infrastructure/BotSharp.Core/Evaluations/EvaluationPlugin.cs b/src/Infrastructure/BotSharp.Core/Evaluations/EvaluationPlugin.cs new file mode 100644 index 00000000..ea5c27e5 --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Evaluations/EvaluationPlugin.cs @@ -0,0 +1,26 @@ +using BotSharp.Abstraction.Evaluations.Settings; +using BotSharp.Abstraction.Evaluations; +using BotSharp.Abstraction.Settings; +using BotSharp.Core.Evaluatings; +using Microsoft.Extensions.Configuration; + +namespace BotSharp.Core.Evaluations; + +public class EvaluationPlugin : IBotSharpPlugin +{ + public string Id => "0ae251a6-7e34-403e-a333-b365d8986068"; + + public void RegisterDI(IServiceCollection services, IConfiguration config) + { + // Evaluation + services.AddScoped(provider => + { + var settingService = provider.GetRequiredService(); + return settingService.Bind("Evaluator"); + }); + + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + } +} diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/LlmProviderPlugin.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/LlmProviderPlugin.cs new file mode 100644 index 00000000..3e55907c --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/LlmProviderPlugin.cs @@ -0,0 +1,47 @@ +using BotSharp.Abstraction.MLTasks.Settings; +using BotSharp.Abstraction.Settings; +using Microsoft.Extensions.Configuration; + +namespace BotSharp.Core.Infrastructures; + +public class LlmProviderPlugin : IBotSharpPlugin +{ + public string Id => "0c52c0e3-cbb9-48ab-9381-260b80f018b8"; + public string Name => "LLM Provider"; + + public SettingsMeta Settings => + new SettingsMeta("LlmProviders"); + + public object GetNewSettingsInstance() => + new List(); + + public void RegisterDI(IServiceCollection services, IConfiguration config) + { + services.AddScoped(provider => + { + var settingService = provider.GetRequiredService(); + var llmProviders = settingService.Bind>("LlmProviders"); + foreach (var llmProvider in llmProviders) + { + Console.WriteLine($"Loaded LlmProvider {llmProvider.Provider} settings with {llmProvider.Models.Count} models."); + } + return llmProviders; + }); + } + + public bool MaskSettings(object settings) + { + if (settings is List instance) + { + foreach (var item in instance) + { + foreach (var model in item.Models) + { + model.Endpoint = SettingService.Mask(model.Endpoint); + model.ApiKey = SettingService.Mask(model.ApiKey); + } + } + } + return true; + } +} diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/SettingService.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/SettingService.cs new file mode 100644 index 00000000..4f4c13e6 --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/SettingService.cs @@ -0,0 +1,52 @@ +using BotSharp.Abstraction.Settings; +using BotSharp.Core.Plugins; +using Microsoft.Extensions.Configuration; + +namespace BotSharp.Core.Infrastructures; + +public class SettingService : ISettingService +{ + private readonly IServiceProvider _services; + private readonly IConfiguration _config; + private readonly ILogger _logger; + + public SettingService(IServiceProvider services, + IConfiguration config, + ILogger logger) + { + _services = services; + _config = config; + } + + public T Bind(string path) where T : new() + { + var settings = new T(); + _config.Bind(path, settings); + return settings; + } + + public object GetDetail(string settingName, bool mask = false) + { + var pluginService = _services.GetRequiredService(); + var plugins = pluginService.GetPlugins(_services); + var plugin = plugins.First(x => x.Module.Settings.Name == settingName); + var instance = plugin.Module.GetNewSettingsInstance(); + _config.Bind(settingName, instance); + if (mask) + { + plugin.Module.MaskSettings(instance); + } + return instance; + } + + public static string Mask(string value) + { + if (value == null) + { + return null; + } + value = value.Substring(0, value.Length / 2 - 1) + + string.Join("", Enumerable.Repeat("*", value.Length / 2)); + return value; + } +} diff --git a/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs b/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs index 96a97c3f..17e740d2 100644 --- a/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs +++ b/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs @@ -53,6 +53,7 @@ public class PluginLoader { Id = module.Id, Name = name, + Module = module, Description = module.Description, Assembly = assemblyName, IconUrl = module.IconUrl, diff --git a/src/Infrastructure/BotSharp.Core/Repository/RepositoryPlugin.cs b/src/Infrastructure/BotSharp.Core/Repository/RepositoryPlugin.cs new file mode 100644 index 00000000..297136b5 --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Repository/RepositoryPlugin.cs @@ -0,0 +1,32 @@ +using BotSharp.Abstraction.Repositories; +using BotSharp.Abstraction.Settings; +using Microsoft.Extensions.Configuration; + +namespace BotSharp.Core.Repository; + +public class RepositoryPlugin : IBotSharpPlugin +{ + public string Id => "866b4b19-b4d3-479d-8e0a-98816643b8db"; + + public void RegisterDI(IServiceCollection services, IConfiguration config) + { + services.AddScoped(provider => + { + var settingService = provider.GetRequiredService(); + return settingService.Bind("Database"); + }); + + services.AddScoped(provider => + { + var settingService = provider.GetRequiredService(); + return settingService.Bind("Database"); + }); + + var myDatabaseSettings = new BotSharpDatabaseSettings(); + config.Bind("Database", myDatabaseSettings); + if (myDatabaseSettings.Default == "FileRepository") + { + services.AddScoped(); + } + } +} diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingPlugin.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingPlugin.cs new file mode 100644 index 00000000..49bd5295 --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingPlugin.cs @@ -0,0 +1,48 @@ +using BotSharp.Abstraction.Planning; +using BotSharp.Abstraction.Routing; +using BotSharp.Abstraction.Routing.Models; +using BotSharp.Abstraction.Routing.Settings; +using BotSharp.Abstraction.Settings; +using BotSharp.Core.Planning; +using BotSharp.Core.Routing.Hooks; +using Microsoft.Extensions.Configuration; + +namespace BotSharp.Core.Routing; + +public class RoutingPlugin : IBotSharpPlugin +{ + public string Id => "87352ece-2c1c-477c-8236-2e047e11dcab"; + + public SettingsMeta Settings => + new SettingsMeta("Router"); + + public object GetNewSettingsInstance() => + new RoutingSettings(); + + public void RegisterDI(IServiceCollection services, IConfiguration config) + { + services.AddScoped(); + + // Register router + services.AddScoped(provider => + { + var settingService = provider.GetRequiredService(); + return settingService.Bind("Router"); + }); + + services.AddScoped(); + services.AddScoped(); + + services.AddScoped(); + services.AddScoped(); + services.AddScoped(provider => + { + var settingService = provider.GetRequiredService(); + var routingSettings = settingService.Bind("Router"); + if (routingSettings.Planner == nameof(HFPlanner)) + return provider.GetRequiredService(); + else + return provider.GetRequiredService(); + }); + } +} diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/SettingController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/SettingController.cs new file mode 100644 index 00000000..cc15e557 --- /dev/null +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/SettingController.cs @@ -0,0 +1,37 @@ +using BotSharp.Abstraction.Settings; +using BotSharp.Core.Plugins; + +namespace BotSharp.OpenAPI.Controllers; + +[Authorize] +[ApiController] +public class SettingController : ControllerBase +{ + private readonly IServiceProvider _services; + private readonly ISettingService _settingService; + + public SettingController(IServiceProvider services, + ISettingService settingService) + { + _services = services; + _settingService = settingService; + } + + [HttpGet("/settings")] + public List GetSettings() + { + var pluginService = _services.GetRequiredService(); + var plugins = pluginService.GetPlugins(_services); + + return plugins.Where(x => x.Module.Settings != null && !string.IsNullOrEmpty(x.Module.Settings.Name)) + .Select(x => x.Module.Settings.Name) + .OrderBy(x => x) + .ToList(); + } + + [HttpGet("/setting/{id}")] + public object GetSettingDetail([FromRoute] string id) + { + return _settingService.GetDetail(id, mask: true); + } +} diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs index ee16b6e4..a067eb09 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs @@ -1,5 +1,7 @@ +using BotSharp.Abstraction.Evaluations.Settings; using BotSharp.Abstraction.MLTasks; using BotSharp.Abstraction.Plugins; +using BotSharp.Abstraction.Settings; using BotSharp.Plugin.AzureOpenAI.Providers; using BotSharp.Plugin.AzureOpenAI.Settings; using Microsoft.Extensions.Configuration; @@ -20,12 +22,10 @@ public class AzureOpenAiPlugin : IBotSharpPlugin public void RegisterDI(IServiceCollection services, IConfiguration config) { - var settings = new AzureOpenAiSettings(); - config.Bind("AzureOpenAi", settings); - services.AddSingleton(x => + services.AddScoped(provider => { - Console.WriteLine($"Loaded AzureOpenAi settings"); - return settings; + var settingService = provider.GetRequiredService(); + return settingService.Bind("AzureOpenAi"); }); services.AddScoped(); diff --git a/src/Plugins/BotSharp.Plugin.ChatbotUI/BotSharp.Plugin.ChatbotUI.csproj b/src/Plugins/BotSharp.Plugin.ChatbotUI/BotSharp.Plugin.ChatbotUI.csproj index 39ea06b2..43aa52fa 100644 --- a/src/Plugins/BotSharp.Plugin.ChatbotUI/BotSharp.Plugin.ChatbotUI.csproj +++ b/src/Plugins/BotSharp.Plugin.ChatbotUI/BotSharp.Plugin.ChatbotUI.csproj @@ -1,7 +1,7 @@ - netstandard2.1 + net6.0 enable $(LangVersion) $(BotSharpVersion) @@ -9,12 +9,9 @@ $(GenerateDocumentationFile) - - - - + diff --git a/src/Plugins/BotSharp.Plugin.GoogleAI/GoogleAiPlugin.cs b/src/Plugins/BotSharp.Plugin.GoogleAI/GoogleAiPlugin.cs index b0c827d7..aac88308 100644 --- a/src/Plugins/BotSharp.Plugin.GoogleAI/GoogleAiPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.GoogleAI/GoogleAiPlugin.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Plugins; +using BotSharp.Abstraction.Settings; using BotSharp.Plugin.GoogleAI.Providers; using BotSharp.Plugin.GoogleAI.Settings; @@ -12,12 +13,10 @@ public class GoogleAiPlugin : IBotSharpPlugin public string IconUrl => "https://vectorseek.com/wp-content/uploads/2021/12/Google-AI-Logo-Vector.png"; public void RegisterDI(IServiceCollection services, IConfiguration config) { - var settings = new GoogleAiSettings(); - config.Bind("GoogleAi", settings); - services.AddSingleton(x => + services.AddScoped(provider => { - Console.WriteLine($"Loaded Google AI settings: {settings.PaLM.Endpoint} {settings.PaLM.ApiKey.SubstringMax(4)}"); - return settings; + var settingService = provider.GetRequiredService(); + return settingService.Bind("GoogleAi"); }); services.AddScoped(); diff --git a/src/Plugins/BotSharp.Plugin.HuggingFace/HuggingFacePlugin.cs b/src/Plugins/BotSharp.Plugin.HuggingFace/HuggingFacePlugin.cs index d82ac83f..e46d19cf 100644 --- a/src/Plugins/BotSharp.Plugin.HuggingFace/HuggingFacePlugin.cs +++ b/src/Plugins/BotSharp.Plugin.HuggingFace/HuggingFacePlugin.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Plugins; +using BotSharp.Abstraction.Settings; using BotSharp.Plugin.HuggingFace.Providers; using BotSharp.Plugin.HuggingFace.Services; using BotSharp.Plugin.HuggingFace.Settings; @@ -16,10 +17,10 @@ public class HuggingFacePlugin : IBotSharpPlugin { var settings = new HuggingFaceSettings(); config.Bind("HuggingFace", settings); - services.AddSingleton(x => + services.AddScoped(provider => { - Console.WriteLine($"Loaded HuggingFace settings: {settings.EndPoint} ({settings.Model}) {settings.Token.SubstringMax(4)}"); - return settings; + var settingService = provider.GetRequiredService(); + return settingService.Bind("HuggingFace"); }); services diff --git a/src/Plugins/BotSharp.Plugin.KnowledgeBase/KnowledgeBasePlugin.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/KnowledgeBasePlugin.cs index 91ce959a..c1e6a080 100644 --- a/src/Plugins/BotSharp.Plugin.KnowledgeBase/KnowledgeBasePlugin.cs +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/KnowledgeBasePlugin.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Plugins.Models; +using BotSharp.Abstraction.Settings; using Microsoft.Extensions.Configuration; namespace BotSharp.Plugin.KnowledgeBase; @@ -13,9 +14,11 @@ public class KnowledgeBasePlugin : IBotSharpPlugin public void RegisterDI(IServiceCollection services, IConfiguration config) { - var settings = new KnowledgeBaseSettings(); - config.Bind("KnowledgeBase", settings); - services.AddSingleton(x => settings); + services.AddScoped(provider => + { + var settingService = provider.GetRequiredService(); + return settingService.Bind("KnowledgeBase"); + }); var a = config["KnowledgeBase"]; diff --git a/src/Plugins/BotSharp.Plugin.LLamaSharp/BotSharp.Plugin.LLamaSharp.csproj b/src/Plugins/BotSharp.Plugin.LLamaSharp/BotSharp.Plugin.LLamaSharp.csproj index 76010525..2ae6f4c0 100644 --- a/src/Plugins/BotSharp.Plugin.LLamaSharp/BotSharp.Plugin.LLamaSharp.csproj +++ b/src/Plugins/BotSharp.Plugin.LLamaSharp/BotSharp.Plugin.LLamaSharp.csproj @@ -6,7 +6,7 @@ $(LangVersion) $(BotSharpVersion) $(GeneratePackageOnBuild) - True + $(GenerateDocumentationFile) diff --git a/src/Plugins/BotSharp.Plugin.LLamaSharp/LLamaSharpPlugin.cs b/src/Plugins/BotSharp.Plugin.LLamaSharp/LLamaSharpPlugin.cs index 84ffffb2..6114e439 100644 --- a/src/Plugins/BotSharp.Plugin.LLamaSharp/LLamaSharpPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.LLamaSharp/LLamaSharpPlugin.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Plugins; +using BotSharp.Abstraction.Settings; using BotSharp.Plugin.LLamaSharp.Providers; namespace BotSharp.Plugins.LLamaSharp; @@ -7,14 +8,23 @@ public class LLamaSharpPlugin : IBotSharpPlugin { public string Id => "3999f668-9fbf-4a91-bd4d-df5b7dfcd90e"; public string Name => "LLamaSharp"; + public SettingsMeta Settings => + new SettingsMeta("LLamaSharp"); + public object GetNewSettingsInstance() + { + return new LlamaSharpSettings(); + } + public string Description => "The C#/.NET binding of llama.cpp. Run local LLaMA/GPT model easily and fast in C#!"; public string IconUrl => "https://raw.githubusercontent.com/SciSharp/LLamaSharp/master/Assets/LLamaSharpLogo.png"; public void RegisterDI(IServiceCollection services, IConfiguration config) { - var llamaSharpSettings = new LlamaSharpSettings(); - config.Bind("LlamaSharp", llamaSharpSettings); - services.AddSingleton(x => llamaSharpSettings); + services.AddSingleton(provider => + { + var settingService = provider.CreateScope().ServiceProvider.GetRequiredService(); + return settingService.Bind("LlamaSharp"); + }); services.AddSingleton(); services.AddSingleton(); diff --git a/src/Plugins/BotSharp.Plugin.MetaAI/MetaAiPlugin.cs b/src/Plugins/BotSharp.Plugin.MetaAI/MetaAiPlugin.cs index f9e6b4b4..bf4081d5 100644 --- a/src/Plugins/BotSharp.Plugin.MetaAI/MetaAiPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.MetaAI/MetaAiPlugin.cs @@ -1,5 +1,7 @@ +using BotSharp.Abstraction.Knowledges.Settings; using BotSharp.Abstraction.MLTasks; using BotSharp.Abstraction.Plugins; +using BotSharp.Abstraction.Settings; using BotSharp.Abstraction.VectorStorage; using BotSharp.Plugin.MetaAI.Providers; using BotSharp.Plugin.MetaAI.Settings; @@ -17,10 +19,17 @@ public class MetaAiPlugin : IBotSharpPlugin public string IconUrl => "https://static.xx.fbcdn.net/rsrc.php/yJ/r/C1E_YZIckM5.svg"; public void RegisterDI(IServiceCollection services, IConfiguration config) { - var settings = new MetaAiSettings(); - config.Bind("MetaAi", settings); - services.AddSingleton(x => settings); - services.AddSingleton(x => settings.fastText); + services.AddScoped(provider => + { + var settingService = provider.GetRequiredService(); + return settingService.Bind("MetaAi"); + }); + + services.AddScoped(provider => + { + var settingService = provider.GetRequiredService(); + return settingService.Bind("MetaAi").fastText; + }); services.AddSingleton(); services.AddSingleton(); diff --git a/src/Plugins/BotSharp.Plugin.MetaAI/Providers/fastTextEmbeddingProvider.cs b/src/Plugins/BotSharp.Plugin.MetaAI/Providers/fastTextEmbeddingProvider.cs index 9bfecfe4..64aff279 100644 --- a/src/Plugins/BotSharp.Plugin.MetaAI/Providers/fastTextEmbeddingProvider.cs +++ b/src/Plugins/BotSharp.Plugin.MetaAI/Providers/fastTextEmbeddingProvider.cs @@ -1,6 +1,8 @@ using BotSharp.Abstraction.MLTasks; using BotSharp.Plugin.MetaAI.Settings; using FastText.NetWrapper; +using Microsoft.Extensions.DependencyInjection; +using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; @@ -10,7 +12,7 @@ namespace BotSharp.Plugin.MetaAI.Providers; public class fastTextEmbeddingProvider : ITextEmbedding { private FastTextWrapper _fastText; - private readonly fastTextSetting _settings; + private readonly IServiceProvider _services; public int Dimension { @@ -21,10 +23,9 @@ public class fastTextEmbeddingProvider : ITextEmbedding } } - public fastTextEmbeddingProvider(fastTextSetting settings) + public fastTextEmbeddingProvider(IServiceProvider services) { - _settings = settings; - + _services = services; } public Task GetVectorAsync(string text) @@ -48,16 +49,18 @@ public class fastTextEmbeddingProvider : ITextEmbedding { if (_fastText == null) { - if (!File.Exists(_settings.ModelPath)) + var settings = _services.CreateScope().ServiceProvider + .GetRequiredService(); + if (!File.Exists(settings.ModelPath)) { - throw new FileNotFoundException($"Can't load pre-trained word vectors from {_settings.ModelPath}.\n Try to download from https://fasttext.cc/docs/en/english-vectors.html."); + throw new FileNotFoundException($"Can't load pre-trained word vectors from {settings.ModelPath}.\n Try to download from https://fasttext.cc/docs/en/english-vectors.html."); } _fastText = new FastTextWrapper(); if (!_fastText.IsModelReady()) { - _fastText.LoadModel(_settings.ModelPath); + _fastText.LoadModel(settings.ModelPath); } } } diff --git a/src/Plugins/BotSharp.Plugin.PaddleSharp/PaddleSharpPlugin.cs b/src/Plugins/BotSharp.Plugin.PaddleSharp/PaddleSharpPlugin.cs index 55b337d4..91f55cf9 100644 --- a/src/Plugins/BotSharp.Plugin.PaddleSharp/PaddleSharpPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.PaddleSharp/PaddleSharpPlugin.cs @@ -4,7 +4,6 @@ using BotSharp.Plugin.PaddleSharp.Providers; using BotSharp.Plugin.PaddleSharp.Settings; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using System; namespace BotSharp.Plugin.PaddleOCR; diff --git a/src/Plugins/BotSharp.Plugin.Qdrant/BotSharp.Plugin.Qdrant.csproj b/src/Plugins/BotSharp.Plugin.Qdrant/BotSharp.Plugin.Qdrant.csproj index e24b8da2..8b7c4143 100644 --- a/src/Plugins/BotSharp.Plugin.Qdrant/BotSharp.Plugin.Qdrant.csproj +++ b/src/Plugins/BotSharp.Plugin.Qdrant/BotSharp.Plugin.Qdrant.csproj @@ -1,4 +1,4 @@ - + netstandard2.1 @@ -6,7 +6,7 @@ $(LangVersion) $(BotSharpVersion) $(GeneratePackageOnBuild) - True + $(GenerateDocumentationFile) diff --git a/src/Plugins/BotSharp.Plugin.Qdrant/QdrantPlugin.cs b/src/Plugins/BotSharp.Plugin.Qdrant/QdrantPlugin.cs index ad151aed..8cfd137b 100644 --- a/src/Plugins/BotSharp.Plugin.Qdrant/QdrantPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.Qdrant/QdrantPlugin.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Plugins; +using BotSharp.Abstraction.Settings; using BotSharp.Abstraction.VectorStorage; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -13,9 +14,11 @@ public class QdrantPlugin : IBotSharpPlugin public string IconUrl => "https://qdrant.tech/images/logo_with_text.png"; public void RegisterDI(IServiceCollection services, IConfiguration config) { - var settings = new QdrantSetting(); - config.Bind("Qdrant", settings); - services.AddSingleton(x => settings); + services.AddScoped(provider => + { + var settingService = provider.GetRequiredService(); + return settingService.Bind("Qdrant"); + }); services.AddScoped(); } diff --git a/src/Plugins/BotSharp.Plugin.RoutingSpeeder/RoutingSpeederPlugin.cs b/src/Plugins/BotSharp.Plugin.RoutingSpeeder/RoutingSpeederPlugin.cs index e72bdacf..5bfe2d12 100644 --- a/src/Plugins/BotSharp.Plugin.RoutingSpeeder/RoutingSpeederPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.RoutingSpeeder/RoutingSpeederPlugin.cs @@ -1,10 +1,10 @@ using BotSharp.Abstraction.Conversations; -using BotSharp.Abstraction.MLTasks; using BotSharp.Abstraction.Plugins; using BotSharp.Plugin.RoutingSpeeder.Settings; using BotSharp.Plugin.RoutingSpeeder.Providers; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using BotSharp.Abstraction.Settings; namespace BotSharp.Plugin.RoutingSpeeder; @@ -13,9 +13,11 @@ public class RoutingSpeederPlugin : IBotSharpPlugin public string Id => "e7dff028-462d-47d2-85aa-dc56a6d362ee"; public void RegisterDI(IServiceCollection services, IConfiguration config) { - var settings = new RouterSpeederSettings(); - config.Bind("RouterSpeeder", settings); - services.AddSingleton(x => settings); + services.AddScoped(provider => + { + var settingService = provider.GetRequiredService(); + return settingService.Bind("RouterSpeeder"); + }); services.AddSingleton(); diff --git a/src/Plugins/BotSharp.Plugin.Twilio/TwilioPlugin.cs b/src/Plugins/BotSharp.Plugin.Twilio/TwilioPlugin.cs index 844f74ab..1a2065ca 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/TwilioPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/TwilioPlugin.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Settings; using BotSharp.Plugin.Twilio.Services; namespace BotSharp.Plugin.Twilio; @@ -10,9 +11,11 @@ public class TwilioPlugin : IBotSharpPlugin public void RegisterDI(IServiceCollection services, IConfiguration config) { - var setting = new TwilioSetting(); - config.Bind("Twilio", setting); - services.AddSingleton(setting); + services.AddSingleton(provider => + { + var settingService = provider.GetRequiredService(); + return settingService.Bind("Twilio"); + }); services.AddScoped(); } diff --git a/src/WebStarter/Program.cs b/src/WebStarter/Program.cs index 9f067666..fbe924a5 100644 --- a/src/WebStarter/Program.cs +++ b/src/WebStarter/Program.cs @@ -9,7 +9,7 @@ var builder = WebApplication.CreateBuilder(args); // Use Serilog Log.Logger = new LoggerConfiguration() #if DEBUG - .MinimumLevel.Debug() + .MinimumLevel.Information() #else .MinimumLevel.Warning() #endif diff --git a/src/WebStarter/WebStarter.csproj b/src/WebStarter/WebStarter.csproj index b430eb3f..6759cba7 100644 --- a/src/WebStarter/WebStarter.csproj +++ b/src/WebStarter/WebStarter.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -37,6 +37,7 @@ +