LLamaSharp/LLama.SemanticKernel/TextEmbedding/LLamaSharpEmbeddingGenerati...

31 lines
935 B
C#

using LLama;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Embeddings;
namespace LLamaSharp.SemanticKernel.TextEmbedding;
public sealed class LLamaSharpEmbeddingGeneration : ITextEmbeddingGenerationService
{
private readonly LLamaEmbedder _embedder;
private readonly Dictionary<string, object?> _attributes = new();
public IReadOnlyDictionary<string, object?> Attributes => this._attributes;
public LLamaSharpEmbeddingGeneration(LLamaEmbedder embedder)
{
_embedder = embedder;
}
/// <inheritdoc/>
public async Task<IList<ReadOnlyMemory<float>>> GenerateEmbeddingsAsync(IList<string> data, Kernel? kernel = null, CancellationToken cancellationToken = default)
{
var result = new List<ReadOnlyMemory<float>>();
foreach (var item in data)
result.Add(await _embedder.GetEmbeddings(item, cancellationToken));
return result;
}
}