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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using LLama.Abstractions;
  2. using LLama.Web.Common;
  3. namespace LLama.Web.Models
  4. {
  5. public class ModelSession : IDisposable
  6. {
  7. private bool _isFirstInteraction = true;
  8. private ModelOptions _modelOptions;
  9. private PromptOptions _promptOptions;
  10. private ParameterOptions _inferenceOptions;
  11. private ITextStreamTransform _outputTransform;
  12. private ILLamaExecutor _executor;
  13. private CancellationTokenSource _cancellationTokenSource;
  14. public ModelSession(string connectionId, ILLamaExecutor executor, ModelOptions modelOptions, PromptOptions promptOptions, ParameterOptions parameterOptions)
  15. {
  16. ConnectionId = connectionId;
  17. _executor = executor;
  18. _modelOptions = modelOptions;
  19. _promptOptions = promptOptions;
  20. _inferenceOptions = parameterOptions;
  21. _inferenceOptions.AntiPrompts = _promptOptions.AntiPrompt?.Concat(_inferenceOptions.AntiPrompts ?? Enumerable.Empty<string>()).Distinct() ?? _inferenceOptions.AntiPrompts;
  22. if (_promptOptions.OutputFilter?.Count > 0)
  23. _outputTransform = new LLamaTransforms.KeywordTextOutputStreamTransform(_promptOptions.OutputFilter, redundancyLength: 5);
  24. }
  25. public string ConnectionId { get; }
  26. public IAsyncEnumerable<string> InferAsync(string message, CancellationTokenSource cancellationTokenSource)
  27. {
  28. _cancellationTokenSource = cancellationTokenSource;
  29. if (_isFirstInteraction)
  30. {
  31. _isFirstInteraction = false;
  32. message = _promptOptions.Prompt + message;
  33. }
  34. if (_outputTransform is not null)
  35. return _outputTransform.TransformAsync(_executor.InferAsync(message, _inferenceOptions, _cancellationTokenSource.Token));
  36. return _executor.InferAsync(message, _inferenceOptions, _cancellationTokenSource.Token);
  37. }
  38. public void CancelInfer()
  39. {
  40. _cancellationTokenSource?.Cancel();
  41. }
  42. public bool IsInferCanceled()
  43. {
  44. return _cancellationTokenSource.IsCancellationRequested;
  45. }
  46. public void Dispose()
  47. {
  48. _inferenceOptions = null;
  49. _outputTransform = null;
  50. _executor.Model?.Dispose();
  51. _executor = null;
  52. }
  53. }
  54. }