Jovynne commited on
Commit
4c3d1bd
·
verified ·
1 Parent(s): d66b0de

Create search_tools.py

Browse files
Files changed (1) hide show
  1. search_tools.py +50 -0
search_tools.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # search_tools.py
2
+ import requests
3
+ from typing import List, Dict
4
+
5
+ # ✅ Safe optional import
6
+ try:
7
+ from smolagents import tool
8
+ except ImportError:
9
+ from mock_smolagents import tool
10
+
11
+
12
+ @tool
13
+ def search_with_links(query: str, num_results: int = 5) -> str:
14
+ """Search the web and return results with clickable URLs."""
15
+ try:
16
+ from duckduckgo_search import DDGS
17
+
18
+ num_results = max(1, min(10, num_results))
19
+
20
+ results = []
21
+ with DDGS() as ddgs:
22
+ for idx, r in enumerate(ddgs.text(query, max_results=num_results), 1):
23
+ results.append(
24
+ f"**{idx}. {r['title']}**\n"
25
+ f"- **URL:** {r['href']}\n"
26
+ f"- **Preview:** {r['body'][:200]}..."
27
+ )
28
+
29
+ if results:
30
+ return f"## 🔍 Search Results for '{query}'\n\n" + "\n\n---\n\n".join(results)
31
+
32
+ return f"No results found for '{query}'"
33
+
34
+ except ImportError:
35
+ return (
36
+ "## 🔍 Mock Search Results\n"
37
+ "- https://huggingface.co\n"
38
+ "- https://arxiv.org\n"
39
+ "- https://scikit-learn.org\n\n"
40
+ "*Install duckduckgo-search for real results*"
41
+ )
42
+
43
+
44
+ @tool
45
+ def search_academic(query: str) -> str:
46
+ return (
47
+ f"## 📚 Academic Search: '{query}'\n"
48
+ f"- https://arxiv.org/search/?query={query.replace(' ', '+')}\n"
49
+ f"- https://scholar.google.com/scholar?q={query.replace(' ', '+')}"
50
+ )