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.

LLamaModel.cs 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using LLama.Abstractions;
  2. using LLama.Web.Common;
  3. using System.Collections.Concurrent;
  4. namespace LLama.Web.Models
  5. {
  6. /// <summary>
  7. /// Wrapper class for LLamaSharp LLamaWeights
  8. /// </summary>
  9. /// <seealso cref="IDisposable" />
  10. public class LLamaModel : IDisposable
  11. {
  12. private readonly ILogger _llamaLogger;
  13. private readonly ModelOptions _config;
  14. private readonly LLamaWeights _weights;
  15. private readonly ConcurrentDictionary<string, LLamaContext> _contexts;
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="LLamaModel"/> class.
  18. /// </summary>
  19. /// <param name="modelParams">The model parameters.</param>
  20. public LLamaModel(ModelOptions modelParams, ILogger llamaLogger)
  21. {
  22. _config = modelParams;
  23. _llamaLogger = llamaLogger;
  24. _weights = LLamaWeights.LoadFromFile(modelParams);
  25. _contexts = new ConcurrentDictionary<string, LLamaContext>();
  26. }
  27. /// <summary>
  28. /// Gets the model configuration.
  29. /// </summary>
  30. public IModelParams ModelParams => _config;
  31. /// <summary>
  32. /// Gets the LLamaWeights
  33. /// </summary>
  34. public LLamaWeights LLamaWeights => _weights;
  35. /// <summary>
  36. /// Gets the context count.
  37. /// </summary>
  38. public int ContextCount => _contexts.Count;
  39. /// <summary>
  40. /// Creates a new context session on this model
  41. /// </summary>
  42. /// <param name="contextName">The unique context identifier</param>
  43. /// <returns>LLamaModelContext for this LLamaModel</returns>
  44. /// <exception cref="Exception">Context exists</exception>
  45. public Task<LLamaContext> CreateContext(string contextName)
  46. {
  47. if (_contexts.TryGetValue(contextName, out var context))
  48. throw new Exception($"Context with id {contextName} already exists.");
  49. if (_config.MaxInstances > -1 && ContextCount >= _config.MaxInstances)
  50. throw new Exception($"Maximum model instances reached");
  51. context = _weights.CreateContext(_config, _llamaLogger);
  52. if (_contexts.TryAdd(contextName, context))
  53. return Task.FromResult(context);
  54. return Task.FromResult<LLamaContext>(null);
  55. }
  56. /// <summary>
  57. /// Get a contexts belonging to this model
  58. /// </summary>
  59. /// <param name="contextName">The unique context identifier</param>
  60. /// <returns>LLamaModelContext for this LLamaModel with the specified contextName</returns>
  61. public Task<LLamaContext> GetContext(string contextName)
  62. {
  63. if (_contexts.TryGetValue(contextName, out var context))
  64. return Task.FromResult(context);
  65. return Task.FromResult<LLamaContext>(null);
  66. }
  67. /// <summary>
  68. /// Remove a context from this model
  69. /// </summary>
  70. /// <param name="contextName">The unique context identifier</param>
  71. /// <returns>true if removed, otherwise false</returns>
  72. public Task<bool> RemoveContext(string contextName)
  73. {
  74. if (!_contexts.TryRemove(contextName, out var context))
  75. return Task.FromResult(false);
  76. context?.Dispose();
  77. return Task.FromResult(true);
  78. }
  79. /// <summary>
  80. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  81. /// </summary>
  82. public void Dispose()
  83. {
  84. foreach (var context in _contexts.Values)
  85. {
  86. context?.Dispose();
  87. }
  88. _weights.Dispose();
  89. }
  90. }
  91. }