Skip to content

Commit

Permalink
Several updates to web project (SciSharp#718)
Browse files Browse the repository at this point in the history
* * 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
Lamothe and AsakusaRinne authored May 15, 2024
1 parent 4657e98 commit 4192bea
Show file tree
Hide file tree
Showing 92 changed files with 10,771 additions and 64,142 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,6 @@ site/
/LLama.Unittest/Models/*.gguf

/LLama.Benchmark/Models/*.bin
/LLama.Benchmark/Models/*.gguf
/LLama.Benchmark/Models/*.gguf

**/appsettings.Local.json
Binary file added Assets/web-ui-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/web-ui-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 21 additions & 11 deletions LLama.Web/Common/ISessionConfig.cs
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; }
}
15 changes: 5 additions & 10 deletions LLama.Web/Common/LLamaOptions.cs
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; }
}
21 changes: 10 additions & 11 deletions LLama.Web/Common/SessionConfig.cs
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; }
}
88 changes: 43 additions & 45 deletions LLama.Web/Extensions.cs
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();
}
}
92 changes: 44 additions & 48 deletions LLama.Web/Hubs/SessionConnectionHub.cs
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);
}
}
3 changes: 2 additions & 1 deletion LLama.Web/LLama.Web.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Import Project="..\LLama\LLamaSharp.Runtime.targets" />
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>disable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand All @@ -15,6 +15,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="7.0.18" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
</ItemGroup>

Expand Down
7 changes: 0 additions & 7 deletions LLama.Web/Models/CancelModel.cs

This file was deleted.

Loading

0 comments on commit 4192bea

Please sign in to comment.