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.4 kB

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