Add Ollama example

This commit is contained in:
fiodarsazanavets
2026-01-27 19:16:17 +00:00
parent d3149e7693
commit fd9b663c14
99 changed files with 63935 additions and 1 deletions
@@ -0,0 +1,59 @@
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);
}
}