Skip to main content

chat_completion_streaming/
chat_completion_streaming.rs

1use lm_studio_api_extended::{Chat, Model, Context, Request};
2
3#[tokio::main]
4async fn main() {
5    let mut chat = Chat::new(
6        Model::Kimiko13b,   // Replace model if you are using a different one
7        Context::new("You're Jarvis – my assistant.", 4090),
8    );
9
10    loop {
11        eprint!("\n>> ");
12        let mut buf = String::new();
13        std::io::stdin().read_line(&mut buf).unwrap();
14
15        let request = Request {
16            messages: vec![buf.into()],
17            context: true,
18            stream: true,
19            ..Default::default()
20        };
21
22        let _ = chat.send(request).await.unwrap();
23
24        while let Some(result) = chat.next().await {
25            match result {
26                Ok(text) if !text.is_empty() => eprint!("{text}"),
27                Err(e) => {
28                    eprintln!("Error: {e}");
29                    break;
30                }
31                _ => {}
32            }
33        }
34    }
35}