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.

LLamaSharpTextEmbeddingGeneration.cs 1.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using LLama;
  2. using LLama.Common;
  3. using Microsoft.SemanticKernel.AI.Embeddings;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace LLamaSharp.KernelMemory
  10. {
  11. public class LLamaSharpTextEmbeddingGeneration : ITextEmbeddingGeneration, IDisposable
  12. {
  13. private readonly LlamaSharpConfig _config;
  14. private readonly LLamaEmbedder _embedder;
  15. private readonly LLamaWeights _weights;
  16. public LLamaSharpTextEmbeddingGeneration(LlamaSharpConfig config)
  17. {
  18. this._config = config;
  19. var @params = new ModelParams(_config.ModelPath);
  20. _weights = LLamaWeights.LoadFromFile(@params);
  21. _embedder = new LLamaEmbedder(_weights, @params);
  22. }
  23. public void Dispose()
  24. {
  25. _embedder.Dispose();
  26. _weights.Dispose();
  27. }
  28. public Task<IList<ReadOnlyMemory<float>>> GenerateEmbeddingsAsync(IList<string> data, CancellationToken cancellationToken = default)
  29. {
  30. IList<ReadOnlyMemory<float>> results = new List<ReadOnlyMemory<float>>();
  31. foreach (var d in data)
  32. {
  33. var embeddings = _embedder.GetEmbeddings(d);
  34. results.Add(new ReadOnlyMemory<float>(embeddings));
  35. }
  36. return Task.FromResult(results);
  37. }
  38. }
  39. }