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(api_key, "gemini-2.5-flash", None);
14
15    println!("--- Basic Chat Example ---");
16
17    // 3. Ask a question
18    let prompt = "What are the benefits of using Rust for systems programming?";
19    println!("User: {}", prompt);
20
21    let response = ai.ask(session.ask_string(prompt)).await.unwrap();
22
23    // 4. Print the reply
24    // get_text_no_think("") extracts text and ignores "thought" parts (if any)
25    let reply = response.get_chat().get_text_no_think("");
26    println!("\nGemini: {}", reply);
27
28    // 5. The session now contains the interaction
29    println!("\nMessages in history: {}", session.get_history_length());
30}