-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy paththinking_basic.rs
More file actions
145 lines (125 loc) · 4.86 KB
/
thinking_basic.rs
File metadata and controls
145 lines (125 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use display_error_chain::DisplayErrorChain;
use gemini_rust::{Gemini, GenerationConfig, ThinkingConfig};
use std::env;
use std::process::ExitCode;
use tracing::info;
#[tokio::main]
async fn main() -> ExitCode {
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::builder()
.with_default_directive(tracing::level_filters::LevelFilter::INFO.into())
.from_env_lossy(),
)
.init();
match do_main().await {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
let error_chain = DisplayErrorChain::new(e.as_ref());
tracing::error!(error.debug = ?e, error.chained = %error_chain, "execution failed");
ExitCode::FAILURE
}
}
}
async fn do_main() -> Result<(), Box<dyn std::error::Error>> {
// Get API key from environment variable
let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY environment variable not set");
// Create client
let client = Gemini::pro(api_key).expect("unable to create Gemini API client");
info!("starting gemini 2.5 thinking basic example");
// Example 1: Using default dynamic thinking
info!("example 1: dynamic thinking (model automatically determines thinking budget)");
let response1 = client
.generate_content()
.with_system_prompt("You are a helpful mathematics assistant.")
.with_user_message(
"Explain Occam's razor principle and provide a simple example from daily life.",
)
.with_dynamic_thinking()
.with_thoughts_included(true)
.execute()
.await?;
// Display thinking process
let thoughts = response1.thoughts();
if !thoughts.is_empty() {
info!("showing thinking summary");
for (i, thought) in thoughts.iter().enumerate() {
info!(thought_number = i + 1, thought = thought, "thought");
}
}
info!(answer = response1.text(), "answer");
// Display token usage
if let Some(usage) = &response1.usage_metadata {
info!("token usage");
if let Some(prompt_tokens) = usage.prompt_token_count {
info!(prompt_tokens = prompt_tokens, "prompt tokens");
}
if let Some(response_tokens) = usage.candidates_token_count {
info!(response_tokens = response_tokens, "response tokens");
}
if let Some(thinking_tokens) = usage.thoughts_token_count {
info!(thinking_tokens = thinking_tokens, "thinking tokens");
}
if let Some(total_tokens) = usage.total_token_count {
info!(total_tokens = total_tokens, "total tokens");
}
}
// Example 2: Set specific thinking budget
info!("example 2: set thinking budget (1024 tokens)");
let response2 = client
.generate_content()
.with_system_prompt("You are a helpful programming assistant.")
.with_user_message("List 3 main advantages of using the Rust programming language")
.with_thinking_budget(1024)
.with_thoughts_included(true)
.execute()
.await?;
// Display thinking process
let thoughts2 = response2.thoughts();
if !thoughts2.is_empty() {
info!("showing thinking summary");
for (i, thought) in thoughts2.iter().enumerate() {
info!(thought_number = i + 1, thought = thought, "thought");
}
}
info!(answer = response2.text(), "answer");
// Example 3: Disable thinking feature
info!("example 3: disable thinking feature");
let response3 = client
.generate_content()
.with_system_prompt("You are a helpful assistant.")
.with_user_message("What is artificial intelligence?")
.execute()
.await?;
info!(answer = response3.text(), "answer");
// Example 4: Use GenerationConfig to set thinking
info!("example 4: use GenerationConfig to set thinking");
let thinking_config = ThinkingConfig::new()
.with_thinking_budget(2048)
.with_thoughts_included(true);
let generation_config = GenerationConfig {
temperature: Some(0.7),
max_output_tokens: Some(500),
thinking_config: Some(thinking_config),
..Default::default()
};
let response4 = client
.generate_content()
.with_system_prompt("You are a creative writing assistant.")
.with_user_message(
"Write the opening of a short story about a robot learning to feel emotions.",
)
.with_generation_config(generation_config)
.execute()
.await?;
// Display thinking process
let thoughts4 = response4.thoughts();
if !thoughts4.is_empty() {
info!("showing thinking summary");
for (i, thought) in thoughts4.iter().enumerate() {
info!(thought_number = i + 1, thought = thought, "thought");
}
}
info!(answer = response4.text(), "answer");
Ok(())
}