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.

LLamaSharpTextEmbeddingGenerator.cs 3.6 kB

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