Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rag #8

Merged
merged 67 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
65407bd
Update nugets
edgett Dec 7, 2023
615e103
Memory demo
edgett Dec 8, 2023
29c70d1
Add File Models
edgett Dec 8, 2023
d75f24d
Conversation memory class
edgett Dec 8, 2023
036fb23
Use memory to store files
edgett Dec 8, 2023
889dfce
Syntax
edgett Dec 8, 2023
4b39903
Constitustion free speech demo
edgett Dec 8, 2023
21f3b7b
Memory operating at sufficent speed
edgett Dec 8, 2023
c41eb0c
Attachment CRUD API
edgett Dec 8, 2023
bb2c141
Websocket notify client upon upload ready.
edgett Dec 8, 2023
ce6f56f
Clear warnings
edgett Dec 8, 2023
02b279f
Header responsiveness fix.
edgett Dec 8, 2023
f9f9565
Remove unnecssary while in demo
edgett Dec 8, 2023
284f331
Standardize genreation/emmedding config+injection.
edgett Dec 9, 2023
32adac0
Fix antiprompt load
edgett Dec 9, 2023
4e2802d
WebClient for API. Add file input to ui. Move settings button.
edgett Dec 9, 2023
11f99b3
Placeholder text
edgett Dec 9, 2023
a4cb891
Razor component readability
edgett Dec 9, 2023
866af7a
Fix doc ready function. Add controller medthod to get file binary.
edgett Dec 9, 2023
706accb
Slim attachment ready code.
edgett Dec 9, 2023
7d802ad
Fix delete doc code.
edgett Dec 9, 2023
1e89946
File upload count badge. Continous progress indicatior.
edgett Dec 9, 2023
9dac961
Add attachment manager panel
edgett Dec 9, 2023
90e0771
Rename memory objects
edgett Dec 9, 2023
7ae61af
Fix no upload when num badge clicked.
edgett Dec 9, 2023
3ea7b76
Panel work
edgett Dec 9, 2023
8c7ad9f
Panel placement
edgett Dec 9, 2023
e54c4b1
Panel styling
edgett Dec 10, 2023
4a484fe
file upload style
llamasaresavager Dec 10, 2023
c0015af
Merge branch 'rag' of https://github.com/edgett/PlamHill.BlazorChat i…
llamasaresavager Dec 10, 2023
84264b8
Move file input into new info panel
llamasaresavager Dec 10, 2023
b19fcf9
Fix up component wiring
edgett Dec 10, 2023
2ae9894
Uploader. Add AttachmentId param to api.
edgett Dec 10, 2023
65f3922
compont out attachment display, allowed attachment persist on close open
llamasaresavager Dec 10, 2023
54e92e4
adds delete button, has type issues
llamasaresavager Dec 10, 2023
ffa099f
Fix onfile delete
edgett Dec 10, 2023
cb24c76
Organize Ui files
edgett Dec 10, 2023
4b15b8e
Attachment delete. Websocket notify fix. Async upload fixes. Thread l…
edgett Dec 10, 2023
76b5592
Prevent memory issues by only allowing import or chat.
edgett Dec 10, 2023
2002f1c
Prevent multiple inbound messages.
edgett Dec 10, 2023
387bdfd
Add enable attachment switch.
edgett Dec 10, 2023
1bbecfc
mmmm.
edgett Dec 11, 2023
da927e4
Fix errant perenths
edgett Dec 11, 2023
8ddd227
Re enable regular chat
edgett Dec 11, 2023
364434e
Fix up memory response handling
edgett Dec 11, 2023
2904b2b
Move thread lock to llama pjt.
edgett Dec 11, 2023
acdc442
Refactor chat.
edgett Dec 12, 2023
ac8a52b
More refactor
edgett Dec 12, 2023
636cfad
Refactor LocalStorage and WebSocketChat to service
edgett Dec 12, 2023
1fad339
Cleanup
edgett Dec 12, 2023
eeec5fd
Rename services for consistancy
edgett Dec 12, 2023
935a7a9
Strong type ids guid. Attachment panel
edgett Dec 12, 2023
a62b242
File upload 500 indicator
edgett Dec 12, 2023
ca4a01a
Upload success fix
edgett Dec 12, 2023
b274417
File success indication
edgett Dec 12, 2023
e738739
Ask document function restored.
edgett Dec 12, 2023
a828b2e
Cancel running inference from ui
edgett Dec 12, 2023
81a6d54
Inference cancling all around.
edgett Dec 12, 2023
682f1e6
Document razor
edgett Dec 12, 2023
cbbb508
Document UI Models
edgett Dec 12, 2023
bcbdcc9
Document services
edgett Dec 13, 2023
16958f7
Document services
edgett Dec 13, 2023
180883b
Fix attachment panel on on settings bug.
edgett Dec 13, 2023
f5cd540
ApiClient readme, Client readme
edgett Dec 13, 2023
7752a5d
Readme.
edgett Dec 13, 2023
395e470
Readme.
edgett Dec 13, 2023
88c7f6f
Include sample for retrieval augmented generation.
edgett Dec 13, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Documentation/constitution-rag_test.pdf
Binary file not shown.
24 changes: 24 additions & 0 deletions PalmHill.BlazorChat.ApiClient/BlazorChatApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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)
{
HttpClient = httpClient;
Attachment = RestService.For<IAttachment>(httpClient);
Chat = RestService.For<IChat>(httpClient);
}

public HttpClient HttpClient { get; }
public IAttachment Attachment { get; }
public IChat Chat { get; }
}
}
17 changes: 17 additions & 0 deletions PalmHill.BlazorChat.ApiClient/PalmHill.BlazorChat.ApiClient.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Refit" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\PalmHill.BlazorChat\Shared\PalmHill.BlazorChat.Shared.csproj" />
</ItemGroup>

</Project>
36 changes: 36 additions & 0 deletions PalmHill.BlazorChat.ApiClient/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# PalmHill.BlazorChat.ApiClient

This project is a .NET 8.0 library that provides an API client for the PalmHill Blazor Chat application. It uses the Refit library for HTTP communication.

## Dependencies
- .NET 8.0
- Refit 7.0.0

## Project References
- PalmHill.BlazorChat.Shared

## API Interface
The API client provides an interface to interact with the chat API. The interface is defined in the `IChat` interface.

### Methods
- `Chat(InferenceRequest conversation)`: This method sends a chat message to the server and returns the server's response as a string. The chat message is encapsulated in an `InferenceRequest` object.
- `Ask(InferenceRequest chatConversation)`: This method sends a chat message to the server and returns the server's response as a `ChatMessage` object. The chat message is encapsulated in an `InferenceRequest` object.
- `CancelChat(Guid conversationId)`: This method sends a request to the server to cancel a chat conversation. The ID of the conversation to be cancelled is passed as a parameter.

### BlazorChatApi.cs
The `BlazorChatApi.cs` file contains the `BlazorChatApi` class, which is the main entry point for using the API client. It provides a convenient way to access the interface and its methods.

The `BlazorChatApi` class has a constructor that takes a `HttpClient` object. This `HttpClient` object is used by the Refit library to make HTTP requests.

The `BlazorChatApi` implments the interfaces by using Refit.

## Usage
To use this library, add a reference to it in your project and create an instance of the `IChat` interface using Refit's `RestService` class. Then, you can call the methods defined in the `IChat` interface.

```csharp
var httpClient = new HttpClient { BaseAddress = new Uri("https://api.example.com") };
var blazorChatApi = new BlazorChatApi(httpClient);
var response = await blazorChatApi.Chat.Chat(new InferenceRequest { /* ... */ });
```

Note: This library is not yet available on NuGet.
22 changes: 22 additions & 0 deletions PalmHill.BlazorChat.ApiClient/WebApiInterface/IAttachment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using PalmHill.BlazorChat.Shared.Models;
using Refit;


namespace PalmHill.BlazorChat.ApiClient.WebApiInterface
{
public interface IAttachment
{
[Get("/api/Attachment/list/{conversationId}")]
Task<IEnumerable<AttachmentInfo>> GetAttachments(string conversationId);

[Get("/api/Attachment/{attachmentId}")]
Task<ApiResponse<AttachmentInfo>> GetAttachmentById(string attachmentId);

[Multipart]
[Post("/api/Attachment/{conversationId}/{attachmentId}")]
Task<ApiResponse<AttachmentInfo>> AddAttachment(Guid conversationId, Guid attachmentId, [AliasAs("file")] StreamPart file);

[Delete("/api/Attachment/{attachmentId}")]
Task<ApiResponse<bool>> DeleteAttachment(Guid attachmentId);
}
}
19 changes: 19 additions & 0 deletions PalmHill.BlazorChat.ApiClient/WebApiInterface/IChat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Refit;
using PalmHill.BlazorChat.Shared.Models;


namespace PalmHill.BlazorChat.ApiClient.WebApiInterface
{
public interface IChat
{
[Post("/api/chat")]
Task<ApiResponse<string>> Chat(InferenceRequest conversation);

[Post("/api/chat/docs")]
Task<ApiResponse<ChatMessage>> Ask(InferenceRequest chatConversation);

[Delete("/api/chat/cancel/{conversationId}")]
public Task<ApiResponse<bool>> CancelChat(Guid conversationId);

}
}
12 changes: 12 additions & 0 deletions PalmHill.BlazorChat.sln
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +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
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -38,6 +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
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
@using PalmHill.BlazorChat.ApiClient
@using PalmHill.BlazorChat.Shared.Models
@inject BlazorChatApi BlazorChatApi

@if (Attachment != null)
{
<FluentPresenceBadge Style="width:100%;" Status="_presenceStatus" StatusTitle="@_presenceLabel">
<FluentCard Style="width:100%; padding:3px 3px" >
<div>
@Attachment.Name
</div>
<FluentButton IconStart="@(new Icons.Regular.Size16.Delete())"
Loading="@_deleteInProgress"
OnClick="@_deleteAttachment"
Title="Delete">
</FluentButton>
</FluentCard>
</FluentPresenceBadge>
}


@code {
/// <summary>
/// The <see cref="AttachmentInfo"/> to display
/// </summary>
[Parameter]
public AttachmentInfo? Attachment { get; set; }

/// <summary>
/// The <see cref="EventCallback"/> to invoke when the attachment is deleted.
/// The event handler will remove the attachment from the list.
/// </summary>
[Parameter]
public EventCallback<AttachmentInfo> OnFileDeleted { get; set; }

private bool _deleteInProgress = false;

/// <summary>
/// Deletes the attachment by calling the api.
/// </summary>
/// <returns></returns>
private async Task _deleteAttachment()
{
if(Attachment == null)
{
return;
}

_deleteInProgress = true;
try
{
var apiResponse = await BlazorChatApi.Attachment.DeleteAttachment(Attachment.Id);

if (!apiResponse.IsSuccessStatusCode )
{
throw new Exception($"Failed to delete attachment. {apiResponse.StatusCode} {apiResponse.ReasonPhrase}");
}

if (!apiResponse.Content)
{
throw new Exception($"Failed to delete attachment. {apiResponse.StatusCode} {apiResponse.ReasonPhrase}");
}

if (apiResponse.IsSuccessStatusCode)
{ //Only invoke when the delete was successful.
//The event handler will remove the attachment from the list.
await OnFileDeleted.InvokeAsync(Attachment);
}

}
catch (Exception ex)
{
Console.WriteLine($"{ex}");
}
_deleteInProgress = false;
}

/// <summary>
/// The <see cref="PresenceStatus"/> to display on the attachment.
/// </summary>
private PresenceStatus _presenceStatus
{
get
{
if (Attachment == null)
{
return PresenceStatus.Busy;

}

var presenceStatus = Attachment.Status switch
{
AttachmentStatus.Pending => PresenceStatus.Away,
AttachmentStatus.Uploaded => PresenceStatus.Available,
AttachmentStatus.Failed => PresenceStatus.Busy,
_ => PresenceStatus.Busy
};

return presenceStatus;
}
}

/// <summary>
/// The tooltip text to display for the attachment.
/// </summary>
private string _presenceLabel
{
get
{
if (Attachment == null)
{
return "";

}

var presenceStatus = Attachment.Status switch
{
AttachmentStatus.Pending => "Pending",
AttachmentStatus.Uploaded => "Ready",
AttachmentStatus.Failed => "Failed",
_ => "Failed"
};

return presenceStatus;
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@if (Controller!.UploadedFiles.Any())
{
<div style="padding:3px 3px;">
@foreach (var file in Controller.UploadedFiles)
{
<AttachmentItemDisplay OnFileDeleted="_fileDeletedHandler" Attachment="file"></AttachmentItemDisplay>
}
</div>
}
@code {
[Parameter]
public ChatService? Controller { get; set; }

/// <summary>
/// Removes the deleted attachment from the <see cref="Controller"/>'s UploadedFiles list.
/// </summary>
/// <param name="deletedAttachment">The deleted <see cref="AttachmentInfo"></param>
private void _fileDeletedHandler(AttachmentInfo deletedAttachment)
{
Controller!.UploadedFiles.Remove(deletedAttachment);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
@using Microsoft.AspNetCore.Components.Forms
@using Refit
@inject BlazorChatApi BlazorChatApi;
<InfoPanel IsOpen="Controller!.AttachmentsVisible" Title="Attachments">
<FluentSwitch

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

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

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

@code {

[Parameter]
public ChatService? 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)
{

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

foreach (var file in files)
{
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 (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;
}
}

}
Loading