-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathchat_with_history_stream.rs
50 lines (42 loc) · 1.45 KB
/
chat_with_history_stream.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use ollama_rs::{
generation::chat::{request::ChatMessageRequest, ChatMessage, ChatMessageResponseStream},
Ollama,
};
use tokio::io::{stdout, AsyncWriteExt};
use tokio_stream::StreamExt;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut ollama = Ollama::new_default_with_history(30);
let mut stdout = stdout();
loop {
stdout.write_all(b"\n> ").await?;
stdout.flush().await?;
let mut input = String::new();
std::io::stdin().read_line(&mut input)?;
let input = input.trim_end();
if input.eq_ignore_ascii_case("exit") {
break;
}
let mut stream: ChatMessageResponseStream = ollama
.send_chat_messages_with_history_stream(
ChatMessageRequest::new(
"llama2:latest".to_string(),
vec![ChatMessage::user(input.to_string())],
),
"user".to_string(),
)
.await?;
let mut response = String::new();
while let Some(Ok(res)) = stream.next().await {
if let Some(assistant_message) = res.message {
stdout
.write_all(assistant_message.content.as_bytes())
.await?;
stdout.flush().await?;
response += assistant_message.content.as_str();
}
}
dbg!(&ollama.get_messages_history("user"));
}
Ok(())
}