File size: 5,846 Bytes
f0ae93a
65a76ae
f0ae93a
 
65a76ae
 
f0ae93a
 
 
 
 
65a76ae
f0ae93a
 
65a76ae
f0ae93a
 
 
 
 
 
 
 
 
 
65a76ae
 
f0ae93a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65a76ae
 
 
 
 
 
771c32e
f0ae93a
65a76ae
f0ae93a
 
65a76ae
f0ae93a
 
 
 
 
65a76ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
771c32e
9fde8f2
65a76ae
 
 
 
 
 
f0ae93a
 
65a76ae
 
f0ae93a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65a76ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0ae93a
65a76ae
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
import requests
import json
from bs4 import BeautifulSoup


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

    Perform a search using DuckDuckGo and return the results.



    Args:

        query: The search query string

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



    Returns:

        List of search results

    """
    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(' ', '+')
        # Format the URL with query and parameter to increase snippet size
        url = f"https://html.duckduckgo.com/html/?q={formatted_query}&kl=wt-wt"

        # 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
                # })

                results.append(snippet)

                if len(results) >= count:
                    break

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


def langsearch_search(query: str, count: int = 5) -> list:
    """

    Perform a search using LangSearch API and return the results.



    Args:

        query: The search query string

        count: Maximum number of results to return (default: 5)

        api_key: LangSearch API key (default: None, will look for env variable)



    Returns:

        List of search results

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

    try:
        import os
        # Use API key from parameters or environment variable
        api_key = os.environ.get("LS_TOKEN")

        if not api_key:
            print("Warning: No LangSearch API key provided. Set LS_TOKEN environment variable.")
            return []

        headers = {
            "Content-Type": "application/json",
            "Authorization": f"Bearer {api_key}"
        }

        payload = json.dumps({
            "query": query,
            "freshness": "noLimit",
            "summary": True,
            "count": count
        })

        url = "https://api.langsearch.com/v1/web-search"

        response = requests.post(url, headers=headers, data=payload, timeout=30)
        response.raise_for_status()
        if response.status_code != 200:
            print(f"LangSearch API error: {response.text}")
            return []
        response = response.json()
        results = []
        for result in response["data"]["webPages"]["value"]:
            results.append(result["summary"])
        #print(f"LangSearch results: {results}")
        return results
    except Exception as e:
        print(f"Error during LangSearch search: {e}")
        return []


# Dictionary mapping tool names to their functions
TOOLS_MAPPING = {
    "duckduckgo_search": duckduckgo_search,
    "langsearch_search": langsearch_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"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "langsearch_search",
            "description": "Search the web using LangSearch API for more relevant results with deeper context",
            "parameters": {
                "type": "object",
                "properties": {
                    "query": {
                        "type": "string",
                        "description": "The search query string"
                    },
                    "top_k": {
                        "type": "integer",
                        "description": "Maximum number of results to return",
                        "default": 5
                    },
                    "api_key": {
                        "type": "string",
                        "description": "LangSearch API key (optional, will use LANGSEARCH_API_KEY env var if not provided)"
                    }
                },
                "required": ["query"]
            }
        }
    }
]