Browse Source

Added workaround to LLama.Web and LLama.WebAPI

tags/v0.6.0
Martin Evans 2 years ago
parent
commit
0f03e8f1a3
3 changed files with 22 additions and 11 deletions
  1. +6
    -6
      LLama.Web/Services/ConnectionSessionService.cs
  2. +8
    -3
      LLama.WebAPI/Services/StatefulChatService.cs
  3. +8
    -2
      LLama.WebAPI/Services/StatelessChatService.cs

+ 6
- 6
LLama.Web/Services/ConnectionSessionService.cs View File

@@ -3,7 +3,6 @@ using LLama.Web.Common;
using LLama.Web.Models; using LLama.Web.Models;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Drawing;


namespace LLama.Web.Services namespace LLama.Web.Services
{ {
@@ -50,15 +49,16 @@ namespace LLama.Web.Services
if (modelOption.MaxInstances > -1 && currentInstances >= modelOption.MaxInstances) if (modelOption.MaxInstances > -1 && currentInstances >= modelOption.MaxInstances)
return Task.FromResult(ServiceResult.FromError<ModelSession>("Maximum model instances reached")); return Task.FromResult(ServiceResult.FromError<ModelSession>("Maximum model instances reached"));


// Create model
var llamaModel = new LLamaContext(modelOption);
// Load weights
// todo: it would be better to have a central service which loads weights and shares them between all contexts that need them!
using var weights = LLamaWeights.LoadFromFile(modelOption);


// Create executor // Create executor
ILLamaExecutor executor = executorType switch ILLamaExecutor executor = executorType switch
{ {
LLamaExecutorType.Interactive => new InteractiveExecutor(llamaModel),
LLamaExecutorType.Instruct => new InstructExecutor(llamaModel),
LLamaExecutorType.Stateless => new StatelessExecutor(llamaModel),
LLamaExecutorType.Interactive => new InteractiveExecutor(new LLamaContext(weights, modelOption)), //todo: properly dispose of LLamaContext
LLamaExecutorType.Instruct => new InstructExecutor(new LLamaContext(weights, modelOption)), //todo: properly dispose of LLamaContext
LLamaExecutorType.Stateless => new StatelessExecutor(weights, modelOption),
_ => default _ => default
}; };




+ 8
- 3
LLama.WebAPI/Services/StatefulChatService.cs View File

@@ -16,10 +16,15 @@ public class StatefulChatService : IDisposable


public StatefulChatService(IConfiguration configuration) public StatefulChatService(IConfiguration configuration)
{ {
_context = new LLamaContext(new Common.ModelParams(configuration["ModelPath"])
var @params = new Common.ModelParams(configuration["ModelPath"])
{ {
ContextSize = 512
});
ContextSize = 512,
};

// todo: share weights from a central service
using var weights = LLamaWeights.LoadFromFile(@params);

_context = new LLamaContext(weights, @params);
_session = new ChatSession(new InteractiveExecutor(_context)); _session = new ChatSession(new InteractiveExecutor(_context));
} }




+ 8
- 2
LLama.WebAPI/Services/StatelessChatService.cs View File

@@ -12,10 +12,16 @@ namespace LLama.WebAPI.Services


public StatelessChatService(IConfiguration configuration) public StatelessChatService(IConfiguration configuration)
{ {
_context = new LLamaContext(new ModelParams(configuration["ModelPath"])
var @params = new Common.ModelParams(configuration["ModelPath"])
{ {
ContextSize = 512, ContextSize = 512,
});
};

// todo: share weights from a central service
using var weights = LLamaWeights.LoadFromFile(@params);

_context = new LLamaContext(weights, @params);

// TODO: replace with a stateless executor // TODO: replace with a stateless executor
_session = new ChatSession(new InteractiveExecutor(_context)) _session = new ChatSession(new InteractiveExecutor(_context))
.WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(new string[] { "User:", "Assistant:" }, redundancyLength: 8)) .WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(new string[] { "User:", "Assistant:" }, redundancyLength: 8))


Loading…
Cancel
Save