First_agent_template / tools /visit_webpage.py
TCares's picture
Update tools/visit_webpage.py
001a158 verified
raw
history blame
2.55 kB
from typing import Any, Optional
from smolagents.tools import Tool
import requests
import markdownify
import smolagents
import re
class VisitWebpageTool(Tool):
name = "visit_webpage"
description = "Visits a webpage at the given url and reads its content as a markdown 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:
try:
import requests
from markdownify import markdownify
from requests.exceptions import RequestException
from smolagents.utils import truncate_content
except ImportError as e:
raise ImportError(
"You must install packages `markdownify` and `requests` to run this tool: for instance run `pip install markdownify requests`."
) from e
try:
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1 Safari/605.1.15",
"referer": "https://duckduckgo.com/",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
'accept-language': 'en-US,en;q=0.9',
'accept-Encoding': 'gzip, deflate, br',
'priority': 'u=0, i',
'referer': 'https://duckduckgo.com/',
'sec-fetch-dest': 'document',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'cross-site',
}
# Send a GET request to the URL with a 20-second timeout
response = requests.get(url, timeout=20, allow_redirects=True, headers=headers)
response.raise_for_status() # Raise an exception for bad status codes
# Convert the HTML content to Markdown
markdown_content = markdownify(response.text).strip()
# Remove multiple line breaks
markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
return truncate_content(markdown_content, 50000)
except requests.exceptions.Timeout:
return "The request timed out. Please try again later or check the URL."
except RequestException as e:
return f"Error fetching the webpage: {str(e)}"
except Exception as e:
return f"An unexpected error occurred: {str(e)}"
def __init__(self, *args, **kwargs):
self.is_initialized = False