Moved spinner out to an extension, so it can easily be used in other examples

This commit is contained in:
Martin Evans 2023-09-24 19:24:52 +01:00
parent d58fcbbd13
commit b7379b7124
2 changed files with 45 additions and 39 deletions

View File

@ -0,0 +1,43 @@
namespace LLama.Examples.Extensions
{
public static class IAsyncEnumerableExtensions
{
/// <summary>
/// Show a console spinner while waiting for the next result
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static async IAsyncEnumerable<string> Spinner(this IAsyncEnumerable<string> source)
{
var enumerator = source.GetAsyncEnumerator();
var characters = new[] { '|', '/', '-', '\\' };
while (true)
{
var next = enumerator.MoveNextAsync();
var (Left, Top) = Console.GetCursorPosition();
// Keep showing the next spinner character while waiting for "MoveNextAsync" to finish
var count = 0;
while (!next.IsCompleted)
{
count = (count + 1) % characters.Length;
Console.SetCursorPosition(Left, Top);
Console.Write(characters[count]);
await Task.Delay(75);
}
// Clear the spinner character
Console.SetCursorPosition(Left, Top);
Console.Write(" ");
Console.SetCursorPosition(Left, Top);
if (!next.Result)
break;
yield return enumerator.Current;
}
}
}
}

View File

@ -1,4 +1,5 @@
using LLama.Common;
using LLama.Examples.Extensions;
namespace LLama.Examples.NewVersion
{
@ -35,49 +36,11 @@ namespace LLama.Examples.NewVersion
Console.ForegroundColor = ConsoleColor.White;
Console.Write("Answer: ");
prompt = $"Question: {prompt?.Trim()} Answer: ";
await foreach (var text in Spinner(ex.InferAsync(prompt, inferenceParams)))
await foreach (var text in ex.InferAsync(prompt, inferenceParams).Spinner())
{
Console.Write(text);
}
}
}
/// <summary>
/// Show a spinner while waiting for the next result
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
private static async IAsyncEnumerable<string> Spinner(IAsyncEnumerable<string> source)
{
var enumerator = source.GetAsyncEnumerator();
var characters = new[] { '|', '/', '-', '\\' };
while (true)
{
var next = enumerator.MoveNextAsync();
var (Left, Top) = Console.GetCursorPosition();
// Keep showing the next spinner character while waiting for "MoveNextAsync" to finish
var count = 0;
while (!next.IsCompleted)
{
count = (count + 1) % characters.Length;
Console.SetCursorPosition(Left, Top);
Console.Write(characters[count]);
await Task.Delay(75);
}
// Clear the spinner character
Console.SetCursorPosition(Left, Top);
Console.Write(" ");
Console.SetCursorPosition(Left, Top);
if (!next.Result)
break;
yield return enumerator.Current;
}
}
}
}