Skip to content

Commit

Permalink
Semantic kernel swap (#11)
Browse files Browse the repository at this point in the history
* Planning

* Move attachment logic to service layer

* Fix no conversationId

* Remove dependency on LlmMemory in WebsocketChat

* Code cleanup

* Code cleanup

* Update nugets. Add new LlamaKernel project.

* LlamaKernel setup

* Core functionality now through semantic-kernel.

* Update read-me.

* Fix readme.

---------

Co-authored-by: Edgett Hilimire <edgett@palmhillai.com>
  • Loading branch information
edgett and edgett authored Jun 18, 2024
1 parent 4adff1b commit 52f9afc
Show file tree
Hide file tree
Showing 47 changed files with 732 additions and 403 deletions.
11 changes: 3 additions & 8 deletions PalmHill.BlazorChat.ApiClient/BlazorChatApi.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
using PalmHill.BlazorChat.ApiClient.WebApiInterface;
using Refit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PalmHill.BlazorChat.ApiClient
{
public class BlazorChatApi
{
public BlazorChatApi(HttpClient httpClient)
{
public BlazorChatApi(HttpClient httpClient)
{
HttpClient = httpClient;
Attachment = RestService.For<IAttachment>(httpClient);
Attachment = RestService.For<IAttachment>(httpClient);
Chat = RestService.For<IChat>(httpClient);
}

Expand Down
10 changes: 9 additions & 1 deletion PalmHill.BlazorChat.ApiClient/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ var blazorChatApi = new BlazorChatApi(httpClient);
var response = await blazorChatApi.Chat.Chat(new InferenceRequest { /* ... */ });
```

Note: This library is not yet available on NuGet.
Note: This library is not yet available on NuGet.


## To Do
- [ ] Add unit tests
- [ ] Publish to NuGet
- [ ] Add SignalR Chat suppport


4 changes: 2 additions & 2 deletions PalmHill.BlazorChat.ApiClient/WebApiInterface/IChat.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Refit;
using PalmHill.BlazorChat.Shared.Models;
using PalmHill.BlazorChat.Shared.Models;
using Refit;


namespace PalmHill.BlazorChat.ApiClient.WebApiInterface
Expand Down
12 changes: 6 additions & 6 deletions PalmHill.BlazorChat.sln
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PalmHill.BlazorChat.Server"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PalmHill.BlazorChat.Shared", "PalmHill.BlazorChat\Shared\PalmHill.BlazorChat.Shared.csproj", "{F2337C3B-69E8-43F8-8D21-382C233702D0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PalmHill.LlmMemory", "PalmHill.LlmMemory\PalmHill.LlmMemory.csproj", "{A3075AF8-E520-4B62-8D47-564D9C88E52A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PalmHill.BlazorChat.ApiClient", "PalmHill.BlazorChat.ApiClient\PalmHill.BlazorChat.ApiClient.csproj", "{434FEDF0-2AD0-4276-AC06-E26126EEF237}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PalmHill.Llama.Kernel", "PalmHill.LlamaKernel\PalmHill.Llama.Kernel.csproj", "{D92943B2-5585-432B-BFEC-ECC13B5A82D1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -42,14 +42,14 @@ Global
{F2337C3B-69E8-43F8-8D21-382C233702D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F2337C3B-69E8-43F8-8D21-382C233702D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F2337C3B-69E8-43F8-8D21-382C233702D0}.Release|Any CPU.Build.0 = Release|Any CPU
{A3075AF8-E520-4B62-8D47-564D9C88E52A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A3075AF8-E520-4B62-8D47-564D9C88E52A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A3075AF8-E520-4B62-8D47-564D9C88E52A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A3075AF8-E520-4B62-8D47-564D9C88E52A}.Release|Any CPU.Build.0 = Release|Any CPU
{434FEDF0-2AD0-4276-AC06-E26126EEF237}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{434FEDF0-2AD0-4276-AC06-E26126EEF237}.Debug|Any CPU.Build.0 = Debug|Any CPU
{434FEDF0-2AD0-4276-AC06-E26126EEF237}.Release|Any CPU.ActiveCfg = Release|Any CPU
{434FEDF0-2AD0-4276-AC06-E26126EEF237}.Release|Any CPU.Build.0 = Release|Any CPU
{D92943B2-5585-432B-BFEC-ECC13B5A82D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D92943B2-5585-432B-BFEC-ECC13B5A82D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D92943B2-5585-432B-BFEC-ECC13B5A82D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D92943B2-5585-432B-BFEC-ECC13B5A82D1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}
@code {
[Parameter]
public ChatService? Controller { get; set; }
public AttachmentService? Controller { get; set; }

/// <summary>
/// Removes the deleted attachment from the <see cref="Controller"/>'s UploadedFiles list.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,73 +1,39 @@
@using Microsoft.AspNetCore.Components.Forms
@using Refit
@inject BlazorChatApi BlazorChatApi;
<InfoPanel IsOpen="Controller!.AttachmentsVisible" Title="Attachments">



<InfoPanel IsOpen="Controller?.AttachmentsVisible ?? false" Title="Attachments">
<FluentSwitch

@bind-Value="Controller!.AttachmentsEnabled"
@bind-Value="AttachmentsEnabledWrapper"
Label="Enable"></FluentSwitch>

<FileInput Controller="Controller" OnInputFileChange="_uploadFiles">
<FileInput OnInputFileChange="_uploadFiles">
</FileInput>

<AttachmentList Controller="Controller">
</AttachmentList>
</InfoPanel>

@code {

[Parameter]
public ChatService? Controller { get; set; }
public AttachmentService? Controller { get; set; }


/// <summary>
/// Uploads files to the server when the user selects them.
/// Adds to the <see cref="Controller"/>'s UploadedFiles list.
/// </summary>
/// <param name="e">The selected files.</param>
private async Task _uploadFiles(InputFileChangeEventArgs e)
{
await Controller!.UploadAttachments(e);
}

var files = e.GetMultipleFiles();
var uploadedCount = 0;
var uploadTasks = new List<Task>();

foreach (var file in files)
public bool AttachmentsEnabledWrapper
{
get => Controller?.AttachmentsEnabled ?? false; // Return false if AttachmentsEnabled is null
set
{
var attachmentInfo = new AttachmentInfo();
attachmentInfo.ConversationId = Controller?.WebSocketChatConnection?.ConversationId;
attachmentInfo.Name = file.Name;
attachmentInfo.Size = file.Size;
attachmentInfo.ContentType = file.ContentType;
attachmentInfo.Status = AttachmentStatus.Pending;

Controller!.UploadedFiles.Add(attachmentInfo);

var uploadTask = new Task(async () =>
if (Controller != null)
{
if (attachmentInfo?.ConversationId is null)
{
attachmentInfo!.Status = AttachmentStatus.Failed;
return;
}

var stream = file.OpenReadStream(10000000);
var streamPart = new StreamPart(stream, file.Name, file.ContentType);
var apiResponse = await BlazorChatApi.Attachment.AddAttachment(attachmentInfo.ConversationId.Value, attachmentInfo.Id, streamPart);
uploadedCount++;

if (!apiResponse.IsSuccessStatusCode)
{
attachmentInfo.Status = AttachmentStatus.Failed;
}
});

uploadTasks.Add(uploadTask);
}

foreach (var uploadTask in uploadTasks)
{
uploadTask.Start();
await uploadTask;
Controller.AttachmentsEnabled = value;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@

@code
{
[Parameter]
public ChatService? Controller { get; set; }

/// <summary>
/// Event callback to invoke when the user selects files.
/// </summary>
Expand Down
21 changes: 16 additions & 5 deletions PalmHill.BlazorChat/Client/Components/Chat/ChatInput.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

<FluentFooter Class="w-100" Style="position:fixed; bottom:15px;">
<FluentStack Class="chat-messages input-area">
<div class="file-input-area" @onclick="Controller!.ToggleAttachmentsVisible">
<FluentCounterBadge Count="Controller?.UploadedFiles.Count ?? 0"
<div class="file-input-area" @onclick="_toggleAttachmentVisible">
<FluentCounterBadge Count="Controller?.AttachmentService?.UploadedFiles.Count ?? 0"
ShowZero="true"
BottomPosition="-10">
<FluentButton Id="uploadButton"
<FluentButton Id="uploadButton" Disabled="Controller?.AttachmentService is null"
IconStart="(new Icons.Regular.Size24.Attach())"
Style="margin-top:6px;"></FluentButton>
</FluentCounterBadge>
Expand All @@ -33,7 +33,7 @@
@if (Controller?.CanSend == true)
{
<FluentButton OnClick="_sendPrompt"
Disabled="string.IsNullOrWhiteSpace(Controller!.UserInput)"
Disabled="!UserInputValid"
Appearance="Appearance.Accent"
IconStart="(new Icons.Regular.Size24.Send())"
Style="margin-top:6px;"></FluentButton>
Expand Down Expand Up @@ -87,12 +87,14 @@
/// <param name="e">The key pressed.</param>
private async Task _handleKeyPress(KeyboardEventArgs e)
{
if (e.Key == "Enter" && !e.ShiftKey)
if (e.Key == "Enter" && !e.ShiftKey && UserInputValid)
{
await _sendPrompt();
}
}

public bool UserInputValid { get => userInnputValid(); }

/// <summary>
/// Resizes the main text area.
/// </summary>
Expand All @@ -101,6 +103,15 @@
await JSRuntime.InvokeVoidAsync("textAreaAdjust", textAreaElement);
}

private void _toggleAttachmentVisible()
{
Controller?.AttachmentService?.ToggleAttachmentsVisible();
}

private bool userInnputValid()
{
return !string.IsNullOrWhiteSpace(Controller!.UserInput);
}



Expand Down
2 changes: 1 addition & 1 deletion PalmHill.BlazorChat/Client/Models/LocalStorageSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class LocalStorageSettings
/// Parameters for the inference request.
/// </summary>
public InferenceSettings InferenceSettings { get; set; } = new InferenceSettings();

/// <summary>
/// System message for the inference request.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions PalmHill.BlazorChat/Client/Models/ModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class ModelExtensions
/// <param name="localStorageSettings">The object to copy.</param>
/// <returns>A new instance of <see cref="LocalStorageSettings"/> with values from <paramref name="localStorageSettings"/>.</returns>
public static LocalStorageSettings CreateCopy(this LocalStorageSettings localStorageSettings)
{
{
var copy = new LocalStorageSettings();
copy.DarkMode = localStorageSettings.DarkMode;
copy.InferenceSettings = localStorageSettings.InferenceSettings.CreateCopy();
Expand All @@ -25,7 +25,7 @@ public static LocalStorageSettings CreateCopy(this LocalStorageSettings localSto
/// <param name="inferenceSettings">The object top copy.</param>
/// <returns>A new instance of <see cref="InferenceSettings"/> with values from <paramref name="inferenceSettings"/>.</returns>
public static InferenceSettings CreateCopy(this InferenceSettings inferenceSettings)
{
{
var copy = new InferenceSettings();
copy.MaxLength = inferenceSettings.MaxLength;
copy.PresencePenalty = inferenceSettings.PresencePenalty;
Expand Down
2 changes: 1 addition & 1 deletion PalmHill.BlazorChat/Client/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</ChatInput>
</div>

<AttachmentManager Controller="Controller">
<AttachmentManager Controller="Controller?.AttachmentService">
</AttachmentManager>


Expand Down
16 changes: 8 additions & 8 deletions PalmHill.BlazorChat/Client/PalmHill.BlazorChat.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Blazored.LocalStorage" Version="4.4.0" />
<PackageReference Include="Markdig" Version="0.33.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.0" />
<PackageReference Include="Blazored.LocalStorage" Version="4.5.0" />
<PackageReference Include="Markdig" Version="0.37.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.6" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.6" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components" Version="4.1.1" />
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components.Emoji" Version="4.1.0" />
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components.Icons" Version="4.1.0" />
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components" Version="4.8.0" />
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components.Emoji" Version="4.6.0" />
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components.Icons" Version="4.8.0" />
</ItemGroup>

<PropertyGroup>
Expand Down
1 change: 0 additions & 1 deletion PalmHill.BlazorChat/Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.FluentUI.AspNetCore.Components;
using Microsoft.FluentUI.AspNetCore.Components.DesignTokens;
using PalmHill.BlazorChat.ApiClient;
using PalmHill.BlazorChat.Client;
using PalmHill.BlazorChat.Client.Services;
Expand Down
5 changes: 5 additions & 0 deletions PalmHill.BlazorChat/Client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ Here's a key page in the Pages folder:
## Key Services
- `ChatService.cs`: The main service provided by Dependency Injection for operating the UI.
- `WebSocketChatService.cs`: The service used by the ChatService to interact with the server via WebSockets.

## To D
- [ ] Themeing and brading
- [ ] Select docs for Chat
- [ ] Display citations for Chat with docs
Loading

0 comments on commit 52f9afc

Please sign in to comment.