Moved spinner out to an extension, so it can easily be used in other examples
This commit is contained in:
parent
d58fcbbd13
commit
b7379b7124
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue