Plugin settings with interaction with UI.

This commit is contained in:
Haiping Chen 2024-01-15 13:15:18 -06:00
parent a2751e9baf
commit 6286393242
31 changed files with 441 additions and 155 deletions

View file

@ -1,7 +1,4 @@
using Microsoft.AspNetCore.Builder;
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Abstraction.Plugins
{

View file

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

View file

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

View file

@ -0,0 +1,13 @@
using System.Text.Json;
namespace BotSharp.Abstraction.Settings;
/// <summary>
/// This settings service is used to cache, monitor changes and encrypt settings in the system and user level
/// </summary>
public interface ISettingService
{
T Bind<T>(string path) where T : new();
object GetDetail(string settingName, bool mask = false);
}

View file

@ -0,0 +1,11 @@
namespace BotSharp.Abstraction.Settings;
public class SettingsMeta
{
public string Name { get; set; }
public SettingsMeta(string name)
{
Name = name;
}
}

View file

@ -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<ILlmProviderSettingService, LlmProviderSettingService>();
services.AddScoped<IAgentService, AgentService>();
services.AddScoped(provider =>
{
var settingService = provider.GetRequiredService<ISettingService>();
return settingService.Bind<AgentSettings>("Agent");
});
}
}

View file

@ -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<ISettingService, SettingService>();
services.AddScoped<IUserService, UserService>();
services.AddScoped<ILlmProviderSettingService, LlmProviderSettingService>();
services.AddScoped<IAgentService, AgentService>();
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<IConversationStorage, ConversationStorage>();
services.AddScoped<IConversationService, ConversationService>();
services.AddScoped<IConversationStateService, ConversationStateService>();
services.AddScoped<RoutingContext>();
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<LlmProviderSetting>();
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<ITemplateRender, TemplateRender>();
services.AddScoped<IResponseTemplateService, ResponseTemplateService>();
// Register router
var routingSettings = new RoutingSettings();
config.Bind("Router", routingSettings);
services.AddSingleton((IServiceProvider x) => routingSettings);
services.AddScoped<NaivePlanner>();
services.AddScoped<HFPlanner>();
services.AddScoped<IPlaner>(provider =>
{
if (routingSettings.Planner == nameof(HFPlanner))
return provider.GetRequiredService<HFPlanner>();
else
return provider.GetRequiredService<NaivePlanner>();
});
services.AddScoped<IExecutor, InstructExecutor>();
services.AddScoped<IRoutingService, RoutingService>();
if (myDatabaseSettings.Default == "FileRepository")
{
services.AddScoped<IBotSharpRepository, FileRepository>();
}
services.AddScoped<IInstructService, InstructService>();
services.AddScoped<ITokenStatistics, TokenStatistics>();
services.AddScoped<IAgentHook, RoutingAgentHook>();
// Rich content messaging
services.AddScoped<IRichContentService, RichContentService>();
// Evaluation
var evalSetting = new EvaluatorSetting();
config.Bind("Evaluator", evalSetting);
services.AddSingleton((IServiceProvider x) => evalSetting);
services.AddScoped<IConversationHook, EvaluationConversationHook>();
services.AddScoped<IEvaluatingService, EvaluatingService>();
services.AddScoped<IExecutionLogger, ExecutionLogger>();
services.AddScoped<IConversationAttachmentService, ConversationAttachmentService>();
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<ISettingService>();
return settingService.Bind<PluginSettings>("PluginLoader");
});
var loader = new PluginLoader(services, config, pluginSettings);
loader.Load(assembly =>

View file

@ -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<ISettingService>();
return settingService.Bind<ConversationSetting>("Conversation");
});
services.AddScoped<IConversationStorage, ConversationStorage>();
services.AddScoped<IConversationService, ConversationService>();
services.AddScoped<IConversationStateService, ConversationStateService>();
services.AddScoped<IConversationAttachmentService, ConversationAttachmentService>();
// Rich content messaging
services.AddScoped<IRichContentService, RichContentService>();
// Register template render
services.AddSingleton<ITemplateRender, TemplateRender>();
services.AddScoped<IResponseTemplateService, ResponseTemplateService>();
services.AddScoped<IExecutor, InstructExecutor>();
services.AddScoped<IInstructService, InstructService>();
services.AddScoped<ITokenStatistics, TokenStatistics>();
}
}

View file

@ -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<ISettingService>();
return settingService.Bind<EvaluatorSetting>("Evaluator");
});
services.AddScoped<IConversationHook, EvaluationConversationHook>();
services.AddScoped<IEvaluatingService, EvaluatingService>();
services.AddScoped<IExecutionLogger, ExecutionLogger>();
}
}

View file

@ -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<LlmProviderSetting>();
public void RegisterDI(IServiceCollection services, IConfiguration config)
{
services.AddScoped(provider =>
{
var settingService = provider.GetRequiredService<ISettingService>();
var llmProviders = settingService.Bind<List<LlmProviderSetting>>("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<LlmProviderSetting> 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;
}
}

View file

@ -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<SettingService> logger)
{
_services = services;
_config = config;
}
public T Bind<T>(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<PluginLoader>();
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;
}
}

View file

@ -53,6 +53,7 @@ public class PluginLoader
{
Id = module.Id,
Name = name,
Module = module,
Description = module.Description,
Assembly = assemblyName,
IconUrl = module.IconUrl,

View file

@ -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<ISettingService>();
return settingService.Bind<DatabaseBasicSettings>("Database");
});
services.AddScoped(provider =>
{
var settingService = provider.GetRequiredService<ISettingService>();
return settingService.Bind<BotSharpDatabaseSettings>("Database");
});
var myDatabaseSettings = new BotSharpDatabaseSettings();
config.Bind("Database", myDatabaseSettings);
if (myDatabaseSettings.Default == "FileRepository")
{
services.AddScoped<IBotSharpRepository, FileRepository>();
}
}
}

View file

@ -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<RoutingContext>();
// Register router
services.AddScoped(provider =>
{
var settingService = provider.GetRequiredService<ISettingService>();
return settingService.Bind<RoutingSettings>("Router");
});
services.AddScoped<IRoutingService, RoutingService>();
services.AddScoped<IAgentHook, RoutingAgentHook>();
services.AddScoped<NaivePlanner>();
services.AddScoped<HFPlanner>();
services.AddScoped<IPlaner>(provider =>
{
var settingService = provider.GetRequiredService<ISettingService>();
var routingSettings = settingService.Bind<RoutingSettings>("Router");
if (routingSettings.Planner == nameof(HFPlanner))
return provider.GetRequiredService<HFPlanner>();
else
return provider.GetRequiredService<NaivePlanner>();
});
}
}

View file

@ -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<string> GetSettings()
{
var pluginService = _services.GetRequiredService<PluginLoader>();
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);
}
}

View file

@ -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<ISettingService>();
return settingService.Bind<AzureOpenAiSettings>("AzureOpenAi");
});
services.AddScoped<ITextCompletion, TextCompletionProvider>();

View file

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
@ -9,12 +9,9 @@
<GenerateDocumentationFile>$(GenerateDocumentationFile)</GenerateDocumentationFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
<ProjectReference Include="..\..\Infrastructure\BotSharp.OpenAPI\BotSharp.OpenAPI.csproj" />
</ItemGroup>
</Project>

View file

@ -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<ISettingService>();
return settingService.Bind<GoogleAiSettings>("GoogleAi");
});
services.AddScoped<IChatCompletion, ChatCompletionProvider>();

View file

@ -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<ISettingService>();
return settingService.Bind<HuggingFaceSettings>("HuggingFace");
});
services

View file

@ -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<ISettingService>();
return settingService.Bind<KnowledgeBaseSettings>("KnowledgeBase");
});
var a = config["KnowledgeBase"];

View file

@ -6,7 +6,7 @@
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<GenerateDocumentationFile>$(GenerateDocumentationFile)</GenerateDocumentationFile>
</PropertyGroup>
<ItemGroup>

View file

@ -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<ISettingService>();
return settingService.Bind<LlamaSharpSettings>("LlamaSharp");
});
services.AddSingleton<LlamaAiModel>();
services.AddSingleton<ITextEmbedding, TextEmbeddingProvider>();

View file

@ -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<ISettingService>();
return settingService.Bind<MetaAiSettings>("MetaAi");
});
services.AddScoped(provider =>
{
var settingService = provider.GetRequiredService<ISettingService>();
return settingService.Bind<MetaAiSettings>("MetaAi").fastText;
});
services.AddSingleton<ITextEmbedding, fastTextEmbeddingProvider>();
services.AddSingleton<IVectorDb, FaissDb>();

View file

@ -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<float[]> 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<fastTextSetting>();
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);
}
}
}

View file

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

View file

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
@ -6,7 +6,7 @@
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<GenerateDocumentationFile>$(GenerateDocumentationFile)</GenerateDocumentationFile>
</PropertyGroup>
<ItemGroup>

View file

@ -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<ISettingService>();
return settingService.Bind<QdrantSetting>("Qdrant");
});
services.AddScoped<IVectorDb, QdrantDb>();
}

View file

@ -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<ISettingService>();
return settingService.Bind<RouterSpeederSettings>("RouterSpeeder");
});
services.AddSingleton<ClassifierSetting>();

View file

@ -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<ISettingService>();
return settingService.Bind<TwilioSetting>("Twilio");
});
services.AddScoped<TwilioService>();
}

View file

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

View file

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
@ -37,6 +37,7 @@
<ItemGroup>
<ProjectReference Include="..\..\tests\BotSharp.Plugin.PizzaBot\BotSharp.Plugin.PizzaBot.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.LLamaSharp\BotSharp.Plugin.LLamaSharp.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.WebDriver\BotSharp.Plugin.WebDriver.csproj" />
</ItemGroup>