Skip to content

Commit b14fc7c

Browse files
authored
Merge pull request #1037 from phil-scott-78/fork-example
Replace the progress bar with a live render of the tree
2 parents fae7169 + ea44712 commit b14fc7c

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

LLama.Examples/Examples/BatchedExecutorFork.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,12 @@ public static async Task Run()
4242

4343
// Create the root node of the tree
4444
var root = new Node(start);
45-
45+
var display = new Tree(prompt);
4646
await AnsiConsole
47-
.Progress()
48-
.StartAsync(async progress =>
47+
.Live(display)
48+
.StartAsync(async ctx =>
4949
{
50-
var reporter = progress.AddTask("Running Inference (1)", maxValue: TokenCount);
51-
50+
5251
// Run inference loop
5352
for (var i = 0; i < TokenCount; i++)
5453
{
@@ -62,15 +61,11 @@ await AnsiConsole
6261
// Sample all active conversations
6362
root.Sample();
6463

65-
// Update progress bar
66-
reporter.Increment(1);
67-
reporter.Description($"Running Inference ({root.ActiveConversationCount})");
64+
display = new Tree(prompt);
65+
root.Display(display);
66+
ctx.UpdateTarget(display);
67+
ctx.Refresh();
6868
}
69-
70-
// Display results
71-
var display = new Tree(prompt);
72-
root.Display(display);
73-
AnsiConsole.Write(display);
7469
});
7570

7671
// Print some stats
@@ -85,6 +80,7 @@ private class Node
8580

8681
private readonly DefaultSamplingPipeline _sampler = new();
8782
private Conversation? _conversation;
83+
private string _message = string.Empty;
8884

8985
private Node? _left;
9086
private Node? _right;
@@ -135,15 +131,16 @@ public void Fork()
135131
}
136132
}
137133

134+
138135
public void Display<T>(T tree, int depth = 0)
139136
where T : IHasTreeNodes
140137
{
141138
var colors = new[] { "red", "green", "blue", "yellow", "white" };
142139
var color = colors[depth % colors.Length];
143140

144-
var message = Markup.Escape(_decoder.Read().ReplaceLineEndings(""));
141+
_message += _decoder.Read().ReplaceLineEndings(". ");
145142

146-
var n = tree.AddNode($"[{color}]{message}[/]");
143+
var n = tree.AddNode($"[{color}]{_message.EscapeMarkup()}[/]");
147144

148145
_left?.Display(n, depth + 1);
149146
_right?.Display(n, depth + 1);

0 commit comments

Comments
 (0)