forked from SciSharp/LLamaSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Several updates to web project (SciSharp#718)
* * Updated to .NET 8 * Updated to Bootstrap 5.3.3 * Use libman for client side packages * Enabled razor runtime compilation in dev * Added fav icon * Removed the footer * Added theme switcher * Use CDN for Bootstrap Icons. * Use CDN for bootstrap also. * Update LLama.Web/Common/ModelOptions.cs Co-authored-by: Rinne <liu_yaohui1998@126.com> * Async model loading. * Updated images on web README. * Corrected filenames. * Fixed typo and updated images. * Menu tweaks. * Removed unused code. --------- Co-authored-by: Rinne <AsakusaRinne@gmail.com>
- Loading branch information
1 parent
4657e98
commit 4192bea
Showing
92 changed files
with
10,771 additions
and
64,142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,23 @@ | ||
namespace LLama.Web.Common | ||
using System.ComponentModel; | ||
|
||
namespace LLama.Web.Common; | ||
|
||
public interface ISessionConfig | ||
{ | ||
public interface ISessionConfig | ||
{ | ||
string AntiPrompt { get; set; } | ||
List<string> AntiPrompts { get; set; } | ||
LLamaExecutorType ExecutorType { get; set; } | ||
string Model { get; set; } | ||
string OutputFilter { get; set; } | ||
List<string> OutputFilters { get; set; } | ||
string Prompt { get; set; } | ||
} | ||
string AntiPrompt { get; set; } | ||
|
||
[DisplayName("Anti Prompts")] | ||
List<string> AntiPrompts { get; set; } | ||
|
||
[DisplayName("Executor Type")] | ||
LLamaExecutorType ExecutorType { get; set; } | ||
|
||
string Model { get; set; } | ||
|
||
[DisplayName("Output Filter")] | ||
string OutputFilter { get; set; } | ||
|
||
List<string> OutputFilters { get; set; } | ||
|
||
string Prompt { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,7 @@ | ||
namespace LLama.Web.Common | ||
{ | ||
public class LLamaOptions | ||
{ | ||
public ModelLoadType ModelLoadType { get; set; } | ||
public List<ModelOptions> Models { get; set; } | ||
namespace LLama.Web.Common; | ||
|
||
public void Initialize() | ||
{ | ||
} | ||
} | ||
public class LLamaOptions | ||
{ | ||
public ModelLoadType ModelLoadType { get; set; } | ||
public List<ModelOptions> Models { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
namespace LLama.Web.Common | ||
namespace LLama.Web.Common; | ||
|
||
public class SessionConfig : ISessionConfig | ||
{ | ||
public class SessionConfig : ISessionConfig | ||
{ | ||
public string Model { get; set; } | ||
public string Prompt { get; set; } | ||
public string Model { get; set; } | ||
public string Prompt { get; set; } | ||
|
||
public string AntiPrompt { get; set; } | ||
public List<string> AntiPrompts { get; set; } | ||
public string OutputFilter { get; set; } | ||
public List<string> OutputFilters { get; set; } | ||
public LLamaExecutorType ExecutorType { get; set; } | ||
} | ||
public string AntiPrompt { get; set; } | ||
public List<string> AntiPrompts { get; set; } | ||
public string OutputFilter { get; set; } | ||
public List<string> OutputFilters { get; set; } | ||
public LLamaExecutorType ExecutorType { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,52 @@ | ||
using LLama.Web.Common; | ||
using LLama.Web.Common; | ||
|
||
namespace LLama.Web | ||
namespace LLama.Web; | ||
|
||
public static class Extensions | ||
{ | ||
public static class Extensions | ||
/// <summary> | ||
/// Combines the AntiPrompts list and AntiPrompt csv | ||
/// </summary> | ||
/// <param name="sessionConfig">The session configuration.</param> | ||
/// <returns>Combined AntiPrompts with duplicates removed</returns> | ||
public static List<string> GetAntiPrompts(this ISessionConfig sessionConfig) | ||
{ | ||
/// <summary> | ||
/// Combines the AntiPrompts list and AntiPrompt csv | ||
/// </summary> | ||
/// <param name="sessionConfig">The session configuration.</param> | ||
/// <returns>Combined AntiPrompts with duplicates removed</returns> | ||
public static List<string> GetAntiPrompts(this ISessionConfig sessionConfig) | ||
{ | ||
return CombineCSV(sessionConfig.AntiPrompts, sessionConfig.AntiPrompt); | ||
} | ||
|
||
/// <summary> | ||
/// Combines the OutputFilters list and OutputFilter csv | ||
/// </summary> | ||
/// <param name="sessionConfig">The session configuration.</param> | ||
/// <returns>Combined OutputFilters with duplicates removed</returns> | ||
public static List<string> GetOutputFilters(this ISessionConfig sessionConfig) | ||
{ | ||
return CombineCSV(sessionConfig.OutputFilters, sessionConfig.OutputFilter); | ||
} | ||
return CombineCSV(sessionConfig.AntiPrompts, sessionConfig.AntiPrompt); | ||
} | ||
|
||
/// <summary> | ||
/// Combines the OutputFilters list and OutputFilter csv | ||
/// </summary> | ||
/// <param name="sessionConfig">The session configuration.</param> | ||
/// <returns>Combined OutputFilters with duplicates removed</returns> | ||
public static List<string> GetOutputFilters(this ISessionConfig sessionConfig) | ||
{ | ||
return CombineCSV(sessionConfig.OutputFilters, sessionConfig.OutputFilter); | ||
} | ||
|
||
/// <summary> | ||
/// Combines a string list and a csv and removes duplicates | ||
/// </summary> | ||
/// <param name="list">The list.</param> | ||
/// <param name="csv">The CSV.</param> | ||
/// <returns>Combined list with duplicates removed</returns> | ||
private static List<string> CombineCSV(List<string> list, string csv) | ||
{ | ||
var results = list is null || list.Count == 0 | ||
? CommaSeparatedToList(csv) | ||
: CommaSeparatedToList(csv).Concat(list); | ||
return results | ||
.Distinct() | ||
.ToList(); | ||
} | ||
/// <summary> | ||
/// Combines a string list and a csv and removes duplicates | ||
/// </summary> | ||
/// <param name="list">The list.</param> | ||
/// <param name="csv">The CSV.</param> | ||
/// <returns>Combined list with duplicates removed</returns> | ||
private static List<string> CombineCSV(List<string> list, string csv) | ||
{ | ||
var results = list is null || list.Count == 0 | ||
? CommaSeparatedToList(csv) | ||
: CommaSeparatedToList(csv).Concat(list); | ||
return results | ||
.Distinct() | ||
.ToList(); | ||
} | ||
|
||
private static List<string> CommaSeparatedToList(string value) | ||
{ | ||
if (string.IsNullOrEmpty(value)) | ||
return new List<string>(); | ||
private static List<string> CommaSeparatedToList(string value) | ||
{ | ||
if (string.IsNullOrEmpty(value)) | ||
return new List<string>(); | ||
|
||
return value.Split(",", StringSplitOptions.RemoveEmptyEntries) | ||
.Select(x => x.Trim()) | ||
.ToList(); | ||
} | ||
return value.Split(",", StringSplitOptions.RemoveEmptyEntries) | ||
.Select(x => x.Trim()) | ||
.ToList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,63 @@ | ||
using LLama.Web.Common; | ||
using LLama.Web.Common; | ||
using LLama.Web.Models; | ||
using LLama.Web.Services; | ||
using Microsoft.AspNetCore.SignalR; | ||
|
||
namespace LLama.Web.Hubs | ||
{ | ||
public class SessionConnectionHub : Hub<ISessionClient> | ||
{ | ||
private readonly ILogger<SessionConnectionHub> _logger; | ||
private readonly IModelSessionService _modelSessionService; | ||
namespace LLama.Web.Hubs; | ||
|
||
public SessionConnectionHub(ILogger<SessionConnectionHub> logger, IModelSessionService modelSessionService) | ||
{ | ||
_logger = logger; | ||
_modelSessionService = modelSessionService; | ||
} | ||
public class SessionConnectionHub : Hub<ISessionClient> | ||
{ | ||
private readonly ILogger<SessionConnectionHub> _logger; | ||
private readonly IModelSessionService _modelSessionService; | ||
|
||
public override async Task OnConnectedAsync() | ||
{ | ||
_logger.Log(LogLevel.Information, "[OnConnectedAsync], Id: {0}", Context.ConnectionId); | ||
public SessionConnectionHub(ILogger<SessionConnectionHub> logger, IModelSessionService modelSessionService) | ||
{ | ||
_logger = logger; | ||
_modelSessionService = modelSessionService; | ||
} | ||
|
||
// Notify client of successful connection | ||
await Clients.Caller.OnStatus(Context.ConnectionId, SessionConnectionStatus.Connected); | ||
await base.OnConnectedAsync(); | ||
} | ||
public override async Task OnConnectedAsync() | ||
{ | ||
_logger.Log(LogLevel.Information, "[OnConnectedAsync], Id: {0}", Context.ConnectionId); | ||
|
||
// Notify client of successful connection | ||
await Clients.Caller.OnStatus(Context.ConnectionId, SessionConnectionStatus.Connected); | ||
await base.OnConnectedAsync(); | ||
} | ||
|
||
public override async Task OnDisconnectedAsync(Exception exception) | ||
{ | ||
_logger.Log(LogLevel.Information, "[OnDisconnectedAsync], Id: {0}", Context.ConnectionId); | ||
public override async Task OnDisconnectedAsync(Exception exception) | ||
{ | ||
_logger.Log(LogLevel.Information, "[OnDisconnectedAsync], Id: {0}", Context.ConnectionId); | ||
|
||
// Remove connections session on disconnect | ||
await _modelSessionService.CloseAsync(Context.ConnectionId); | ||
await base.OnDisconnectedAsync(exception); | ||
} | ||
// Remove connections session on disconnect | ||
await _modelSessionService.CloseAsync(Context.ConnectionId); | ||
await base.OnDisconnectedAsync(exception); | ||
} | ||
|
||
[HubMethodName("LoadModel")] | ||
public async Task OnLoadModel(SessionConfig sessionConfig, InferenceOptions inferenceConfig) | ||
{ | ||
_logger.Log(LogLevel.Information, "[OnLoadModel] - Load new model, Connection: {0}", Context.ConnectionId); | ||
await _modelSessionService.CloseAsync(Context.ConnectionId); | ||
|
||
[HubMethodName("LoadModel")] | ||
public async Task OnLoadModel(SessionConfig sessionConfig, InferenceOptions inferenceConfig) | ||
// Create model session | ||
var modelSession = await _modelSessionService.CreateAsync(Context.ConnectionId, sessionConfig, inferenceConfig); | ||
if (modelSession is null) | ||
{ | ||
_logger.Log(LogLevel.Information, "[OnLoadModel] - Load new model, Connection: {0}", Context.ConnectionId); | ||
await _modelSessionService.CloseAsync(Context.ConnectionId); | ||
|
||
// Create model session | ||
var modelSession = await _modelSessionService.CreateAsync(Context.ConnectionId, sessionConfig, inferenceConfig); | ||
if (modelSession is null) | ||
{ | ||
await Clients.Caller.OnError("Failed to create model session"); | ||
return; | ||
} | ||
|
||
// Notify client | ||
await Clients.Caller.OnStatus(Context.ConnectionId, SessionConnectionStatus.Loaded); | ||
await Clients.Caller.OnError("Failed to create model session"); | ||
return; | ||
} | ||
|
||
// Notify client | ||
await Clients.Caller.OnStatus(Context.ConnectionId, SessionConnectionStatus.Loaded); | ||
} | ||
|
||
[HubMethodName("SendPrompt")] | ||
public IAsyncEnumerable<TokenModel> OnSendPrompt(string prompt, InferenceOptions inferConfig, CancellationToken cancellationToken) | ||
{ | ||
_logger.Log(LogLevel.Information, "[OnSendPrompt] - New prompt received, Connection: {0}", Context.ConnectionId); | ||
[HubMethodName("SendPrompt")] | ||
public IAsyncEnumerable<TokenModel> OnSendPrompt(string prompt, InferenceOptions inferConfig, CancellationToken cancellationToken) | ||
{ | ||
_logger.Log(LogLevel.Information, "[OnSendPrompt] - New prompt received, Connection: {0}", Context.ConnectionId); | ||
|
||
var linkedCancelationToken = CancellationTokenSource.CreateLinkedTokenSource(Context.ConnectionAborted, cancellationToken); | ||
return _modelSessionService.InferAsync(Context.ConnectionId, prompt, inferConfig, linkedCancelationToken.Token); | ||
} | ||
var linkedCancelationToken = CancellationTokenSource.CreateLinkedTokenSource(Context.ConnectionAborted, cancellationToken); | ||
return _modelSessionService.InferAsync(Context.ConnectionId, prompt, inferConfig, linkedCancelationToken.Token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.