curl_google_search/
curl_google_search.rs

1use gemini_rust::{Content, Gemini, Part, Tool};
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    println!("--- Curl equivalent with Google Search tool ---");
10
11    // This is equivalent to the curl example:
12    // curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GEMINI_API_KEY" \
13    //   -H "Content-Type: application/json" \
14    //   -d '{
15    //       "contents": [
16    //           {
17    //               "parts": [
18    //                   {"text": "What is the current Google stock price?"}
19    //               ]
20    //           }
21    //       ],
22    //       "tools": [
23    //           {
24    //               "google_search": {}
25    //           }
26    //       ]
27    //   }'
28
29    // Create client
30    let client = Gemini::new(api_key);
31
32    // Create a content part that matches the JSON in the curl example
33    let text_part = Part::Text {
34        text: "What is the current Google stock price?".to_string(),
35        thought: None,
36    };
37
38    let content = Content {
39        parts: vec![text_part].into(),
40        role: None,
41    };
42
43    // Create a Google Search tool
44    let google_search_tool = Tool::google_search();
45
46    // Add the content and tool directly to the request
47    // This exactly mirrors the JSON structure in the curl example
48    let mut content_builder = client.generate_content();
49    content_builder.contents.push(content);
50    content_builder = content_builder.with_tool(google_search_tool);
51
52    let response = content_builder.execute().await?;
53
54    println!("Response: {}", response.text());
55
56    Ok(())
57}