Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -123,3 +123,40 @@ def duckduckgo_search(query: str) -> str:
|
|
| 123 |
|
| 124 |
except Exception as e:
|
| 125 |
return f"Search failed: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
except Exception as e:
|
| 125 |
return f"Search failed: {e}"
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
@tool
|
| 129 |
+
def search_papers(query: str) -> str:
|
| 130 |
+
"""Search for academic papers using Semantic Scholar.
|
| 131 |
+
Args:
|
| 132 |
+
query: The query to search the papers in Semantic Scholar
|
| 133 |
+
"""
|
| 134 |
+
url = "https://api.semanticscholar.org/graph/v1/paper/search"
|
| 135 |
+
params = {
|
| 136 |
+
"query": query,
|
| 137 |
+
"limit": 3,
|
| 138 |
+
"fields": "title,abstract,authors,url,year"
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
try:
|
| 142 |
+
response = requests.get(url, params=params)
|
| 143 |
+
data = response.json()
|
| 144 |
+
|
| 145 |
+
if not data.get("data"):
|
| 146 |
+
return "No papers found."
|
| 147 |
+
|
| 148 |
+
results = []
|
| 149 |
+
for paper in data["data"]:
|
| 150 |
+
title = paper.get("title", "No title")
|
| 151 |
+
authors = ", ".join([a.get("name", "") for a in paper.get("authors", [])])
|
| 152 |
+
year = paper.get("year", "n.d.")
|
| 153 |
+
abstract = paper.get("abstract", "No abstract available.")
|
| 154 |
+
link = paper.get("url", "")
|
| 155 |
+
|
| 156 |
+
result = f"**{title}** ({year}) by {authors}\n{abstract}\nLink: {link}"
|
| 157 |
+
results.append(result)
|
| 158 |
+
|
| 159 |
+
return "\n\n".join(results)
|
| 160 |
+
|
| 161 |
+
except Exception as e:
|
| 162 |
+
return f"Error fetching papers: {e}"
|