Files
OnlineShop/AppWithOllamaChatbot/OnlineShop/OnlineShop.Web/ChatHub.cs
T
fiodarsazanavets fd9b663c14 Add Ollama example
2026-01-27 19:16:17 +00:00

60 lines
1.7 KiB
C#

using Microsoft.AspNetCore.SignalR;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using System.Runtime.CompilerServices;
namespace OnlineShop.Web;
public class ChatHub : Hub
{
private readonly Kernel _kernel;
private readonly IChatCompletionService _chat;
private readonly IChatHistoryService _chatHistoryService;
public ChatHub(
Kernel kernel,
IChatCompletionService chat,
IChatHistoryService chatHistoryService)
{
_kernel = kernel;
_chat = chat;
_chatHistoryService = chatHistoryService;
}
public async IAsyncEnumerable<string> StreamAnswer(
string prompt,
[EnumeratorCancellation] CancellationToken cancellationToken)
{
_chatHistoryService.AddUserMessage(Context.ConnectionId, prompt);
var settings = new PromptExecutionSettings
{
};
await foreach (var delta in _chat.GetStreamingChatMessageContentsAsync(
_chatHistoryService.GetChatHistory(
Context.ConnectionId), settings, _kernel, cancellationToken))
{
if (!string.IsNullOrEmpty(delta.Content))
{
yield return delta.Content;
}
}
}
public override async Task OnConnectedAsync()
{
Console.WriteLine($"[ChatHub] Connected: {Context.ConnectionId}");
await base.OnConnectedAsync();
}
public override Task OnDisconnectedAsync(Exception? exception)
{
Console.WriteLine($"[ChatHub] Disconnected: {Context.ConnectionId} {exception?.Message}");
_chatHistoryService.RemoveHistory(Context.ConnectionId);
return base.OnDisconnectedAsync(exception);
}
}