using LLama.Abstractions; using LLama.Web.Common; using System.Collections.Concurrent; namespace LLama.Web.Models { /// /// Wrapper class for LLamaSharp LLamaWeights /// /// public class LLamaModel : IDisposable { private readonly ILogger _llamaLogger; private readonly ModelOptions _config; private readonly LLamaWeights _weights; private readonly ConcurrentDictionary _contexts; /// /// Initializes a new instance of the class. /// /// The model parameters. public LLamaModel(ModelOptions modelParams, ILogger llamaLogger) { _config = modelParams; _llamaLogger = llamaLogger; _weights = LLamaWeights.LoadFromFile(modelParams); _contexts = new ConcurrentDictionary(); } /// /// Gets the model configuration. /// public IModelParams ModelParams => _config; /// /// Gets the LLamaWeights /// public LLamaWeights LLamaWeights => _weights; /// /// Gets the context count. /// public int ContextCount => _contexts.Count; /// /// Creates a new context session on this model /// /// The unique context identifier /// LLamaModelContext for this LLamaModel /// Context exists public Task CreateContext(string contextName) { if (_contexts.TryGetValue(contextName, out var context)) throw new Exception($"Context with id {contextName} already exists."); if (_config.MaxInstances > -1 && ContextCount >= _config.MaxInstances) throw new Exception($"Maximum model instances reached"); context = _weights.CreateContext(_config, _llamaLogger); if (_contexts.TryAdd(contextName, context)) return Task.FromResult(context); return Task.FromResult(null); } /// /// Get a contexts belonging to this model /// /// The unique context identifier /// LLamaModelContext for this LLamaModel with the specified contextName public Task GetContext(string contextName) { if (_contexts.TryGetValue(contextName, out var context)) return Task.FromResult(context); return Task.FromResult(null); } /// /// Remove a context from this model /// /// The unique context identifier /// true if removed, otherwise false public Task RemoveContext(string contextName) { if (!_contexts.TryRemove(contextName, out var context)) return Task.FromResult(false); context?.Dispose(); return Task.FromResult(true); } /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// public void Dispose() { foreach (var context in _contexts.Values) { context?.Dispose(); } _weights.Dispose(); } } }