Skip to content

Commit

Permalink
#14 - Fixed an issue where the message did not appear to be streaming
Browse files Browse the repository at this point in the history
Signed-off-by: joshuajpiluden@gmail.com <joshuajpiluden@Gmail.com>
  • Loading branch information
jjosh102 committed Nov 12, 2024
1 parent 67a2e65 commit 1cd1311
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
8 changes: 3 additions & 5 deletions src/Open.Blazor.Core/Features/Chat/Chat.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,11 @@ await ChatService.StreamChatMessageContentAsync(_kernel, _discourse, OnStreamCom
_isChatOngoing = false;
}
}


//See if this can be optimize further
private Task OnStreamCompletion(string updatedContent)

private async Task OnStreamCompletion(string updatedContent)
{
_discourse.ChatMessages.Last().Content += updatedContent;
return Task.CompletedTask;
await ScrollToBottom();
}

private void ResetCancellationTokenSource()
Expand Down
2 changes: 1 addition & 1 deletion src/Open.Blazor.Core/Features/Chat/ChatContent.razor
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
{
await JsRuntime!.InvokeVoidAsync("navigator.clipboard.writeText", Message.Content);

Check warning on line 59 in src/Open.Blazor.Core/Features/Chat/ChatContent.razor

View workflow job for this annotation

GitHub Actions / Build and Deploy Job

Dereference of a possibly null reference.
if (!string.IsNullOrEmpty(Message.Content))
ToastService!.ShowSuccess("Copying to clipboard was successful!");
ToastService!.ShowSuccess("Copied to clipboard.");
else
ToastService!.ShowError("Clipboard's empty");
}
Expand Down
15 changes: 7 additions & 8 deletions src/Open.Blazor.Core/Features/Chat/ChatService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
Expand All @@ -9,11 +8,10 @@ namespace Open.Blazor.Core.Features.Chat;

internal sealed class ChatService
{

private readonly Config _config;

public ChatService(Config config) => _config = config;

public Kernel CreateKernel(string model)
{
ArgumentNullException.ThrowIfNull(model);
Expand Down Expand Up @@ -42,12 +40,13 @@ public async Task StreamChatMessageContentAsync(Kernel kernel,

var executionSettings = chatSettings.ToOpenAIPromptExecutionSettings();
var history = discourse.ToChatHistory();

await foreach (var completionResult in chatCompletion.GetStreamingChatMessageContentsAsync(history,
executionSettings, cancellationToken: cancellationToken))
var chatCompletionStream =
chatCompletion.GetStreamingChatMessageContentsAsync(history, executionSettings,
cancellationToken: cancellationToken);
await foreach (var completionResult in chatCompletionStream)
{
if (cancellationToken.IsCancellationRequested) return;

await onStreamCompletion.Invoke(completionResult.Content ?? string.Empty);
}
}
Expand Down

0 comments on commit 1cd1311

Please sign in to comment.