curl_equivalent/
curl_equivalent.rs

1use gemini_rust::{Gemini, Content, Part};
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")
8        .expect("GEMINI_API_KEY environment variable not set");
9
10    // This is equivalent to the curl example:
11    // curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$YOUR_API_KEY" \
12    //   -H 'Content-Type: application/json' \
13    //   -X POST \
14    //   -d '{
15    //     "contents": [
16    //       {
17    //         "parts": [
18    //           {
19    //             "text": "Explain how AI works in a few words"
20    //           }
21    //         ]
22    //       }
23    //     ]
24    //   }'
25
26    // Create client - now using gemini-2.0-flash by default
27    let client = Gemini::new(api_key);
28    
29    // Method 1: Using the high-level API (simplest approach)
30    println!("--- Method 1: Using the high-level API ---");
31    
32    let response = client
33        .generate_content()
34        .with_user_message("Explain how AI works in a few words")
35        .execute()
36        .await?;
37
38    println!("Response: {}", response.text());
39
40    // Method 2: Using Content directly to match the curl example exactly
41    println!("\n--- Method 2: Matching curl example structure exactly ---");
42    
43    // Create a content part that matches the JSON in the curl example
44    let text_part = Part::Text { 
45        text: "Explain how AI works in a few words".to_string() 
46    };
47    
48    let content = Content {
49        parts: vec![text_part],
50        role: None,
51    };
52    
53    // Add the content directly to the request
54    // This exactly mirrors the JSON structure in the curl example
55    let mut content_builder = client.generate_content();
56    content_builder.contents.push(content);
57    let response = content_builder.execute().await?;
58
59    println!("Response: {}", response.text());
60
61    Ok(())
62}