curl_google_search/curl_google_search.rs
1use gemini_rust::{Gemini, Content, 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("GOOGLE_API_KEY")
8 .expect("GOOGLE_API_KEY environment variable not set");
9
10 println!("--- Curl equivalent with Google Search tool ---");
11
12 // This is equivalent to the curl example:
13 // curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GOOGLE_API_KEY" \
14 // -H "Content-Type: application/json" \
15 // -d '{
16 // "contents": [
17 // {
18 // "parts": [
19 // {"text": "What is the current Google stock price?"}
20 // ]
21 // }
22 // ],
23 // "tools": [
24 // {
25 // "google_search": {}
26 // }
27 // ]
28 // }'
29
30 // Create client
31 let client = Gemini::new(api_key);
32
33 // Create a content part that matches the JSON in the curl example
34 let text_part = Part::Text {
35 text: "What is the current Google stock price?".to_string()
36 };
37
38 let content = Content {
39 parts: vec![text_part],
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}