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

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