File size: 3,032 Bytes
f0ae93a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
from bs4 import BeautifulSoup

def duckduckgo_search(query: str, num_results: int = 3) -> list:
    """

    Perform a search using DuckDuckGo and return the results.



    Args:

        query: The search query string

        num_results: Maximum number of results to return (default: 3)



    Returns:

        List of dictionaries containing search results with title, url, and snippet

    """
    print(f"Performing DuckDuckGo search for: {query}")

    try:
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
        }

        # Format the query for the URL
        formatted_query = query.replace(' ', '+')
        url = f"https://html.duckduckgo.com/html/?q={formatted_query}"

        # Send the request
        response = requests.get(url, headers=headers, timeout=10)
        response.raise_for_status()

        # Parse the HTML response
        soup = BeautifulSoup(response.text, 'html.parser')

        # Extract search results
        results = []
        for result in soup.select('.result'):
            title_elem = result.select_one('.result__title')
            link_elem = result.select_one('.result__url')
            snippet_elem = result.select_one('.result__snippet')

            if title_elem and link_elem:
                title = title_elem.get_text(strip=True)
                url = link_elem.get('href') if link_elem.get('href') else link_elem.get_text(strip=True)
                snippet = snippet_elem.get_text(strip=True) if snippet_elem else ""

                results.append({
                    "title": title,
                    "url": url,
                    "snippet": snippet
                })

                if len(results) >= num_results:
                    break

        print(f"Found {len(results)} results for query: {query}")
        return results
    except Exception as e:
        print(f"Error during DuckDuckGo search: {e}")
        return []

# Dictionary mapping tool names to their functions
TOOLS_MAPPING = {
    "duckduckgo_search": duckduckgo_search
}

# Tool definitions for LLM API
TOOLS_DEFINITION = [
    {
        "type": "function",
        "function": {
            "name": "duckduckgo_search",
            "description": "Search the web using DuckDuckGo and return relevant results",
            "parameters": {
                "type": "object",
                "properties": {
                    "query": {
                        "type": "string",
                        "description": "The search query string"
                    },
                    "num_results": {
                        "type": "integer",
                        "description": "Maximum number of results to return",
                        "default": 5
                    }
                },
                "required": ["query"]
            }
        }
    }
]