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 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using LLama;
  2. using LLama.Abstractions;
  3. using LLama.Common;
  4. using Microsoft.SemanticKernel.AI.Embeddings;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace LLamaSharp.KernelMemory
  11. {
  12. /// <summary>
  13. /// Provides text embedding generation for LLamaSharp.
  14. /// </summary>
  15. public class LLamaSharpTextEmbeddingGeneration : ITextEmbeddingGeneration, IDisposable
  16. {
  17. private readonly LLamaSharpConfig? _config;
  18. private readonly LLamaWeights? _weights;
  19. private readonly LLamaEmbedder _embedder;
  20. private bool _ownsEmbedder = false;
  21. private bool _ownsWeights = false;
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="LLamaSharpTextEmbeddingGeneration"/> class.
  24. /// </summary>
  25. /// <param name="config">The configuration for LLamaSharp.</param>
  26. public LLamaSharpTextEmbeddingGeneration(LLamaSharpConfig config)
  27. {
  28. this._config = config;
  29. var @params = new ModelParams(_config.ModelPath);
  30. _weights = LLamaWeights.LoadFromFile(@params);
  31. _embedder = new LLamaEmbedder(_weights, @params);
  32. _ownsWeights = true;
  33. _ownsEmbedder = true;
  34. }
  35. /// <summary>
  36. /// Initializes a new instance of the <see cref="LLamaSharpTextEmbeddingGeneration"/> class from reused weights.
  37. /// </summary>
  38. /// <param name="config">The configuration for LLamaSharp.</param>
  39. /// <param name="weights">A LLamaWeights object.</param>
  40. public LLamaSharpTextEmbeddingGeneration(LLamaSharpConfig config, LLamaWeights weights)
  41. {
  42. this._config = config;
  43. var @params = new ModelParams(_config.ModelPath);
  44. _weights = weights;
  45. _embedder = new LLamaEmbedder(_weights, @params);
  46. _ownsEmbedder = true;
  47. }
  48. /// <summary>
  49. /// Initializes a new instance of the <see cref="LLamaSharpTextEmbeddingGeneration"/> class from reused embedder.
  50. /// </summary>
  51. /// <param name="embedder">A LLamaEmbedder object.</param>
  52. public LLamaSharpTextEmbeddingGeneration(LLamaEmbedder embedder)
  53. {
  54. this._config = null;
  55. this._weights = null;
  56. _embedder = embedder;
  57. }
  58. /// <inheritdoc/>
  59. public void Dispose()
  60. {
  61. if (_ownsWeights)
  62. {
  63. _weights?.Dispose();
  64. }
  65. if(_ownsEmbedder)
  66. {
  67. _embedder.Dispose();
  68. }
  69. }
  70. /// <inheritdoc/>
  71. public Task<IList<ReadOnlyMemory<float>>> GenerateEmbeddingsAsync(IList<string> data, CancellationToken cancellationToken = default)
  72. {
  73. IList<ReadOnlyMemory<float>> results = new List<ReadOnlyMemory<float>>();
  74. foreach (var d in data)
  75. {
  76. var embeddings = _embedder.GetEmbeddings(d);
  77. results.Add(new ReadOnlyMemory<float>(embeddings));
  78. }
  79. return Task.FromResult(results);
  80. }
  81. }
  82. }