Spaces:
Sleeping
Sleeping
Delete custom_tools.py
Browse files- custom_tools.py +0 -322
custom_tools.py
DELETED
@@ -1,322 +0,0 @@
|
|
1 |
-
import requests
|
2 |
-
from duckduckgo_search import DDGS
|
3 |
-
from langchain_core.tools import tool
|
4 |
-
import time
|
5 |
-
import re
|
6 |
-
import json
|
7 |
-
from datetime import datetime, timedelta
|
8 |
-
import urllib.parse
|
9 |
-
|
10 |
-
# Rate limiting
|
11 |
-
last_search_time = None
|
12 |
-
min_search_interval = 1.0
|
13 |
-
|
14 |
-
@tool
|
15 |
-
def reverse_text(input: str) -> str:
|
16 |
-
"""Reverse the characters in a text or string."""
|
17 |
-
return input[::-1]
|
18 |
-
|
19 |
-
@tool
|
20 |
-
def web_search(query: str) -> str:
|
21 |
-
"""Perform web search using multiple providers for robustness."""
|
22 |
-
global last_search_time
|
23 |
-
|
24 |
-
# Rate limiting
|
25 |
-
if last_search_time:
|
26 |
-
elapsed = time.time() - last_search_time
|
27 |
-
if elapsed < min_search_interval:
|
28 |
-
time.sleep(min_search_interval - elapsed)
|
29 |
-
|
30 |
-
query = query.strip()
|
31 |
-
if not query:
|
32 |
-
return "Empty search query"
|
33 |
-
|
34 |
-
results = []
|
35 |
-
|
36 |
-
# Try multiple search methods in order
|
37 |
-
search_methods = [
|
38 |
-
("Wikipedia", search_wikipedia),
|
39 |
-
("Google (via SerpAPI simulation)", search_google_fallback),
|
40 |
-
("DuckDuckGo", search_duckduckgo),
|
41 |
-
("Bing", search_bing_fallback),
|
42 |
-
]
|
43 |
-
|
44 |
-
for method_name, method_func in search_methods:
|
45 |
-
try:
|
46 |
-
print(f"Trying {method_name} search...")
|
47 |
-
method_results = method_func(query)
|
48 |
-
if method_results:
|
49 |
-
results.extend(method_results)
|
50 |
-
print(f"{method_name} found {len(method_results)} results")
|
51 |
-
if len(results) >= 3: # Enough results
|
52 |
-
break
|
53 |
-
except Exception as e:
|
54 |
-
print(f"{method_name} search failed: {e}")
|
55 |
-
continue
|
56 |
-
|
57 |
-
if not results:
|
58 |
-
return "No search results found. All search methods failed."
|
59 |
-
|
60 |
-
# Format results
|
61 |
-
formatted_results = []
|
62 |
-
for i, result in enumerate(results[:8]):
|
63 |
-
if isinstance(result, dict):
|
64 |
-
title = result.get('title', '')
|
65 |
-
content = result.get('content', '')
|
66 |
-
url = result.get('url', '')
|
67 |
-
formatted = f"{title}. {content}"
|
68 |
-
if url:
|
69 |
-
formatted += f" (Source: {url})"
|
70 |
-
formatted_results.append(formatted)
|
71 |
-
else:
|
72 |
-
formatted_results.append(str(result))
|
73 |
-
|
74 |
-
return "\n\n".join(formatted_results)
|
75 |
-
|
76 |
-
def search_wikipedia(query: str) -> list:
|
77 |
-
"""Search Wikipedia directly"""
|
78 |
-
results = []
|
79 |
-
|
80 |
-
try:
|
81 |
-
# Wikipedia API search
|
82 |
-
search_url = "https://en.wikipedia.org/w/api.php"
|
83 |
-
|
84 |
-
# First, search for articles
|
85 |
-
search_params = {
|
86 |
-
"action": "query",
|
87 |
-
"list": "search",
|
88 |
-
"srsearch": query,
|
89 |
-
"format": "json",
|
90 |
-
"srlimit": 5,
|
91 |
-
"srprop": "snippet|titlesnippet|size|wordcount"
|
92 |
-
}
|
93 |
-
|
94 |
-
response = requests.get(search_url, params=search_params, timeout=10)
|
95 |
-
if response.status_code == 200:
|
96 |
-
data = response.json()
|
97 |
-
search_results = data.get("query", {}).get("search", [])
|
98 |
-
|
99 |
-
for item in search_results[:3]:
|
100 |
-
title = item.get("title", "")
|
101 |
-
snippet = re.sub(r'<[^>]+>', '', item.get("snippet", ""))
|
102 |
-
|
103 |
-
# Get more detailed content
|
104 |
-
page_params = {
|
105 |
-
"action": "query",
|
106 |
-
"prop": "extracts|info",
|
107 |
-
"exintro": True,
|
108 |
-
"explaintext": True,
|
109 |
-
"inprop": "url",
|
110 |
-
"titles": title,
|
111 |
-
"format": "json",
|
112 |
-
"exsentences": 5
|
113 |
-
}
|
114 |
-
|
115 |
-
page_response = requests.get(search_url, params=page_params, timeout=10)
|
116 |
-
if page_response.status_code == 200:
|
117 |
-
page_data = page_response.json()
|
118 |
-
pages = page_data.get("query", {}).get("pages", {})
|
119 |
-
|
120 |
-
for page_id, page_info in pages.items():
|
121 |
-
extract = page_info.get("extract", "")
|
122 |
-
url = page_info.get("fullurl", "")
|
123 |
-
|
124 |
-
if extract:
|
125 |
-
results.append({
|
126 |
-
"title": f"Wikipedia: {title}",
|
127 |
-
"content": extract[:500],
|
128 |
-
"url": url
|
129 |
-
})
|
130 |
-
break
|
131 |
-
else:
|
132 |
-
# Use snippet if can't get extract
|
133 |
-
results.append({
|
134 |
-
"title": f"Wikipedia: {title}",
|
135 |
-
"content": snippet,
|
136 |
-
"url": f"https://en.wikipedia.org/wiki/{title.replace(' ', '_')}"
|
137 |
-
})
|
138 |
-
|
139 |
-
except Exception as e:
|
140 |
-
print(f"Wikipedia search error: {e}")
|
141 |
-
|
142 |
-
return results
|
143 |
-
|
144 |
-
def search_duckduckgo(query: str) -> list:
|
145 |
-
"""Search using DuckDuckGo"""
|
146 |
-
results = []
|
147 |
-
|
148 |
-
try:
|
149 |
-
with DDGS() as ddgs:
|
150 |
-
# Simple search without problematic parameters
|
151 |
-
search_results = list(ddgs.text(query, max_results=5))
|
152 |
-
|
153 |
-
for r in search_results:
|
154 |
-
results.append({
|
155 |
-
"title": r.get("title", ""),
|
156 |
-
"content": r.get("body", ""),
|
157 |
-
"url": r.get("href", "")
|
158 |
-
})
|
159 |
-
|
160 |
-
except Exception as e:
|
161 |
-
print(f"DuckDuckGo error: {e}")
|
162 |
-
|
163 |
-
return results
|
164 |
-
|
165 |
-
def search_google_fallback(query: str) -> list:
|
166 |
-
"""Fallback Google search using alternative methods"""
|
167 |
-
results = []
|
168 |
-
|
169 |
-
try:
|
170 |
-
# Try Google Custom Search JSON API simulation
|
171 |
-
# This is a fallback method - in production, use proper API
|
172 |
-
encoded_query = urllib.parse.quote(query)
|
173 |
-
|
174 |
-
# Try to get Google search results page
|
175 |
-
headers = {
|
176 |
-
'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'
|
177 |
-
}
|
178 |
-
|
179 |
-
# Use a Google search URL
|
180 |
-
search_url = f"https://www.google.com/search?q={encoded_query}&hl=en"
|
181 |
-
|
182 |
-
# Note: This is a simplified approach and may not always work
|
183 |
-
# In production, use Google Custom Search API
|
184 |
-
|
185 |
-
except Exception as e:
|
186 |
-
print(f"Google fallback error: {e}")
|
187 |
-
|
188 |
-
return results
|
189 |
-
|
190 |
-
def search_bing_fallback(query: str) -> list:
|
191 |
-
"""Fallback Bing search"""
|
192 |
-
results = []
|
193 |
-
|
194 |
-
try:
|
195 |
-
# Bing Web Search API would be used here in production
|
196 |
-
# This is a placeholder for the pattern
|
197 |
-
pass
|
198 |
-
|
199 |
-
except Exception as e:
|
200 |
-
print(f"Bing fallback error: {e}")
|
201 |
-
|
202 |
-
return results
|
203 |
-
|
204 |
-
@tool
|
205 |
-
def calculate(expression: str) -> str:
|
206 |
-
"""Evaluate mathematical expressions safely."""
|
207 |
-
try:
|
208 |
-
# Clean the expression
|
209 |
-
expression = expression.strip()
|
210 |
-
|
211 |
-
# Handle various notations
|
212 |
-
expression = expression.replace("×", "*").replace("÷", "/")
|
213 |
-
expression = expression.replace("^", "**")
|
214 |
-
expression = expression.replace(",", "")
|
215 |
-
|
216 |
-
# Handle percentages
|
217 |
-
expression = re.sub(r'(\d+(?:\.\d+)?)\s*%\s*of\s*(\d+(?:\.\d+)?)', r'(\2 * \1 / 100)', expression)
|
218 |
-
expression = re.sub(r'(\d+(?:\.\d+)?)\s*%', r'(\1/100)', expression)
|
219 |
-
|
220 |
-
# Safe evaluation
|
221 |
-
allowed_names = {
|
222 |
-
"abs": abs, "round": round, "min": min, "max": max,
|
223 |
-
"pow": pow, "sum": sum, "__builtins__": {}
|
224 |
-
}
|
225 |
-
|
226 |
-
result = eval(expression, allowed_names)
|
227 |
-
|
228 |
-
if isinstance(result, float) and result.is_integer():
|
229 |
-
return str(int(result))
|
230 |
-
return str(result)
|
231 |
-
|
232 |
-
except Exception as e:
|
233 |
-
return f"Calculation error: {e}"
|
234 |
-
|
235 |
-
@tool
|
236 |
-
def wikipedia_summary(query: str) -> str:
|
237 |
-
"""Get Wikipedia summary for a topic."""
|
238 |
-
try:
|
239 |
-
results = search_wikipedia(query)
|
240 |
-
if results:
|
241 |
-
# Combine top results
|
242 |
-
summaries = []
|
243 |
-
for r in results[:2]:
|
244 |
-
summaries.append(f"{r['title']}: {r['content']}")
|
245 |
-
return "\n\n".join(summaries)
|
246 |
-
|
247 |
-
return f"No Wikipedia article found for '{query}'"
|
248 |
-
|
249 |
-
except Exception as e:
|
250 |
-
return f"Wikipedia error: {e}"
|
251 |
-
|
252 |
-
@tool
|
253 |
-
def define_term(term: str) -> str:
|
254 |
-
"""Define a term using dictionary API."""
|
255 |
-
try:
|
256 |
-
term = term.strip().lower()
|
257 |
-
|
258 |
-
# Try dictionary API
|
259 |
-
response = requests.get(
|
260 |
-
f"https://api.dictionaryapi.dev/api/v2/entries/en/{term}",
|
261 |
-
timeout=10
|
262 |
-
)
|
263 |
-
|
264 |
-
if response.status_code == 200:
|
265 |
-
data = response.json()
|
266 |
-
definitions = []
|
267 |
-
|
268 |
-
for entry in data:
|
269 |
-
for meaning in entry.get("meanings", []):
|
270 |
-
for definition in meaning.get("definitions", []):
|
271 |
-
def_text = definition.get("definition", "")
|
272 |
-
if def_text:
|
273 |
-
definitions.append(def_text)
|
274 |
-
|
275 |
-
if definitions:
|
276 |
-
return definitions[0] # Return first definition
|
277 |
-
|
278 |
-
# Fallback to Wikipedia
|
279 |
-
wiki_results = search_wikipedia(f"{term} definition meaning")
|
280 |
-
if wiki_results:
|
281 |
-
return wiki_results[0]['content'][:200]
|
282 |
-
|
283 |
-
return f"No definition found for '{term}'"
|
284 |
-
|
285 |
-
except Exception as e:
|
286 |
-
return f"Definition error: {e}"
|
287 |
-
|
288 |
-
# Advanced search function for specific GAIA queries
|
289 |
-
@tool
|
290 |
-
def gaia_smart_search(query: str) -> str:
|
291 |
-
"""Smart search specifically optimized for GAIA questions."""
|
292 |
-
|
293 |
-
# Parse query for specific patterns
|
294 |
-
query_lower = query.lower()
|
295 |
-
|
296 |
-
# For album/discography queries
|
297 |
-
if 'album' in query_lower or 'discography' in query_lower:
|
298 |
-
artist_match = re.search(r'([\w\s]+?)(?:\s+album|\s+discography|\s+between)', query)
|
299 |
-
if artist_match:
|
300 |
-
artist = artist_match.group(1).strip()
|
301 |
-
# Search for discography
|
302 |
-
return web_search(f"{artist} discography albums list")
|
303 |
-
|
304 |
-
# For Olympic queries
|
305 |
-
if 'olympic' in query_lower:
|
306 |
-
year_match = re.search(r'(\d{4})\s+(?:summer|winter)?\s*olympics', query_lower)
|
307 |
-
if year_match:
|
308 |
-
year = year_match.group(1)
|
309 |
-
return web_search(f"{year} Olympics participating countries athletes count")
|
310 |
-
|
311 |
-
# For academic papers
|
312 |
-
if 'paper' in query_lower or 'article' in query_lower:
|
313 |
-
author_match = re.search(r'by\s+([\w\s]+?)(?:\s+was|\s+published|\s+in)', query)
|
314 |
-
if author_match:
|
315 |
-
author = author_match.group(1).strip()
|
316 |
-
return web_search(f"{author} research paper article")
|
317 |
-
|
318 |
-
# Default to regular search
|
319 |
-
return web_search(query)
|
320 |
-
|
321 |
-
# List of tools
|
322 |
-
TOOLS = [web_search, calculate, wikipedia_summary, define_term, reverse_text, gaia_smart_search]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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