Skip to main content

basic_chat/
basic_chat.rs

1use gemini_client_api::gemini::ask::Gemini;
2use gemini_client_api::gemini::types::sessions::Session;
3use std::env;
4
5#[tokio::main]
6async fn main() {
7    // 1. Initialize the session with a history limit (e.g., 6 messages)
8    let mut session = Session::new(6);
9
10    // 2. Create the Gemini client
11    // Get your API key from https://aistudio.google.com/app/apikey
12    let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY must be set");
13    let ai = Gemini::new(
14        api_key,
15        "gemini-2.5-flash",
16        Some("You are a senior engineer at google".into()),
17    );
18
19    // 3. Ask a question
20    let prompt = "What are the benefits of using Rust for systems programming?";
21    session.ask(prompt);
22    session.ask("\nKeep you answer short");
23
24    println!("User: {:?}", session.get_last_chat().unwrap().parts());
25    let response = ai.ask(&mut session).await.unwrap();
26
27    // 4. Print the reply
28    // get_text_no_think("") extracts text and ignores "thought" parts (if any)
29    let reply = response.get_chat().get_text_no_think("");
30    println!("\nGemini: {}", reply);
31
32    // 5. The session now contains the interaction
33    println!("\nMessages in history: {}", session.get_history_length());
34}