From eccef74d47f5ef4933ce05a213f645c12fba0076 Mon Sep 17 00:00:00 2001 From: xbotter Date: Mon, 20 Nov 2023 21:44:49 +0800 Subject: [PATCH] refactor SemanticKernelTextEmbeddingProvider --- .../SemanticKernelPlugin.cs | 1 + .../SemanticKernelTextEmbeddingProvider.cs | 8 ++++++-- .../SemanticKernelPluginTests.cs | 3 +++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelPlugin.cs b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelPlugin.cs index b1b4e21b..45afa52f 100644 --- a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelPlugin.cs @@ -20,6 +20,7 @@ namespace BotSharp.Plugin.SemanticKernel /// public void RegisterDI(IServiceCollection services, IConfiguration config) { + var provider = services.BuildServiceProvider().CreateScope().ServiceProvider; if (provider.GetService() != null) diff --git a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelTextEmbeddingProvider.cs b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelTextEmbeddingProvider.cs index b44c0108..0b2fa22c 100644 --- a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelTextEmbeddingProvider.cs +++ b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelTextEmbeddingProvider.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.MLTasks; +using Microsoft.Extensions.Configuration; using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.AI.Embeddings; using Microsoft.SemanticKernel.Memory; @@ -17,17 +18,20 @@ namespace BotSharp.Plugin.SemanticKernel public class SemanticKernelTextEmbeddingProvider : ITextEmbedding { private readonly ITextEmbeddingGeneration _embedding; + private readonly IConfiguration _configuration; /// /// Constructor of /// - public SemanticKernelTextEmbeddingProvider(ITextEmbeddingGeneration embedding) + public SemanticKernelTextEmbeddingProvider(ITextEmbeddingGeneration embedding, IConfiguration configuration) { this._embedding = embedding; + this._configuration = configuration; + this.Dimension = configuration.GetValue("SemanticKernel:Dimension"); } /// - public int Dimension { get; } + public int Dimension { get; set; } /// public async Task GetVectorAsync(string text) diff --git a/tests/BotSharp.Plugin.SemanticKernel.UnitTests/SemanticKernelPluginTests.cs b/tests/BotSharp.Plugin.SemanticKernel.UnitTests/SemanticKernelPluginTests.cs index 7e567d56..a1ac3963 100644 --- a/tests/BotSharp.Plugin.SemanticKernel.UnitTests/SemanticKernelPluginTests.cs +++ b/tests/BotSharp.Plugin.SemanticKernel.UnitTests/SemanticKernelPluginTests.cs @@ -16,6 +16,9 @@ namespace BotSharp.Plugin.SemanticKernel.Tests { var services = new ServiceCollection(); var config = new ConfigurationBuilder().Build(); + + services.AddSingleton(config); + var plugin = new SemanticKernelPlugin(); services.AddScoped(x => Mock.Of()); services.AddScoped(x => Mock.Of());