1use futures_util::StreamExt;
2use gemini_rust::Gemini;
3use std::env;
4
5#[tokio::main]
6async fn main() -> Result<(), Box<dyn std::error::Error>> {
7 let api_key = env::var("GEMINI_API_KEY")
9 .expect("GEMINI_API_KEY environment variable not set");
10
11 let client = Gemini::new(api_key);
13
14 println!("--- Streaming generation ---");
16
17 let mut stream = client
18 .generate_content()
19 .with_system_prompt("You are a helpful, creative assistant.")
20 .with_user_message("Write a short story about a robot who learns to feel emotions.")
21 .execute_stream()
22 .await?;
23
24 print!("Streaming response: ");
25 while let Some(chunk_result) = stream.next().await {
26 match chunk_result {
27 Ok(chunk) => {
28 print!("{}", chunk.text());
29 std::io::Write::flush(&mut std::io::stdout())?;
30 }
31 Err(e) => eprintln!("Error in stream: {}", e),
32 }
33 }
34 println!("\n");
35
36 println!("--- Multi-turn conversation ---");
38
39 let response1 = client
41 .generate_content()
42 .with_system_prompt("You are a helpful travel assistant.")
43 .with_user_message("I'm planning a trip to Japan. What are the best times to visit?")
44 .execute()
45 .await?;
46
47 println!("User: I'm planning a trip to Japan. What are the best times to visit?");
48 println!("Assistant: {}\n", response1.text());
49
50 let response2 = client
52 .generate_content()
53 .with_system_prompt("You are a helpful travel assistant.")
54 .with_user_message("I'm planning a trip to Japan. What are the best times to visit?")
55 .with_model_message(response1.text())
56 .with_user_message("What about cherry blossom season? When exactly does that happen?")
57 .execute()
58 .await?;
59
60 println!("User: What about cherry blossom season? When exactly does that happen?");
61 println!("Assistant: {}\n", response2.text());
62
63 let response3 = client
65 .generate_content()
66 .with_system_prompt("You are a helpful travel assistant.")
67 .with_user_message("I'm planning a trip to Japan. What are the best times to visit?")
68 .with_model_message(response1.text())
69 .with_user_message("What about cherry blossom season? When exactly does that happen?")
70 .with_model_message(response2.text())
71 .with_user_message("What are some must-visit places in Tokyo?")
72 .execute()
73 .await?;
74
75 println!("User: What are some must-visit places in Tokyo?");
76 println!("Assistant: {}", response3.text());
77
78 Ok(())
79}