Skip to content

Commit e4e27dd

Browse files
committed
refactor: use loop
1 parent 2fe4a06 commit e4e27dd

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

examples/chat_stream_cli.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ async fn main() {
4747

4848
async fn listen_for_tokens(mut chat_stream: Receiver<ChatCompletionDelta>) -> ChatCompletion {
4949
let mut merged: Option<ChatCompletionDelta> = None;
50-
51-
let mut d = true;
52-
while d {
50+
loop {
5351
match chat_stream.try_recv() {
5452
Ok(delta) => {
5553
let choice = &delta.choices[0];
@@ -60,8 +58,7 @@ async fn listen_for_tokens(mut chat_stream: Receiver<ChatCompletionDelta>) -> Ch
6058
print!("{}", content);
6159
}
6260
stdout().flush().unwrap();
63-
64-
// Merge completion into accrued.
61+
// Merge token into full completion.
6562
match merged.as_mut() {
6663
Some(c) => {
6764
c.merge(delta).unwrap();
@@ -70,11 +67,11 @@ async fn listen_for_tokens(mut chat_stream: Receiver<ChatCompletionDelta>) -> Ch
7067
};
7168
}
7269
Err(TryRecvError::Empty) => {
73-
let d = std::time::Duration::from_millis(100);
74-
std::thread::sleep(d);
70+
let duration = std::time::Duration::from_millis(50);
71+
tokio::time::sleep(duration).await;
7572
}
7673
Err(TryRecvError::Disconnected) => {
77-
d = false;
74+
break;
7875
}
7976
};
8077
}

0 commit comments

Comments
 (0)