You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

LLamaSharpChatResult.cs 1.2 kB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using Microsoft.SemanticKernel.AI.ChatCompletion;
  2. using System.Runtime.CompilerServices;
  3. using System.Text;
  4. namespace LLamaSharp.SemanticKernel.ChatCompletion;
  5. internal sealed class LLamaSharpChatResult : IChatStreamingResult
  6. {
  7. private readonly IAsyncEnumerable<string> _stream;
  8. /// <summary>
  9. ///
  10. /// </summary>
  11. /// <param name="stream"></param>
  12. public LLamaSharpChatResult(IAsyncEnumerable<string> stream)
  13. {
  14. _stream = stream;
  15. }
  16. /// <inheritdoc/>
  17. public async Task<ChatMessageBase> GetChatMessageAsync(CancellationToken cancellationToken = default)
  18. {
  19. var sb = new StringBuilder();
  20. await foreach (var token in _stream)
  21. {
  22. sb.Append(token);
  23. }
  24. return await Task.FromResult(new LLamaSharpChatMessage(AuthorRole.Assistant, sb.ToString())).ConfigureAwait(false);
  25. }
  26. /// <inheritdoc/>
  27. public async IAsyncEnumerable<ChatMessageBase> GetStreamingChatMessageAsync([EnumeratorCancellation] CancellationToken cancellationToken = default)
  28. {
  29. await foreach (var token in _stream)
  30. {
  31. yield return new LLamaSharpChatMessage(AuthorRole.Assistant, token);
  32. }
  33. }
  34. }