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.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. private readonly Dictionary<string, string> _attributes = new();
  23. public IReadOnlyDictionary<string, string> Attributes => this._attributes;
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="LLamaSharpTextEmbeddingGeneration"/> class.
  26. /// </summary>
  27. /// <param name="config">The configuration for LLamaSharp.</param>
  28. public LLamaSharpTextEmbeddingGeneration(LLamaSharpConfig config)
  29. {
  30. this._config = config;
  31. var @params = new ModelParams(_config.ModelPath);
  32. _weights = LLamaWeights.LoadFromFile(@params);
  33. _embedder = new LLamaEmbedder(_weights, @params);
  34. _ownsWeights = true;
  35. _ownsEmbedder = true;
  36. }
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="LLamaSharpTextEmbeddingGeneration"/> class from reused weights.
  39. /// </summary>
  40. /// <param name="config">The configuration for LLamaSharp.</param>
  41. /// <param name="weights">A LLamaWeights object.</param>
  42. public LLamaSharpTextEmbeddingGeneration(LLamaSharpConfig config, LLamaWeights weights)
  43. {
  44. this._config = config;
  45. var @params = new ModelParams(_config.ModelPath);
  46. _weights = weights;
  47. _embedder = new LLamaEmbedder(_weights, @params);
  48. _ownsEmbedder = true;
  49. }
  50. /// <summary>
  51. /// Initializes a new instance of the <see cref="LLamaSharpTextEmbeddingGeneration"/> class from reused embedder.
  52. /// </summary>
  53. /// <param name="embedder">A LLamaEmbedder object.</param>
  54. public LLamaSharpTextEmbeddingGeneration(LLamaEmbedder embedder)
  55. {
  56. this._config = null;
  57. this._weights = null;
  58. _embedder = embedder;
  59. }
  60. /// <inheritdoc/>
  61. public void Dispose()
  62. {
  63. if (_ownsWeights)
  64. {
  65. _weights?.Dispose();
  66. }
  67. if (_ownsEmbedder)
  68. {
  69. _embedder.Dispose();
  70. }
  71. }
  72. /// <inheritdoc/>
  73. public Task<IList<ReadOnlyMemory<float>>> GenerateEmbeddingsAsync(IList<string> data, CancellationToken cancellationToken = default)
  74. {
  75. IList<ReadOnlyMemory<float>> results = new List<ReadOnlyMemory<float>>();
  76. foreach (var d in data)
  77. {
  78. var embeddings = _embedder.GetEmbeddings(d);
  79. results.Add(new ReadOnlyMemory<float>(embeddings));
  80. }
  81. return Task.FromResult(results);
  82. }
  83. }
  84. }