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