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.

LlamaSharpTextGeneration.cs 3.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using LLama;
  2. using LLama.Abstractions;
  3. using LLama.Common;
  4. using Microsoft.KernelMemory.AI;
  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 generation for LLamaSharp.
  14. /// </summary>
  15. public class LlamaSharpTextGeneration : ITextGeneration, IDisposable
  16. {
  17. private readonly LLamaSharpConfig? _config;
  18. private readonly LLamaWeights _weights;
  19. private readonly StatelessExecutor _executor;
  20. private readonly LLamaContext _context;
  21. private bool _ownsContext = false;
  22. private bool _ownsWeights = false;
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="LlamaSharpTextGeneration"/> class.
  25. /// </summary>
  26. /// <param name="config">The configuration for LLamaSharp.</param>
  27. public LlamaSharpTextGeneration(LLamaSharpConfig config)
  28. {
  29. this._config = config;
  30. var parameters = new ModelParams(config.ModelPath)
  31. {
  32. ContextSize = config?.ContextSize ?? 2048,
  33. Seed = config?.Seed ?? 0,
  34. GpuLayerCount = config?.GpuLayerCount ?? 20
  35. };
  36. _weights = LLamaWeights.LoadFromFile(parameters);
  37. _context = _weights.CreateContext(parameters);
  38. _executor = new StatelessExecutor(_weights, parameters);
  39. _ownsWeights = _ownsContext = true;
  40. }
  41. /// <summary>
  42. /// Initializes a new instance of the <see cref="LlamaSharpTextGeneration"/> class from reused weights, context and executor.
  43. /// If executor is not specified, then a StatelessExecutor will be created with `context.Params`. So far only `StatelessExecutor` is expected.
  44. /// </summary>
  45. /// <param name="weights">A LLamaWeights object.</param>
  46. /// <param name="context">A LLamaContext object.</param>
  47. /// <param name="executor">An executor. Currently only StatelessExecutor is expected.</param>
  48. public LlamaSharpTextGeneration(LLamaWeights weights, LLamaContext context, StatelessExecutor? executor = null)
  49. {
  50. _config = null;
  51. _weights = weights;
  52. _context = context;
  53. _executor = executor ?? new StatelessExecutor(_weights, _context.Params);
  54. }
  55. /// <inheritdoc/>
  56. public void Dispose()
  57. {
  58. if (_ownsWeights)
  59. {
  60. _weights?.Dispose();
  61. }
  62. if (_ownsContext)
  63. {
  64. _context.Dispose();
  65. }
  66. }
  67. /// <inheritdoc/>
  68. public IAsyncEnumerable<string> GenerateTextAsync(string prompt, TextGenerationOptions options, CancellationToken cancellationToken = default)
  69. {
  70. return _executor.InferAsync(prompt, OptionsToParams(options), cancellationToken: cancellationToken);
  71. }
  72. private static InferenceParams OptionsToParams(TextGenerationOptions options)
  73. {
  74. return new InferenceParams()
  75. {
  76. AntiPrompts = options.StopSequences.ToList().AsReadOnly(),
  77. Temperature = (float)options.Temperature,
  78. MaxTokens = options.MaxTokens ?? 1024,
  79. FrequencyPenalty = (float)options.FrequencyPenalty,
  80. PresencePenalty = (float)options.PresencePenalty,
  81. TopP = (float)options.TopP,
  82. };
  83. }
  84. }
  85. }