You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

LLamaSharpEmbeddingGeneration.cs 893 B

1234567891011121314151617181920212223242526
  1. using LLama;
  2. using Microsoft.SemanticKernel;
  3. using Microsoft.SemanticKernel.Embeddings;
  4. namespace LLamaSharp.SemanticKernel.TextEmbedding;
  5. public sealed class LLamaSharpEmbeddingGeneration : ITextEmbeddingGenerationService
  6. {
  7. private LLamaEmbedder _embedder;
  8. private readonly Dictionary<string, object?> _attributes = new();
  9. public IReadOnlyDictionary<string, object?> Attributes => this._attributes;
  10. public LLamaSharpEmbeddingGeneration(LLamaEmbedder embedder)
  11. {
  12. _embedder = embedder;
  13. }
  14. /// <inheritdoc/>
  15. public async Task<IList<ReadOnlyMemory<float>>> GenerateEmbeddingsAsync(IList<string> data, Kernel? kernel = null, CancellationToken cancellationToken = default)
  16. {
  17. var embeddings = data.Select(text => new ReadOnlyMemory<float>(_embedder.GetEmbeddings(text))).ToList();
  18. return await Task.FromResult(embeddings);
  19. }
  20. }