forked from jeremychone/rust-genai
-
Notifications
You must be signed in to change notification settings - Fork 1
/
c01-conv.rs
33 lines (24 loc) · 969 Bytes
/
c01-conv.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
use genai::chat::printer::print_chat_stream;
use genai::chat::{ChatMessage, ChatRequest};
use genai::Client;
const MODEL: &str = "gpt-4o-mini";
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let questions = &[
// follow-up questions
"Why is the sky blue?",
"Why is it red sometime?",
];
let client = Client::default();
let mut chat_req = ChatRequest::default().with_system("Answer in one sentence");
// Similar to putting a first System Chat Message(s) (will be cumulative with system chat messages)
for &question in questions {
chat_req = chat_req.append_message(ChatMessage::user(question));
println!("\n--- Question:\n{question}");
let chat_res = client.exec_chat_stream(MODEL, chat_req.clone(), None).await?;
println!("\n--- Answer: (streaming)");
let assistant_answer = print_chat_stream(chat_res, None).await?;
chat_req = chat_req.append_message(ChatMessage::assistant(assistant_answer));
}
Ok(())
}