SmolNews / tools /visit_webpage.py
jairo's picture
Modify prompts.yaml and app.py to focus on news and time information
3a2c59e
import xml.etree.ElementTree as ET
import requests
from smolagents.tools import Tool
class VisitWebpageTool(Tool):
name = "visit_webpage"
description = "Visits a webpage at the given url and reads its content as a xml string. Use this to browse webpages."
inputs = {
"url": {
"type": "string",
"description": "The url of the webpage to visit.",
}
}
output_type = "string"
def forward(self, url: str) -> str:
response = requests.get(url, timeout=20)
response.raise_for_status()
xml_content = response.text
start_index = xml_content.find("<rss")
if start_index == -1:
raise ValueError("Invalid RSS feed: <rss> element not found")
xml_content = xml_content[start_index:]
try:
root = ET.fromstring(xml_content)
except ET.ParseError as e:
raise ValueError(f"Error parsing XML: {e}") from e
channel = root.find("channel")
if channel is None:
raise ValueError("Invalid RSS feed: <channel> element not found")
titles = [item.findtext("title") for item in channel.findall("item")]
return titles
def __init__(self, *args, **kwargs):
self.is_initialized = False