using Microsoft.SemanticKernel.AI.ChatCompletion; using Microsoft.SemanticKernel.Orchestration; using System.Runtime.CompilerServices; using System.Text; namespace LLamaSharp.SemanticKernel.ChatCompletion; internal sealed class LLamaSharpChatResult : IChatStreamingResult { private readonly ModelResult _modelResult; private readonly IAsyncEnumerable _stream; /// /// /// /// public LLamaSharpChatResult(IAsyncEnumerable stream) { _stream = stream; this._modelResult = new ModelResult(stream); } public ModelResult ModelResult => this._modelResult; /// public async Task GetChatMessageAsync(CancellationToken cancellationToken = default) { var sb = new StringBuilder(); await foreach (var token in _stream) { sb.Append(token); } return await Task.FromResult(new LLamaSharpChatMessage(AuthorRole.Assistant, sb.ToString())).ConfigureAwait(false); } /// public async IAsyncEnumerable GetStreamingChatMessageAsync([EnumeratorCancellation] CancellationToken cancellationToken = default) { await foreach (var token in _stream) { yield return new LLamaSharpChatMessage(AuthorRole.Assistant, token); } } }