use api_gemini::{ client::Client, models::* };
#[ tokio::main ]
async fn main() -> Result< (), Box< dyn core::error::Error > >
{
let client = Client::new()?;
let request = GenerateContentRequest
{
contents: vec!
[
Content
{
parts: vec!
[
Part
{
text: Some( "Hello! Can you explain what artificial intelligence is in simple terms?".to_string() ),
inline_data: None,
function_call: None,
function_response: None,
..Default::default()
}
],
role: "user".to_string(),
}
],
generation_config: Some( GenerationConfig
{
temperature: Some( 0.7 ),
top_k: Some( 40 ),
top_p: Some( 0.95 ),
candidate_count: Some( 1 ),
max_output_tokens: Some( 1024 ),
stop_sequences: None,
}),
safety_settings: Some( vec!
[
SafetySetting
{
category: "HARM_CATEGORY_HARASSMENT".to_string(),
threshold: "BLOCK_MEDIUM_AND_ABOVE".to_string(),
},
SafetySetting
{
category: "HARM_CATEGORY_HATE_SPEECH".to_string(),
threshold: "BLOCK_MEDIUM_AND_ABOVE".to_string(),
}
]),
tools: None,
tool_config: None,
system_instruction: None,
cached_content: None,
};
#[ cfg( feature = "diagnostics_curl" ) ]
{
use api_gemini::diagnostics::AsCurl;
println!( "=== Exact Curl Command Being Executed ===" );
println!( "{}", request.as_curl() );
println!( "=== End Curl Command ===\n" );
}
println!( "=== Request JSON Payload ===" );
println!( "{}", serde_json::to_string_pretty( &request )? );
println!( "=== End JSON Payload ===\n" );
println!( "Sending request to Gemini API..." );
let response = client
.models()
.by_name( "gemini-2.5-flash" )
.generate_content( &request )
.await?;
if let Some( candidate ) = response.candidates.first()
{
if let Some( part ) = candidate.content.parts.first()
{
if let Some( text ) = &part.text
{
println!( "\n=== Gemini Response ===" );
println!( "{text}" );
}
}
if let Some( finish_reason ) = &candidate.finish_reason
{
println!( "\nFinish reason : {finish_reason}" );
}
}
else
{
println!( "No response received from the API." );
}
if let Some( usage ) = &response.usage_metadata
{
println!( "\n=== Token Usage ===" );
if let Some( prompt_tokens ) = usage.prompt_token_count
{
println!( "Prompt tokens : {prompt_tokens}" );
}
if let Some( candidate_tokens ) = usage.candidates_token_count
{
println!( "Response tokens : {candidate_tokens}" );
}
if let Some( total_tokens ) = usage.total_token_count
{
println!( "Total tokens : {total_tokens}" );
}
}
Ok( () )
}