thinking_basic/
thinking_basic.rs

1use gemini_rust::{Gemini, GenerationConfig, ThinkingConfig};
2use std::env;
3
4#[tokio::main]
5async fn main() -> Result<(), Box<dyn std::error::Error>> {
6    // Get API key from environment variable
7    let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY environment variable not set");
8
9    // Create client
10    let client = Gemini::with_model(api_key, "models/gemini-2.5-pro".to_string());
11
12    println!("=== Gemini 2.5 Thinking Basic Example ===\n");
13
14    // Example 1: Using default dynamic thinking
15    println!(
16        "--- Example 1: Dynamic thinking (model automatically determines thinking budget) ---"
17    );
18    let response1 = client
19        .generate_content()
20        .with_system_prompt("You are a helpful mathematics assistant.")
21        .with_user_message(
22            "Explain Occam's razor principle and provide a simple example from daily life.",
23        )
24        .with_dynamic_thinking()
25        .with_thoughts_included(true)
26        .execute()
27        .await?;
28
29    // Display thinking process
30    let thoughts = response1.thoughts();
31    if !thoughts.is_empty() {
32        println!("Thinking summary:");
33        for (i, thought) in thoughts.iter().enumerate() {
34            println!("Thought {}: {}\n", i + 1, thought);
35        }
36    }
37
38    println!("Answer: {}\n", response1.text());
39
40    // Display token usage
41    if let Some(usage) = &response1.usage_metadata {
42        println!("Token usage:");
43        println!("  Prompt tokens: {}", usage.prompt_token_count);
44        println!(
45            "  Response tokens: {}",
46            usage.candidates_token_count.unwrap_or(0)
47        );
48        if let Some(thinking_tokens) = usage.thoughts_token_count {
49            println!("  Thinking tokens: {}", thinking_tokens);
50        }
51        println!("  Total tokens: {}\n", usage.total_token_count);
52    }
53
54    // Example 2: Set specific thinking budget
55    println!("--- Example 2: Set thinking budget (1024 tokens) ---");
56    let response2 = client
57        .generate_content()
58        .with_system_prompt("You are a helpful programming assistant.")
59        .with_user_message("List 3 main advantages of using the Rust programming language")
60        .with_thinking_budget(1024)
61        .with_thoughts_included(true)
62        .execute()
63        .await?;
64
65    // Display thinking process
66    let thoughts2 = response2.thoughts();
67    if !thoughts2.is_empty() {
68        println!("Thinking summary:");
69        for (i, thought) in thoughts2.iter().enumerate() {
70            println!("Thought {}: {}\n", i + 1, thought);
71        }
72    }
73
74    println!("Answer: {}\n", response2.text());
75
76    // Example 3: Disable thinking feature
77    println!("--- Example 3: Disable thinking feature ---");
78    let response3 = client
79        .generate_content()
80        .with_system_prompt("You are a helpful assistant.")
81        .with_user_message("What is artificial intelligence?")
82        .execute()
83        .await?;
84
85    println!("Answer: {}\n", response3.text());
86
87    // Example 4: Use GenerationConfig to set thinking
88    println!("--- Example 4: Use GenerationConfig to set thinking ---");
89    let thinking_config = ThinkingConfig::new()
90        .with_thinking_budget(2048)
91        .with_thoughts_included(true);
92
93    let generation_config = GenerationConfig {
94        temperature: Some(0.7),
95        max_output_tokens: Some(500),
96        thinking_config: Some(thinking_config),
97        ..Default::default()
98    };
99
100    let response4 = client
101        .generate_content()
102        .with_system_prompt("You are a creative writing assistant.")
103        .with_user_message(
104            "Write the opening of a short story about a robot learning to feel emotions.",
105        )
106        .with_generation_config(generation_config)
107        .execute()
108        .await?;
109
110    // Display thinking process
111    let thoughts4 = response4.thoughts();
112    if !thoughts4.is_empty() {
113        println!("Thinking summary:");
114        for (i, thought) in thoughts4.iter().enumerate() {
115            println!("Thought {}: {}\n", i + 1, thought);
116        }
117    }
118
119    println!("Answer: {}\n", response4.text());
120
121    Ok(())
122}