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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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(ILLamaExecutor executor, ModelOptions modelOptions, PromptOptions promptOptions, ParameterOptions parameterOptions)
  15. {
  16. _executor = executor;
  17. _modelOptions = modelOptions;
  18. _promptOptions = promptOptions;
  19. _inferenceOptions = parameterOptions;
  20. _inferenceOptions.AntiPrompts = _promptOptions.AntiPrompt?.Concat(_inferenceOptions.AntiPrompts ?? Enumerable.Empty<string>()).Distinct() ?? _inferenceOptions.AntiPrompts;
  21. if (_promptOptions.OutputFilter?.Count > 0)
  22. _outputTransform = new LLamaTransforms.KeywordTextOutputStreamTransform(_promptOptions.OutputFilter, redundancyLength: 5);
  23. }
  24. public string ModelName
  25. {
  26. get { return _modelOptions.Name; }
  27. }
  28. public IAsyncEnumerable<string> InferAsync(string message, CancellationTokenSource cancellationTokenSource)
  29. {
  30. _cancellationTokenSource = cancellationTokenSource;
  31. if (_isFirstInteraction)
  32. {
  33. _isFirstInteraction = false;
  34. message = _promptOptions.Prompt + message;
  35. }
  36. if (_outputTransform is not null)
  37. return _outputTransform.TransformAsync(_executor.InferAsync(message, _inferenceOptions, _cancellationTokenSource.Token));
  38. return _executor.InferAsync(message, _inferenceOptions, _cancellationTokenSource.Token);
  39. }
  40. public void CancelInfer()
  41. {
  42. _cancellationTokenSource?.Cancel();
  43. }
  44. public bool IsInferCanceled()
  45. {
  46. return _cancellationTokenSource.IsCancellationRequested;
  47. }
  48. public void Dispose()
  49. {
  50. _inferenceOptions = null;
  51. _outputTransform = null;
  52. _executor.Model?.Dispose();
  53. _executor = null;
  54. }
  55. }
  56. }