1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use std::error::Error;

use async_trait::async_trait;
use serde_json::Value;

use crate::tools::Tool;

pub struct SerpApi {
    api_key: String,
    location: Option<String>,
    hl: Option<String>,
    gl: Option<String>,
    google_domain: Option<String>,
}

impl SerpApi {
    pub fn new(api_key: String) -> Self {
        Self {
            api_key,
            location: None,
            hl: None,
            gl: None,
            google_domain: None,
        }
    }
    pub fn with_location<S: Into<String>>(mut self, location: S) -> Self {
        self.location = Some(location.into());
        self
    }
    pub fn with_hl<S: Into<String>>(mut self, hl: S) -> Self {
        self.hl = Some(hl.into());
        self
    }
    pub fn with_gl(mut self, gl: String) -> Self {
        self.gl = Some(gl);
        self
    }
    pub fn with_google_domain<S: Into<String>>(mut self, google_domain: S) -> Self {
        self.google_domain = Some(google_domain.into());
        self
    }

    pub fn with_api_key<S: Into<String>>(mut self, api_key: S) -> Self {
        self.api_key = api_key.into();
        self
    }

    pub async fn simple_search(&self, query: &str) -> Result<String, Box<dyn Error>> {
        let mut url = format!(
            "https://serpapi.com/search.json?q={}&api_key={}",
            query, self.api_key
        );
        if let Some(location) = &self.location {
            url.push_str(&format!("&location={}", location));
        }
        if let Some(hl) = &self.hl {
            url.push_str(&format!("&hl={}", hl));
        }
        if let Some(gl) = &self.gl {
            url.push_str(&format!("&gl={}", gl));
        }
        if let Some(google_domain) = &self.google_domain {
            url.push_str(&format!("&google_domain={}", google_domain));
        }
        let results: Value = reqwest::get(&url).await?.json().await?;

        let res = process_response(&results)?;

        Ok(res)
    }
}

fn get_answer_box(result: &Value) -> String {
    if let Some(map) = result["answer_box"].as_object() {
        if let Some(answer) = map.get("answer").and_then(|v| v.as_str()) {
            return answer.to_string();
        }

        if let Some(snippet) = map.get("snippet").and_then(|v| v.as_str()) {
            return snippet.to_string();
        }

        if let Some(snippet) = map
            .get("snippet_highlighted_words")
            .and_then(|v| v.as_array())
        {
            if !snippet.is_empty() {
                if let Some(first) = snippet.first().and_then(|v| v.as_str()) {
                    return first.to_string();
                }
            }
        }
    }

    "".to_string()
}

fn process_response(res: &Value) -> Result<String, Box<dyn Error>> {
    if !get_answer_box(res).is_empty() {
        return Ok(get_answer_box(res));
    }
    if !get_sport_result(res).is_empty() {
        return Ok(get_sport_result(res));
    }
    if !get_knowledge_graph(res).is_empty() {
        return Ok(get_knowledge_graph(res));
    }
    if !get_organic_result(res).is_empty() {
        return Ok(get_organic_result(res));
    }
    Err("No good result".into())
}

fn get_sport_result(result: &Value) -> String {
    if let Some(map) = result["sports_results"].as_object() {
        if let Some(game_spotlight) = map.get("game_spotlight").and_then(|v| v.as_str()) {
            return game_spotlight.to_string();
        }
    }

    "".to_string()
}

fn get_knowledge_graph(result: &Value) -> String {
    if let Some(map) = result["knowledge_graph"].as_object() {
        if let Some(description) = map.get("description").and_then(|v| v.as_str()) {
            return description.to_string();
        }
    }

    "".to_string()
}

fn get_organic_result(result: &Value) -> String {
    if let Some(array) = result["organic_results"].as_array() {
        if !array.is_empty() {
            if let Some(first) = array.first() {
                if let Some(first_map) = first.as_object() {
                    if let Some(snippet) = first_map.get("snippet").and_then(|v| v.as_str()) {
                        return snippet.to_string();
                    }
                }
            }
        }
    }

    "".to_string()
}

#[async_trait]
impl Tool for SerpApi {
    fn name(&self) -> String {
        String::from("GoogleSearch")
    }
    fn description(&self) -> String {
        String::from(
            r#""A wrapper around Google Search. "
	"Useful for when you need to answer questions about current events. "
	"Always one of the first options when you need to find information on internet"
	"Input should be a search query."#,
        )
    }

    async fn run(&self, input: Value) -> Result<String, Box<dyn Error>> {
        let input = input.as_str().ok_or("Input should be a string")?;
        self.simple_search(input).await
    }
}

impl Default for SerpApi {
    fn default() -> SerpApi {
        SerpApi {
            api_key: std::env::var("SERPAPI_API_KEY").unwrap_or_default(),
            location: None,
            hl: None,
            gl: None,
            google_domain: None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::SerpApi;

    #[tokio::test]
    #[ignore]
    async fn serpapi_tool() {
        let serpapi = SerpApi::default();
        let s = serpapi
            .simple_search("Who is the President of Peru")
            .await
            .unwrap();
        println!("{}", s);
    }
}