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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using LLama.Abstractions;
  2. namespace LLama.Web.Models
  3. {
  4. public class ModelSession : IDisposable
  5. {
  6. private bool _isFirstInteraction = true;
  7. private ModelOptions _modelOptions;
  8. private PromptOptions _promptOptions;
  9. private ParameterOptions _inferenceOptions;
  10. private ITextStreamTransform _outputTransform;
  11. private ILLamaExecutor _executor;
  12. private CancellationTokenSource _cancellationTokenSource;
  13. public ModelSession(string connectionId, ILLamaExecutor executor, ModelOptions modelOptions, PromptOptions promptOptions, ParameterOptions parameterOptions)
  14. {
  15. ConnectionId = connectionId;
  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 ConnectionId { get; }
  25. public IAsyncEnumerable<string> InferAsync(string message, CancellationTokenSource cancellationTokenSource)
  26. {
  27. _cancellationTokenSource = cancellationTokenSource;
  28. if (_isFirstInteraction)
  29. {
  30. _isFirstInteraction = false;
  31. message = _promptOptions.Prompt + message;
  32. }
  33. if (_outputTransform is not null)
  34. return _outputTransform.TransformAsync(_executor.InferAsync(message, _inferenceOptions, _cancellationTokenSource.Token));
  35. return _executor.InferAsync(message, _inferenceOptions, _cancellationTokenSource.Token);
  36. }
  37. public void CancelInfer()
  38. {
  39. _cancellationTokenSource?.Cancel();
  40. }
  41. public bool IsInferCanceled()
  42. {
  43. return _cancellationTokenSource.IsCancellationRequested;
  44. }
  45. public void Dispose()
  46. {
  47. _inferenceOptions = null;
  48. _outputTransform = null;
  49. _executor.Model?.Dispose();
  50. _executor = null;
  51. }
  52. }
  53. }