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. { this._embedding = embedding; this._configuration = configuration; this.Dimension = configuration.GetValue("SemanticKernel:Dimension"); } /// public int Dimension { get; set; } 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(); } } }