Skip to main content

use_google_tools/
use_google_tools.rs

1use anyhow::Result;
2use schemars::JsonSchema;
3use serde::Deserialize;
4use serde::Serialize;
5
6use allms::{
7    llm::{
8        tools::{GeminiCodeInterpreterConfig, GeminiWebSearchConfig, LLMTools},
9        GoogleModels,
10    },
11    Completions, ThinkingLevel,
12};
13
14mod utils;
15use utils::get_vertex_token;
16
17// Example 1: Web search
18#[derive(Deserialize, Serialize, JsonSchema, Debug, Clone)]
19struct AINewsArticles {
20    pub articles: Vec<AINewsArticle>,
21}
22
23#[derive(Deserialize, Serialize, JsonSchema, Debug, Clone)]
24struct AINewsArticle {
25    pub title: String,
26    pub url: String,
27    pub description: String,
28}
29
30// Example 2: Code interpreter example
31#[derive(Deserialize, Serialize, Debug, Clone, JsonSchema)]
32pub struct CodeInterpreterResponse {
33    pub problem: String,
34    pub code: String,
35    pub solution: String,
36}
37
38#[tokio::main]
39async fn main() -> Result<()> {
40    env_logger::init();
41
42    let google_api_key: String =
43        std::env::var("GOOGLE_AI_STUDIO_API_KEY").expect("GOOGLE_AI_STUDIO_API_KEY not set");
44    let vertex_token = get_vertex_token().await?;
45
46    // Example 1A: Web search example (with Studio API)
47    let web_search_config =
48        GeminiWebSearchConfig::new().add_source("https://www.artificialintelligence-news.com/");
49
50    let web_search_tool = LLMTools::GeminiWebSearch(web_search_config);
51    let google_responses =
52        Completions::new(GoogleModels::Gemini3Flash, &google_api_key, None, None)
53            .add_tool(web_search_tool.clone())
54            .thinking_level(ThinkingLevel::Low);
55
56    match google_responses
57        .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models. 
58        For each news item, provide the title, url, and a short description.")
59        .await
60    {
61        Ok(response) => println!("[AI Studio] AI news articles:\n{:#?}", response),
62        Err(e) => eprintln!("[AI Studio] AI news articles error: {:?}", e),
63    }
64
65    // Example 1B: Web search example (with Vertex API)
66    let google_responses_vertex =
67        Completions::new(GoogleModels::Gemini3_1FlashLite, &vertex_token, None, None)
68            .add_tool(web_search_tool)
69            .version("google-vertex");
70
71    match google_responses_vertex
72        .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models. 
73        For each news item, provide the title, url, and a short description.")
74        .await
75    {
76        Ok(response) => println!("[Vertex] AI news articles:\n{:#?}", response),
77        Err(e) => eprintln!("[Vertex] AI news articles error: {:?}", e),
78    }
79
80    // Example 2A: Code interpreter example (with Studio API)
81    let code_interpreter_tool = LLMTools::GeminiCodeInterpreter(GeminiCodeInterpreterConfig::new());
82    let google_responses =
83        Completions::new(GoogleModels::Gemini3_1Pro, &google_api_key, None, None)
84            .add_tool(code_interpreter_tool.clone());
85
86    match google_responses
87        .get_answer::<CodeInterpreterResponse>(
88            "Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]",
89        )
90        .await
91    {
92        Ok(response) => println!("[AI Studio] Code interpreter response:\n{:#?}", response),
93        Err(e) => eprintln!("[AI Studio] Code interpreter error: {:?}", e),
94    }
95
96    // Example 2B: Code interpreter example (with Vertex API)
97    let google_responses_vertex =
98        Completions::new(GoogleModels::Gemini3_1Pro, &vertex_token, None, None)
99            .add_tool(code_interpreter_tool)
100            .version("google-vertex");
101
102    match google_responses_vertex
103        .get_answer::<CodeInterpreterResponse>(
104            "Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]",
105        )
106        .await
107    {
108        Ok(response) => println!("[Vertex] Code interpreter response:\n{:#?}", response),
109        Err(e) => eprintln!("[Vertex] Code interpreter error: {:?}", e),
110    }
111
112    Ok(())
113}