Skip to content

Commit 4192bea

Browse files
Several updates to web project (#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 <[email protected]> * Async model loading. * Updated images on web README. * Corrected filenames. * Fixed typo and updated images. * Menu tweaks. * Removed unused code. --------- Co-authored-by: Rinne <[email protected]>
1 parent 4657e98 commit 4192bea

File tree

92 files changed

+10771
-64142
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+10771
-64142
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,4 +347,6 @@ site/
347347
/LLama.Unittest/Models/*.gguf
348348

349349
/LLama.Benchmark/Models/*.bin
350-
/LLama.Benchmark/Models/*.gguf
350+
/LLama.Benchmark/Models/*.gguf
351+
352+
**/appsettings.Local.json

Assets/web-ui-dark.png

80.2 KB
Loading

Assets/web-ui-light.png

76.1 KB
Loading

LLama.Web/Common/ISessionConfig.cs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1-
namespace LLama.Web.Common
1+
using System.ComponentModel;
2+
3+
namespace LLama.Web.Common;
4+
5+
public interface ISessionConfig
26
{
3-
public interface ISessionConfig
4-
{
5-
string AntiPrompt { get; set; }
6-
List<string> AntiPrompts { get; set; }
7-
LLamaExecutorType ExecutorType { get; set; }
8-
string Model { get; set; }
9-
string OutputFilter { get; set; }
10-
List<string> OutputFilters { get; set; }
11-
string Prompt { get; set; }
12-
}
7+
string AntiPrompt { get; set; }
8+
9+
[DisplayName("Anti Prompts")]
10+
List<string> AntiPrompts { get; set; }
11+
12+
[DisplayName("Executor Type")]
13+
LLamaExecutorType ExecutorType { get; set; }
14+
15+
string Model { get; set; }
16+
17+
[DisplayName("Output Filter")]
18+
string OutputFilter { get; set; }
19+
20+
List<string> OutputFilters { get; set; }
21+
22+
string Prompt { get; set; }
1323
}

LLama.Web/Common/LLamaOptions.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
namespace LLama.Web.Common
2-
{
3-
public class LLamaOptions
4-
{
5-
public ModelLoadType ModelLoadType { get; set; }
6-
public List<ModelOptions> Models { get; set; }
1+
namespace LLama.Web.Common;
72

8-
public void Initialize()
9-
{
10-
}
11-
}
3+
public class LLamaOptions
4+
{
5+
public ModelLoadType ModelLoadType { get; set; }
6+
public List<ModelOptions> Models { get; set; }
127
}

LLama.Web/Common/SessionConfig.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
namespace LLama.Web.Common
1+
namespace LLama.Web.Common;
2+
3+
public class SessionConfig : ISessionConfig
24
{
3-
public class SessionConfig : ISessionConfig
4-
{
5-
public string Model { get; set; }
6-
public string Prompt { get; set; }
5+
public string Model { get; set; }
6+
public string Prompt { get; set; }
77

8-
public string AntiPrompt { get; set; }
9-
public List<string> AntiPrompts { get; set; }
10-
public string OutputFilter { get; set; }
11-
public List<string> OutputFilters { get; set; }
12-
public LLamaExecutorType ExecutorType { get; set; }
13-
}
8+
public string AntiPrompt { get; set; }
9+
public List<string> AntiPrompts { get; set; }
10+
public string OutputFilter { get; set; }
11+
public List<string> OutputFilters { get; set; }
12+
public LLamaExecutorType ExecutorType { get; set; }
1413
}

LLama.Web/Extensions.cs

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,52 @@
1-
using LLama.Web.Common;
1+
using LLama.Web.Common;
22

3-
namespace LLama.Web
3+
namespace LLama.Web;
4+
5+
public static class Extensions
46
{
5-
public static class Extensions
7+
/// <summary>
8+
/// Combines the AntiPrompts list and AntiPrompt csv
9+
/// </summary>
10+
/// <param name="sessionConfig">The session configuration.</param>
11+
/// <returns>Combined AntiPrompts with duplicates removed</returns>
12+
public static List<string> GetAntiPrompts(this ISessionConfig sessionConfig)
613
{
7-
/// <summary>
8-
/// Combines the AntiPrompts list and AntiPrompt csv
9-
/// </summary>
10-
/// <param name="sessionConfig">The session configuration.</param>
11-
/// <returns>Combined AntiPrompts with duplicates removed</returns>
12-
public static List<string> GetAntiPrompts(this ISessionConfig sessionConfig)
13-
{
14-
return CombineCSV(sessionConfig.AntiPrompts, sessionConfig.AntiPrompt);
15-
}
16-
17-
/// <summary>
18-
/// Combines the OutputFilters list and OutputFilter csv
19-
/// </summary>
20-
/// <param name="sessionConfig">The session configuration.</param>
21-
/// <returns>Combined OutputFilters with duplicates removed</returns>
22-
public static List<string> GetOutputFilters(this ISessionConfig sessionConfig)
23-
{
24-
return CombineCSV(sessionConfig.OutputFilters, sessionConfig.OutputFilter);
25-
}
14+
return CombineCSV(sessionConfig.AntiPrompts, sessionConfig.AntiPrompt);
15+
}
2616

17+
/// <summary>
18+
/// Combines the OutputFilters list and OutputFilter csv
19+
/// </summary>
20+
/// <param name="sessionConfig">The session configuration.</param>
21+
/// <returns>Combined OutputFilters with duplicates removed</returns>
22+
public static List<string> GetOutputFilters(this ISessionConfig sessionConfig)
23+
{
24+
return CombineCSV(sessionConfig.OutputFilters, sessionConfig.OutputFilter);
25+
}
2726

28-
/// <summary>
29-
/// Combines a string list and a csv and removes duplicates
30-
/// </summary>
31-
/// <param name="list">The list.</param>
32-
/// <param name="csv">The CSV.</param>
33-
/// <returns>Combined list with duplicates removed</returns>
34-
private static List<string> CombineCSV(List<string> list, string csv)
35-
{
36-
var results = list is null || list.Count == 0
37-
? CommaSeparatedToList(csv)
38-
: CommaSeparatedToList(csv).Concat(list);
39-
return results
40-
.Distinct()
41-
.ToList();
42-
}
27+
/// <summary>
28+
/// Combines a string list and a csv and removes duplicates
29+
/// </summary>
30+
/// <param name="list">The list.</param>
31+
/// <param name="csv">The CSV.</param>
32+
/// <returns>Combined list with duplicates removed</returns>
33+
private static List<string> CombineCSV(List<string> list, string csv)
34+
{
35+
var results = list is null || list.Count == 0
36+
? CommaSeparatedToList(csv)
37+
: CommaSeparatedToList(csv).Concat(list);
38+
return results
39+
.Distinct()
40+
.ToList();
41+
}
4342

44-
private static List<string> CommaSeparatedToList(string value)
45-
{
46-
if (string.IsNullOrEmpty(value))
47-
return new List<string>();
43+
private static List<string> CommaSeparatedToList(string value)
44+
{
45+
if (string.IsNullOrEmpty(value))
46+
return new List<string>();
4847

49-
return value.Split(",", StringSplitOptions.RemoveEmptyEntries)
50-
.Select(x => x.Trim())
51-
.ToList();
52-
}
48+
return value.Split(",", StringSplitOptions.RemoveEmptyEntries)
49+
.Select(x => x.Trim())
50+
.ToList();
5351
}
5452
}
Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,63 @@
1-
using LLama.Web.Common;
1+
using LLama.Web.Common;
22
using LLama.Web.Models;
33
using LLama.Web.Services;
44
using Microsoft.AspNetCore.SignalR;
55

6-
namespace LLama.Web.Hubs
7-
{
8-
public class SessionConnectionHub : Hub<ISessionClient>
9-
{
10-
private readonly ILogger<SessionConnectionHub> _logger;
11-
private readonly IModelSessionService _modelSessionService;
6+
namespace LLama.Web.Hubs;
127

13-
public SessionConnectionHub(ILogger<SessionConnectionHub> logger, IModelSessionService modelSessionService)
14-
{
15-
_logger = logger;
16-
_modelSessionService = modelSessionService;
17-
}
8+
public class SessionConnectionHub : Hub<ISessionClient>
9+
{
10+
private readonly ILogger<SessionConnectionHub> _logger;
11+
private readonly IModelSessionService _modelSessionService;
1812

19-
public override async Task OnConnectedAsync()
20-
{
21-
_logger.Log(LogLevel.Information, "[OnConnectedAsync], Id: {0}", Context.ConnectionId);
13+
public SessionConnectionHub(ILogger<SessionConnectionHub> logger, IModelSessionService modelSessionService)
14+
{
15+
_logger = logger;
16+
_modelSessionService = modelSessionService;
17+
}
2218

23-
// Notify client of successful connection
24-
await Clients.Caller.OnStatus(Context.ConnectionId, SessionConnectionStatus.Connected);
25-
await base.OnConnectedAsync();
26-
}
19+
public override async Task OnConnectedAsync()
20+
{
21+
_logger.Log(LogLevel.Information, "[OnConnectedAsync], Id: {0}", Context.ConnectionId);
2722

23+
// Notify client of successful connection
24+
await Clients.Caller.OnStatus(Context.ConnectionId, SessionConnectionStatus.Connected);
25+
await base.OnConnectedAsync();
26+
}
2827

29-
public override async Task OnDisconnectedAsync(Exception exception)
30-
{
31-
_logger.Log(LogLevel.Information, "[OnDisconnectedAsync], Id: {0}", Context.ConnectionId);
28+
public override async Task OnDisconnectedAsync(Exception exception)
29+
{
30+
_logger.Log(LogLevel.Information, "[OnDisconnectedAsync], Id: {0}", Context.ConnectionId);
3231

33-
// Remove connections session on disconnect
34-
await _modelSessionService.CloseAsync(Context.ConnectionId);
35-
await base.OnDisconnectedAsync(exception);
36-
}
32+
// Remove connections session on disconnect
33+
await _modelSessionService.CloseAsync(Context.ConnectionId);
34+
await base.OnDisconnectedAsync(exception);
35+
}
3736

37+
[HubMethodName("LoadModel")]
38+
public async Task OnLoadModel(SessionConfig sessionConfig, InferenceOptions inferenceConfig)
39+
{
40+
_logger.Log(LogLevel.Information, "[OnLoadModel] - Load new model, Connection: {0}", Context.ConnectionId);
41+
await _modelSessionService.CloseAsync(Context.ConnectionId);
3842

39-
[HubMethodName("LoadModel")]
40-
public async Task OnLoadModel(SessionConfig sessionConfig, InferenceOptions inferenceConfig)
43+
// Create model session
44+
var modelSession = await _modelSessionService.CreateAsync(Context.ConnectionId, sessionConfig, inferenceConfig);
45+
if (modelSession is null)
4146
{
42-
_logger.Log(LogLevel.Information, "[OnLoadModel] - Load new model, Connection: {0}", Context.ConnectionId);
43-
await _modelSessionService.CloseAsync(Context.ConnectionId);
44-
45-
// Create model session
46-
var modelSession = await _modelSessionService.CreateAsync(Context.ConnectionId, sessionConfig, inferenceConfig);
47-
if (modelSession is null)
48-
{
49-
await Clients.Caller.OnError("Failed to create model session");
50-
return;
51-
}
52-
53-
// Notify client
54-
await Clients.Caller.OnStatus(Context.ConnectionId, SessionConnectionStatus.Loaded);
47+
await Clients.Caller.OnError("Failed to create model session");
48+
return;
5549
}
5650

51+
// Notify client
52+
await Clients.Caller.OnStatus(Context.ConnectionId, SessionConnectionStatus.Loaded);
53+
}
5754

58-
[HubMethodName("SendPrompt")]
59-
public IAsyncEnumerable<TokenModel> OnSendPrompt(string prompt, InferenceOptions inferConfig, CancellationToken cancellationToken)
60-
{
61-
_logger.Log(LogLevel.Information, "[OnSendPrompt] - New prompt received, Connection: {0}", Context.ConnectionId);
55+
[HubMethodName("SendPrompt")]
56+
public IAsyncEnumerable<TokenModel> OnSendPrompt(string prompt, InferenceOptions inferConfig, CancellationToken cancellationToken)
57+
{
58+
_logger.Log(LogLevel.Information, "[OnSendPrompt] - New prompt received, Connection: {0}", Context.ConnectionId);
6259

63-
var linkedCancelationToken = CancellationTokenSource.CreateLinkedTokenSource(Context.ConnectionAborted, cancellationToken);
64-
return _modelSessionService.InferAsync(Context.ConnectionId, prompt, inferConfig, linkedCancelationToken.Token);
65-
}
60+
var linkedCancelationToken = CancellationTokenSource.CreateLinkedTokenSource(Context.ConnectionAborted, cancellationToken);
61+
return _modelSessionService.InferAsync(Context.ConnectionId, prompt, inferConfig, linkedCancelationToken.Token);
6662
}
6763
}

LLama.Web/LLama.Web.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22
<Import Project="..\LLama\LLamaSharp.Runtime.targets" />
33
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
4+
<TargetFramework>net8.0</TargetFramework>
55
<Nullable>disable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
</PropertyGroup>
@@ -15,6 +15,7 @@
1515
</ItemGroup>
1616

1717
<ItemGroup>
18+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="7.0.18" />
1819
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
1920
</ItemGroup>
2021

LLama.Web/Models/CancelModel.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)