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.

LLamaTextResult.cs 1.1 kB

12345678910111213141516171819202122232425262728293031323334353637
  1. using Microsoft.SemanticKernel.AI.TextCompletion;
  2. using Microsoft.SemanticKernel.Orchestration;
  3. using System.Runtime.CompilerServices;
  4. using System.Text;
  5. namespace LLamaSharp.SemanticKernel.TextCompletion;
  6. internal sealed class LLamaTextResult : ITextResult, ITextStreamingResult
  7. {
  8. private readonly IAsyncEnumerable<string> _text;
  9. public LLamaTextResult(IAsyncEnumerable<string> text)
  10. {
  11. _text = text;
  12. ModelResult = new(text);
  13. }
  14. public ModelResult ModelResult { get; }
  15. public async Task<string> GetCompletionAsync(CancellationToken cancellationToken = default)
  16. {
  17. var sb = new StringBuilder();
  18. await foreach (var token in _text)
  19. {
  20. sb.Append(token);
  21. }
  22. return await Task.FromResult(sb.ToString()).ConfigureAwait(false);
  23. }
  24. public async IAsyncEnumerable<string> GetCompletionStreamingAsync([EnumeratorCancellation] CancellationToken cancellationToken = default)
  25. {
  26. await foreach (string word in _text)
  27. {
  28. yield return word;
  29. }
  30. }
  31. }