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.

ModelSession.cs 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using LLama.Abstractions;
  2. using LLama.Web.Common;
  3. namespace LLama.Web.Models;
  4. public class ModelSession
  5. {
  6. private readonly string _sessionId;
  7. private readonly LLamaModel _model;
  8. private readonly LLamaContext _context;
  9. private readonly ILLamaExecutor _executor;
  10. private readonly ISessionConfig _sessionConfig;
  11. private readonly ITextStreamTransform _outputTransform;
  12. private readonly InferenceOptions _defaultInferenceConfig;
  13. private CancellationTokenSource _cancellationTokenSource;
  14. public ModelSession(LLamaModel model, LLamaContext context, string sessionId, ISessionConfig sessionConfig, InferenceOptions inferenceOptions = null)
  15. {
  16. _model = model;
  17. _context = context;
  18. _sessionId = sessionId;
  19. _sessionConfig = sessionConfig;
  20. _defaultInferenceConfig = inferenceOptions ?? new InferenceOptions();
  21. _outputTransform = CreateOutputFilter();
  22. _executor = CreateExecutor();
  23. }
  24. /// <summary>
  25. /// Gets the session identifier.
  26. /// </summary>
  27. public string SessionId => _sessionId;
  28. /// <summary>
  29. /// Gets the name of the model.
  30. /// </summary>
  31. public string ModelName => _sessionConfig.Model;
  32. /// <summary>
  33. /// Gets the context.
  34. /// </summary>
  35. public LLamaContext Context => _context;
  36. /// <summary>
  37. /// Gets the session configuration.
  38. /// </summary>
  39. public ISessionConfig SessionConfig => _sessionConfig;
  40. /// <summary>
  41. /// Gets the inference parameters.
  42. /// </summary>
  43. public InferenceOptions InferenceParams => _defaultInferenceConfig;
  44. /// <summary>
  45. /// Initializes the prompt.
  46. /// </summary>
  47. /// <param name="inferenceConfig">The inference configuration.</param>
  48. /// <param name="cancellationToken">The cancellation token.</param>
  49. internal async Task InitializePrompt(InferenceOptions inferenceConfig = null, CancellationToken cancellationToken = default)
  50. {
  51. if (_sessionConfig.ExecutorType == LLamaExecutorType.Stateless)
  52. return;
  53. if (string.IsNullOrEmpty(_sessionConfig.Prompt))
  54. return;
  55. // Run Initial prompt
  56. var inferenceParams = ConfigureInferenceParams(inferenceConfig);
  57. _cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
  58. await foreach (var _ in _executor.InferAsync(_sessionConfig.Prompt, inferenceParams, _cancellationTokenSource.Token))
  59. {
  60. // We dont really need the response of the initial prompt, so exit on first token
  61. break;
  62. };
  63. }
  64. /// <summary>
  65. /// Runs inference on the model context
  66. /// </summary>
  67. /// <param name="message">The message.</param>
  68. /// <param name="inferenceConfig">The inference configuration.</param>
  69. /// <param name="cancellationToken">The cancellation token.</param>
  70. /// <returns></returns>
  71. internal IAsyncEnumerable<string> InferAsync(string message, InferenceOptions inferenceConfig = null, CancellationToken cancellationToken = default)
  72. {
  73. var inferenceParams = ConfigureInferenceParams(inferenceConfig);
  74. _cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
  75. var inferenceStream = _executor.InferAsync(message, inferenceParams, _cancellationTokenSource.Token);
  76. if (_outputTransform is not null)
  77. return _outputTransform.TransformAsync(inferenceStream);
  78. return inferenceStream;
  79. }
  80. public void CancelInfer()
  81. {
  82. _cancellationTokenSource?.Cancel();
  83. }
  84. public bool IsInferCanceled()
  85. {
  86. return _cancellationTokenSource.IsCancellationRequested;
  87. }
  88. /// <summary>
  89. /// Configures the inference parameters.
  90. /// </summary>
  91. /// <param name="inferenceConfig">The inference configuration.</param>
  92. private IInferenceParams ConfigureInferenceParams(InferenceOptions inferenceConfig)
  93. {
  94. var inferenceParams = inferenceConfig ?? _defaultInferenceConfig;
  95. inferenceParams.AntiPrompts = _sessionConfig.GetAntiPrompts();
  96. return inferenceParams;
  97. }
  98. private ITextStreamTransform CreateOutputFilter()
  99. {
  100. var outputFilters = _sessionConfig.GetOutputFilters();
  101. if (outputFilters.Count > 0)
  102. return new LLamaTransforms.KeywordTextOutputStreamTransform(outputFilters);
  103. return null;
  104. }
  105. private ILLamaExecutor CreateExecutor()
  106. {
  107. return _sessionConfig.ExecutorType switch
  108. {
  109. LLamaExecutorType.Interactive => new InteractiveExecutor(_context),
  110. LLamaExecutorType.Instruct => new InstructExecutor(_context),
  111. LLamaExecutorType.Stateless => new StatelessExecutor(_model.LLamaWeights, _context.Params),
  112. _ => default
  113. };
  114. }
  115. }