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

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