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
use std::{collections::HashMap, error::Error};

use async_trait::async_trait;
use reqwest::Client;
use scraper::{Html, Selector};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use url::Url;

use crate::tools::Tool;

pub struct DuckDuckGoSearchResults {
    url: String,
    client: Client,
    max_results: usize,
}

impl DuckDuckGoSearchResults {
    pub fn new() -> Self {
        Self {
            client: Client::new(),
            url: "https://duckduckgo.com/html/".to_string(),
            max_results: 4,
        }
    }

    pub fn with_max_results(mut self, max_results: usize) -> Self {
        self.max_results = max_results;
        self
    }

    pub async fn search(&self, query: &str) -> Result<String, Box<dyn Error>> {
        let mut url = Url::parse(&self.url)?;

        let mut query_params = HashMap::new();
        query_params.insert("q", query);

        url.query_pairs_mut().extend_pairs(query_params.iter());

        let response = self.client.get(url).send().await?;
        let body = response.text().await?;
        let document = Html::parse_document(&body);

        let result_selector = Selector::parse(".web-result").unwrap();
        let result_title_selector = Selector::parse(".result__a").unwrap();
        let result_url_selector = Selector::parse(".result__url").unwrap();
        let result_snippet_selector = Selector::parse(".result__snippet").unwrap();

        let results = document
            .select(&result_selector)
            .map(|result| {
                let title = result
                    .select(&result_title_selector)
                    .next()
                    .unwrap()
                    .text()
                    .collect::<Vec<_>>()
                    .join("");
                let link = result
                    .select(&result_url_selector)
                    .next()
                    .unwrap()
                    .text()
                    .collect::<Vec<_>>()
                    .join("")
                    .trim()
                    .to_string();
                let snippet = result
                    .select(&result_snippet_selector)
                    .next()
                    .unwrap()
                    .text()
                    .collect::<Vec<_>>()
                    .join("");

                SearchResult {
                    title,
                    link,
                    snippet,
                }
            })
            .take(self.max_results)
            .collect::<Vec<_>>();

        Ok(serde_json::to_string(&results)?)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchResult {
    title: String,
    link: String,
    snippet: String,
}

#[async_trait]
impl Tool for DuckDuckGoSearchResults {
    fn name(&self) -> String {
        String::from("DuckDuckGoSearch")
    }

    fn description(&self) -> String {
        String::from(
            r#""Wrapper for DuckDuckGo Search API. "
	"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. Output is a JSON array of the query results."#,
        )
    }

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

    fn parameters(&self) -> Value {
        let prompt = r#"A wrapper around DuckDuckGo Search.
            Useful for when you need to answer questions about current events.
            Input should be a search query. Output is a JSON array of the query results."#;

        json!({
            "description": prompt,
            "type": "object",
            "properties": {
                "query": {
                    "type": "string",
                    "description": "Search query to look up"
                }
            },
            "required": ["query"]
        })
    }
}

impl Default for DuckDuckGoSearchResults {
    fn default() -> DuckDuckGoSearchResults {
        DuckDuckGoSearchResults::new()
    }
}

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

    #[tokio::test]
    #[ignore]
    async fn duckduckgosearch_tool() {
        let ddg = DuckDuckGoSearchResults::default().with_max_results(5);
        let s = ddg
            .search("Who is the current President of Peru?")
            .await
            .unwrap();

        println!("{}", s);
    }
}