thinking_curl_equivalent/
thinking_curl_equivalent.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    // This is equivalent to the following curl example:
10    // curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro:generateContent" \
11    //   -H "x-goog-api-key: $GEMINI_API_KEY" \
12    //   -H 'Content-Type: application/json' \
13    //   -X POST \
14    //   -d '{
15    //     "contents": [
16    //       {
17    //         "parts": [
18    //           {
19    //             "text": "Provide a list of the top 3 famous physicists and their major contributions"
20    //           }
21    //         ]
22    //       }
23    //     ],
24    //     "generationConfig": {
25    //       "thinkingConfig": {
26    //         "thinkingBudget": 1024,
27    //         "includeThoughts": true
28    //       }
29    //     }
30    //   }'
31
32    // Create client
33    let client = Gemini::with_model(api_key, "models/gemini-2.5-pro".to_string());
34
35    println!("=== Thinking Curl Equivalent Example ===\n");
36
37    // Method 1: Using high-level API (simplest approach)
38    println!("--- Method 1: Using high-level API ---");
39
40    let response1 = client
41        .generate_content()
42        .with_user_message(
43            "Provide a list of the top 3 famous physicists and their major contributions",
44        )
45        .with_thinking_budget(1024)
46        .with_thoughts_included(true)
47        .execute()
48        .await?;
49
50    // Display thinking process
51    let thoughts1 = response1.thoughts();
52    if !thoughts1.is_empty() {
53        println!("Thinking summary:");
54        for (i, thought) in thoughts1.iter().enumerate() {
55            println!("Thought {}: {}\n", i + 1, thought);
56        }
57    }
58
59    println!("Answer: {}\n", response1.text());
60
61    // Method 2: Using GenerationConfig to fully match curl example structure
62    println!("--- Method 2: Fully matching curl example structure ---");
63
64    let thinking_config = ThinkingConfig {
65        thinking_budget: Some(1024),
66        include_thoughts: Some(true),
67    };
68
69    let generation_config = GenerationConfig {
70        thinking_config: Some(thinking_config),
71        ..Default::default()
72    };
73
74    let response2 = client
75        .generate_content()
76        .with_user_message(
77            "Provide a list of the top 3 famous physicists and their major contributions",
78        )
79        .with_generation_config(generation_config)
80        .execute()
81        .await?;
82
83    // Display thinking process
84    let thoughts2 = response2.thoughts();
85    if !thoughts2.is_empty() {
86        println!("Thinking summary:");
87        for (i, thought) in thoughts2.iter().enumerate() {
88            println!("Thought {}: {}\n", i + 1, thought);
89        }
90    }
91
92    println!("Answer: {}\n", response2.text());
93
94    // Show token usage
95    if let Some(usage) = &response2.usage_metadata {
96        println!("Token usage:");
97        println!("  Prompt tokens: {}", usage.prompt_token_count);
98        println!(
99            "  Response tokens: {}",
100            usage.candidates_token_count.unwrap_or(0)
101        );
102        if let Some(thinking_tokens) = usage.thoughts_token_count {
103            println!("  Thinking tokens: {}", thinking_tokens);
104        }
105        println!("  Total tokens: {}", usage.total_token_count);
106    }
107
108    // Method 3: Demonstrate different thinking budget settings
109    println!("\n--- Method 3: Different thinking budget comparison ---");
110
111    // Thinking disabled
112    println!("Thinking disabled:");
113    let response_no_thinking = client
114        .generate_content()
115        .with_user_message("Explain the basic principles of quantum mechanics")
116        .execute()
117        .await?;
118    println!("Answer: {}\n", response_no_thinking.text());
119
120    // Dynamic thinking
121    println!("Dynamic thinking:");
122    let response_dynamic = client
123        .generate_content()
124        .with_user_message("Explain the basic principles of quantum mechanics")
125        .with_dynamic_thinking()
126        .with_thoughts_included(true)
127        .execute()
128        .await?;
129
130    let thoughts_dynamic = response_dynamic.thoughts();
131    if !thoughts_dynamic.is_empty() {
132        println!("Thinking summary:");
133        for (i, thought) in thoughts_dynamic.iter().enumerate() {
134            println!("Thought {}: {}\n", i + 1, thought);
135        }
136    }
137    println!("Answer: {}\n", response_dynamic.text());
138
139    // High thinking budget
140    println!("High thinking budget (4096 tokens):");
141    let response_high_budget = client
142        .generate_content()
143        .with_user_message("Explain the basic principles of quantum mechanics")
144        .with_thinking_budget(4096)
145        .with_thoughts_included(true)
146        .execute()
147        .await?;
148
149    let thoughts_high = response_high_budget.thoughts();
150    if !thoughts_high.is_empty() {
151        println!("Thinking summary:");
152        for (i, thought) in thoughts_high.iter().enumerate() {
153            println!("Thought {}: {}\n", i + 1, thought);
154        }
155    }
156    println!("Answer: {}", response_high_budget.text());
157
158    Ok(())
159}