using Microsoft.SemanticKernel.AI.TextCompletion; using Microsoft.SemanticKernel.Orchestration; using System.Runtime.CompilerServices; using System.Text; namespace LLamaSharp.SemanticKernel.TextCompletion; internal sealed class LLamaTextResult : ITextStreamingResult { private readonly IAsyncEnumerable _text; public LLamaTextResult(IAsyncEnumerable text) { _text = text; ModelResult = new(text); } public ModelResult ModelResult { get; } public async Task GetCompletionAsync(CancellationToken cancellationToken = default) { var sb = new StringBuilder(); await foreach (var token in _text) { sb.Append(token); } return await Task.FromResult(sb.ToString()).ConfigureAwait(false); } public async IAsyncEnumerable GetCompletionStreamingAsync([EnumeratorCancellation] CancellationToken cancellationToken = default) { await foreach (string word in _text) { yield return word; } } }