using BotSharp.Abstraction.MLTasks;
using Microsoft.Extensions.Configuration;
using Microsoft.SemanticKernel.Embeddings;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BotSharp.Plugin.SemanticKernel
{
///
/// Use Semantic Kernel Memory as text embedding provider
///
public class SemanticKernelTextEmbeddingProvider : ITextEmbedding
{
#pragma warning disable SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
private readonly ITextEmbeddingGenerationService _embedding;
#pragma warning restore SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
private readonly IConfiguration _configuration;
///
/// Constructor of
///
#pragma warning disable SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
public SemanticKernelTextEmbeddingProvider(ITextEmbeddingGenerationService embedding, IConfiguration configuration)
#pragma warning restore SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
{
_embedding = embedding;
_configuration = configuration;
_dimension = configuration.GetValue("SemanticKernel:Dimension");
}
///
protected int _dimension;
public string Provider => "semantic-kernel";
///
public async Task GetVectorAsync(string text)
{
#pragma warning disable SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
return (await this._embedding.GenerateEmbeddingAsync(text)).ToArray();
#pragma warning restore SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
}
///
public async Task> GetVectorsAsync(List texts)
{
var embeddings = await this._embedding.GenerateEmbeddingsAsync(texts);
return embeddings.Select(_ => _.ToArray())
.ToList();
}
public void SetModelName(string model) { }
public void SetDimension(int dimension)
{
_dimension = dimension > 0 ? dimension : _configuration.GetValue("SemanticKernel:Dimension");
}
public int GetDimension()
{
return _dimension;
}
}
}