Nuriza01 commited on
Commit
516a35b
·
verified ·
1 Parent(s): 2d7f297

Update agent_tools.py

Browse files
Files changed (1) hide show
  1. agent_tools.py +70 -86
agent_tools.py CHANGED
@@ -1,145 +1,129 @@
1
  import requests
2
  import json
3
  from bs4 import BeautifulSoup
 
4
 
5
-
6
- def duckduckgo_search(query: str, count: int = 3) -> list:
7
  """
8
- Perform a search using DuckDuckGo and return the results.
9
 
10
  Args:
11
- query: The search query string
12
- count: Maximum number of results to return (default: 3)
13
 
14
  Returns:
15
- List of search results
16
  """
17
- print(f"Performing DuckDuckGo search for: {query}")
18
 
19
  try:
20
  headers = {
21
- "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"
 
 
 
 
22
  }
23
 
24
- # Format the query for the URL
25
- formatted_query = query.replace(' ', '+')
26
- # Format the URL with query and parameter to increase snippet size
27
- url = f"https://html.duckduckgo.com/html/?q={formatted_query}&kl=wt-wt"
28
 
29
- # Send the request
30
- response = requests.get(url, headers=headers, timeout=10)
31
  response.raise_for_status()
32
 
33
- # Parse the HTML response
34
- soup = BeautifulSoup(response.text, 'html.parser')
35
-
36
- # Extract search results
37
- results = []
38
- for result in soup.select('.result'):
39
- title_elem = result.select_one('.result__title')
40
- link_elem = result.select_one('.result__url')
41
- snippet_elem = result.select_one('.result__snippet')
42
-
43
- if title_elem and link_elem:
44
- title = title_elem.get_text(strip=True)
45
- url = link_elem.get('href') if link_elem.get('href') else link_elem.get_text(strip=True)
46
- snippet = snippet_elem.get_text(strip=True) if snippet_elem else ""
47
 
48
- # results.append({
49
- # "title": title,
50
- # "url": url,
51
- # "snippet": snippet
52
- # })
53
 
54
- results.append(snippet)
 
 
55
 
56
- if len(results) >= count:
57
  break
58
 
59
- print(f"DuckDuckGo results: {results}")
60
- return results
61
- except Exception as e:
62
- print(f"Error during DuckDuckGo search: {e}")
 
63
  return []
64
 
65
 
66
- def langsearch_search(query: str, count: int = 5) -> list:
67
  """
68
- Perform a search using LangSearch API and return the results.
69
 
70
  Args:
71
- query: The search query string
72
- count: Maximum number of results to return (default: 5)
73
- api_key: LangSearch API key (default: None, will look for env variable)
74
 
75
  Returns:
76
- List of search results
77
  """
78
- print(f"Performing LangSearch search for: {query}")
79
 
80
  try:
81
- import os
82
- # Use API key from parameters or environment variable
83
- api_key = os.environ.get("LS_TOKEN")
84
 
85
- if not api_key:
86
- print("Warning: No LangSearch API key provided. Set LS_TOKEN environment variable.")
87
  return []
88
 
89
  headers = {
90
  "Content-Type": "application/json",
91
- "Authorization": f"Bearer {api_key}"
92
  }
93
 
94
- payload = json.dumps({
95
  "query": query,
96
  "freshness": "noLimit",
97
  "summary": True,
98
- "count": count
99
  })
100
 
101
- url = "https://api.langsearch.com/v1/web-search"
 
 
102
 
103
- response = requests.post(url, headers=headers, data=payload, timeout=30)
104
- response.raise_for_status()
105
- if response.status_code != 200:
106
- print(f"LangSearch API error: {response.text}")
107
- return []
108
- response = response.json()
109
- results = []
110
- for result in response["data"]["webPages"]["value"]:
111
- results.append(result["summary"])
112
- #print(f"LangSearch results: {results}")
113
- return results
114
- except Exception as e:
115
- print(f"Error during LangSearch search: {e}")
116
  return []
117
 
118
 
119
- # Dictionary mapping tool names to their functions
120
- TOOLS_MAPPING = {
121
- "duckduckgo_search": duckduckgo_search,
122
- "langsearch_search": langsearch_search
123
  }
124
 
125
- # Tool definitions for LLM API
126
- TOOLS_DEFINITION = [
127
  {
128
  "type": "function",
129
  "function": {
130
- "name": "duckduckgo_search",
131
- "description": "Search the web using DuckDuckGo and return relevant results",
132
  "parameters": {
133
  "type": "object",
134
  "properties": {
135
  "query": {
136
  "type": "string",
137
- "description": "The search query string"
138
  },
139
- "num_results": {
140
  "type": "integer",
141
- "description": "Maximum number of results to return",
142
- "default": 5
143
  }
144
  },
145
  "required": ["query"]
@@ -149,23 +133,23 @@ TOOLS_DEFINITION = [
149
  {
150
  "type": "function",
151
  "function": {
152
- "name": "langsearch_search",
153
- "description": "Search the web using LangSearch API for more relevant results with deeper context",
154
  "parameters": {
155
  "type": "object",
156
  "properties": {
157
  "query": {
158
  "type": "string",
159
- "description": "The search query string"
160
  },
161
- "top_k": {
162
  "type": "integer",
163
- "description": "Maximum number of results to return",
164
  "default": 5
165
  },
166
  "api_key": {
167
  "type": "string",
168
- "description": "LangSearch API key (optional, will use LANGSEARCH_API_KEY env var if not provided)"
169
  }
170
  },
171
  "required": ["query"]
 
1
  import requests
2
  import json
3
  from bs4 import BeautifulSoup
4
+ import os
5
 
6
+ def search_duckduckgo(query: str, max_results: int = 3) -> list:
 
7
  """
8
+ Execute a DuckDuckGo search and retrieve snippets from results.
9
 
10
  Args:
11
+ query (str): The search term to look up.
12
+ max_results (int): Maximum number of result snippets to return. Default is 3.
13
 
14
  Returns:
15
+ list: A list of result snippets (strings).
16
  """
17
+ print(f"Searching DuckDuckGo for: '{query}'")
18
 
19
  try:
20
  headers = {
21
+ "User-Agent": (
22
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
23
+ "AppleWebKit/537.36 (KHTML, like Gecko) "
24
+ "Chrome/91.0.4472.124 Safari/537.36"
25
+ )
26
  }
27
 
28
+ search_term = query.replace(" ", "+")
29
+ search_url = f"https://html.duckduckgo.com/html/?q={search_term}&kl=wt-wt"
 
 
30
 
31
+ response = requests.get(search_url, headers=headers, timeout=10)
 
32
  response.raise_for_status()
33
 
34
+ soup = BeautifulSoup(response.text, "html.parser")
35
+ snippets = []
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
+ for result in soup.select(".result"):
38
+ title = result.select_one(".result__title")
39
+ url = result.select_one(".result__url")
40
+ summary = result.select_one(".result__snippet")
 
41
 
42
+ if title and url:
43
+ snippet_text = summary.get_text(strip=True) if summary else ""
44
+ snippets.append(snippet_text)
45
 
46
+ if len(snippets) >= max_results:
47
  break
48
 
49
+ print(f"DuckDuckGo snippets retrieved: {snippets}")
50
+ return snippets
51
+
52
+ except Exception as error:
53
+ print(f"Failed to fetch DuckDuckGo results: {error}")
54
  return []
55
 
56
 
57
+ def search_langsearch(query: str, max_items: int = 5) -> list:
58
  """
59
+ Use the LangSearch API to perform a web search and return summaries.
60
 
61
  Args:
62
+ query (str): The search phrase.
63
+ max_items (int): Number of results to return. Defaults to 5.
 
64
 
65
  Returns:
66
+ list: A list of summarized search results.
67
  """
68
+ print(f"Using LangSearch API for: '{query}'")
69
 
70
  try:
71
+ token = os.getenv("LS_TOKEN")
 
 
72
 
73
+ if not token:
74
+ print("LangSearch token not found. Set LS_TOKEN environment variable.")
75
  return []
76
 
77
  headers = {
78
  "Content-Type": "application/json",
79
+ "Authorization": f"Bearer {token}"
80
  }
81
 
82
+ body = json.dumps({
83
  "query": query,
84
  "freshness": "noLimit",
85
  "summary": True,
86
+ "count": max_items
87
  })
88
 
89
+ endpoint = "https://api.langsearch.com/v1/web-search"
90
+ res = requests.post(endpoint, headers=headers, data=body, timeout=30)
91
+ res.raise_for_status()
92
 
93
+ data = res.json()
94
+ summaries = [entry["summary"] for entry in data.get("data", {}).get("webPages", {}).get("value", [])]
95
+
96
+ return summaries
97
+
98
+ except Exception as error:
99
+ print(f"Error during LangSearch API call: {error}")
 
 
 
 
 
 
100
  return []
101
 
102
 
103
+ # Map available tools to their corresponding function handlers
104
+ SEARCH_TOOLS = {
105
+ "search_duckduckgo": search_duckduckgo,
106
+ "search_langsearch": search_langsearch
107
  }
108
 
109
+ # Definitions for LLM-compatible function tools
110
+ LLM_TOOL_DEFINITIONS = [
111
  {
112
  "type": "function",
113
  "function": {
114
+ "name": "search_duckduckgo",
115
+ "description": "Query DuckDuckGo to retrieve short summaries of search results.",
116
  "parameters": {
117
  "type": "object",
118
  "properties": {
119
  "query": {
120
  "type": "string",
121
+ "description": "Search term for the web query"
122
  },
123
+ "max_results": {
124
  "type": "integer",
125
+ "description": "Limit of results to return",
126
+ "default": 3
127
  }
128
  },
129
  "required": ["query"]
 
133
  {
134
  "type": "function",
135
  "function": {
136
+ "name": "search_langsearch",
137
+ "description": "Use the LangSearch API to find and summarize relevant web content.",
138
  "parameters": {
139
  "type": "object",
140
  "properties": {
141
  "query": {
142
  "type": "string",
143
+ "description": "Text to search using LangSearch"
144
  },
145
+ "max_items": {
146
  "type": "integer",
147
+ "description": "Number of search results to return",
148
  "default": 5
149
  },
150
  "api_key": {
151
  "type": "string",
152
+ "description": "Optional LangSearch API key (uses LS_TOKEN from environment if not given)"
153
  }
154
  },
155
  "required": ["query"]