mirror of
https://github.com/fiodarsazanavets/aspire-13-examples.git
synced 2026-06-20 12:23:14 +00:00
Add Ollama example
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user