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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using LLama;
  2. using LLama.Common;
  3. using Microsoft.KernelMemory.AI;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace LLamaSharp.KernelMemory
  10. {
  11. /// <summary>
  12. /// Provides text generation for LLamaSharp.
  13. /// </summary>
  14. public class LlamaSharpTextGeneration : ITextGeneration, IDisposable
  15. {
  16. private readonly LLamaSharpConfig _config;
  17. private readonly LLamaWeights _weights;
  18. private readonly InstructExecutor _executor;
  19. private readonly LLamaContext _context;
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="LlamaSharpTextGeneration"/> class.
  22. /// </summary>
  23. /// <param name="config">The configuration for LLamaSharp.</param>
  24. public LlamaSharpTextGeneration(LLamaSharpConfig config)
  25. {
  26. this._config = config;
  27. var parameters = new ModelParams(config.ModelPath)
  28. {
  29. ContextSize = config?.ContextSize ?? 1024,
  30. Seed = config?.Seed ?? 0,
  31. GpuLayerCount = config?.GpuLayerCount ?? 20
  32. };
  33. _weights = LLamaWeights.LoadFromFile(parameters);
  34. _context = _weights.CreateContext(parameters);
  35. _executor = new InstructExecutor(_context);
  36. }
  37. /// <inheritdoc/>
  38. public void Dispose()
  39. {
  40. _context.Dispose();
  41. _weights.Dispose();
  42. }
  43. /// <inheritdoc/>
  44. public IAsyncEnumerable<string> GenerateTextAsync(string prompt, TextGenerationOptions options, CancellationToken cancellationToken = default)
  45. {
  46. return _executor.InferAsync(prompt, OptionsToParams(options), cancellationToken: cancellationToken);
  47. }
  48. private static InferenceParams OptionsToParams(TextGenerationOptions options)
  49. {
  50. return new InferenceParams()
  51. {
  52. AntiPrompts = options.StopSequences,
  53. Temperature = (float)options.Temperature,
  54. MaxTokens = options.MaxTokens ?? 1024,
  55. FrequencyPenalty = (float)options.FrequencyPenalty,
  56. PresencePenalty = (float)options.PresencePenalty,
  57. TopP = (float)options.TopP,
  58. };
  59. }
  60. }
  61. }