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.

LLamaEmbedder.cs 3.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using LLama.Native;
  2. using System;
  3. using LLama.Exceptions;
  4. using LLama.Abstractions;
  5. namespace LLama
  6. {
  7. /// <summary>
  8. /// The embedder for LLama, which supports getting embeddings from text.
  9. /// </summary>
  10. public sealed class LLamaEmbedder
  11. : IDisposable
  12. {
  13. private readonly LLamaContext _ctx;
  14. /// <summary>
  15. /// Dimension of embedding vectors
  16. /// </summary>
  17. public int EmbeddingSize => _ctx.EmbeddingSize;
  18. /// <summary>
  19. ///
  20. /// </summary>
  21. /// <param name="params"></param>
  22. public LLamaEmbedder(IModelParams @params)
  23. {
  24. @params.EmbeddingMode = true;
  25. using var weights = LLamaWeights.LoadFromFile(@params);
  26. _ctx = weights.CreateContext(@params);
  27. }
  28. /// <summary>
  29. /// Get the embeddings of the text.
  30. /// </summary>
  31. /// <param name="text"></param>
  32. /// <param name="threads">unused</param>
  33. /// <param name="addBos">Add bos to the text.</param>
  34. /// <param name="encoding">unused</param>
  35. /// <returns></returns>
  36. /// <exception cref="RuntimeError"></exception>
  37. [Obsolete("'threads' and 'encoding' parameters are no longer used")]
  38. // ReSharper disable once MethodOverloadWithOptionalParameter
  39. public float[] GetEmbeddings(string text, int threads = -1, bool addBos = true, string encoding = "UTF-8")
  40. {
  41. return GetEmbeddings(text, addBos);
  42. }
  43. /// <summary>
  44. /// Get the embeddings of the text.
  45. /// </summary>
  46. /// <param name="text"></param>
  47. /// <returns></returns>
  48. /// <exception cref="RuntimeError"></exception>
  49. public float[] GetEmbeddings(string text)
  50. {
  51. return GetEmbeddings(text, true);
  52. }
  53. /// <summary>
  54. /// Get the embeddings of the text.
  55. /// </summary>
  56. /// <param name="text"></param>
  57. /// <param name="addBos">Add bos to the text.</param>
  58. /// <returns></returns>
  59. /// <exception cref="RuntimeError"></exception>
  60. public float[] GetEmbeddings(string text, bool addBos)
  61. {
  62. if (addBos)
  63. {
  64. text = text.Insert(0, " ");
  65. }
  66. var embed_inp_array = _ctx.Tokenize(text, addBos);
  67. // TODO(Rinne): deal with log of prompt
  68. if (embed_inp_array.Length > 0)
  69. _ctx.Eval(embed_inp_array, 0);
  70. unsafe
  71. {
  72. var embeddings = NativeApi.llama_get_embeddings(_ctx.NativeHandle);
  73. if (embeddings == null)
  74. return Array.Empty<float>();
  75. return new Span<float>(embeddings, EmbeddingSize).ToArray();
  76. }
  77. }
  78. /// <summary>
  79. ///
  80. /// </summary>
  81. public void Dispose()
  82. {
  83. _ctx.Dispose();
  84. }
  85. }
  86. }