Update tools.py
#23
by
shsad
- opened
tools.py
CHANGED
|
@@ -2,7 +2,7 @@ from smolagents import DuckDuckGoSearchTool
|
|
| 2 |
from smolagents import Tool
|
| 3 |
import random
|
| 4 |
from huggingface_hub import list_models
|
| 5 |
-
|
| 6 |
|
| 7 |
# Initialize the DuckDuckGo search tool
|
| 8 |
#search_tool = DuckDuckGoSearchTool()
|
|
@@ -54,3 +54,23 @@ class HubStatsTool(Tool):
|
|
| 54 |
except Exception as e:
|
| 55 |
return f"Error fetching models for {author}: {str(e)}"
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from smolagents import Tool
|
| 3 |
import random
|
| 4 |
from huggingface_hub import list_models
|
| 5 |
+
import requests
|
| 6 |
|
| 7 |
# Initialize the DuckDuckGo search tool
|
| 8 |
#search_tool = DuckDuckGoSearchTool()
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
return f"Error fetching models for {author}: {str(e)}"
|
| 56 |
|
| 57 |
+
class NewsTool(Tool):
|
| 58 |
+
name = "get_latest_news"
|
| 59 |
+
description = "Fetches the latest news headlines about a given topic."
|
| 60 |
+
|
| 61 |
+
inputs = {
|
| 62 |
+
"topic": {
|
| 63 |
+
"type": "string",
|
| 64 |
+
"description": "The topic to search news for."
|
| 65 |
+
}
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
def forward(self, topic: str):
|
| 69 |
+
url = f"https://newsapi.org/v2/everything?q={topic}&apiKey=15768396aec7461d927ed37dfffa2f84"
|
| 70 |
+
response = requests.get(url)
|
| 71 |
+
data = response.json()
|
| 72 |
+
if "articles" in data and len(data["articles"]) > 0:
|
| 73 |
+
headlines = [a["title"] for a in data["articles"][:5]]
|
| 74 |
+
return "\n".join(headlines)
|
| 75 |
+
return "No recent news found."
|
| 76 |
+
|