refactor SemanticKernelTextEmbeddingProvider

This commit is contained in:
xbotter 2023-11-20 21:44:49 +08:00
parent fe3e324fb8
commit eccef74d47
No known key found for this signature in database
GPG key ID: D299220A7FE5CF1E
3 changed files with 10 additions and 2 deletions

View file

@ -20,6 +20,7 @@ namespace BotSharp.Plugin.SemanticKernel
/// <inheritdoc/>
public void RegisterDI(IServiceCollection services, IConfiguration config)
{
var provider = services.BuildServiceProvider().CreateScope().ServiceProvider;
if (provider.GetService<Microsoft.SemanticKernel.AI.TextCompletion.ITextCompletion>() != null)

View file

@ -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;
/// <summary>
/// Constructor of <see cref="SemanticKernelTextEmbeddingProvider"/>
/// </summary>
public SemanticKernelTextEmbeddingProvider(ITextEmbeddingGeneration embedding)
public SemanticKernelTextEmbeddingProvider(ITextEmbeddingGeneration embedding, IConfiguration configuration)
{
this._embedding = embedding;
this._configuration = configuration;
this.Dimension = configuration.GetValue<int>("SemanticKernel:Dimension");
}
/// <inheritdoc/>
public int Dimension { get; }
public int Dimension { get; set; }
/// <inheritdoc/>
public async Task<float[]> GetVectorAsync(string text)

View file

@ -16,6 +16,9 @@ namespace BotSharp.Plugin.SemanticKernel.Tests
{
var services = new ServiceCollection();
var config = new ConfigurationBuilder().Build();
services.AddSingleton<IConfiguration>(config);
var plugin = new SemanticKernelPlugin();
services.AddScoped(x => Mock.Of<Microsoft.SemanticKernel.AI.TextCompletion.ITextCompletion>());
services.AddScoped(x => Mock.Of<Microsoft.SemanticKernel.AI.ChatCompletion.IChatCompletion>());