CoolFace
Apppublic

Gr1ntch/First_agent_template

sourceHugging Faceupdated 4mo agoView on Hugging Face
0likes
visit_webpage.py47 linesDownload Raw Back to tools
1from typing import Any, Optional2from smolagents.tools import Tool3import requests4import markdownify5import smolagents6 7class VisitWebpageTool(Tool):8    name = "visit_webpage"9    description = "Visits a webpage at the given url and reads its content as a markdown string. Use this to browse webpages."10    inputs = {'url': {'type': 'string', 'description': 'The url of the webpage to visit.'}}11    output_type = "string"12 13    def forward(self, url: str) -> str:14        try:15            import re16            import requests17            from markdownify import markdownify18            from requests.exceptions import RequestException19 20            from smolagents.utils import truncate_content21        except ImportError as e:22            raise ImportError(23                "You must install packages `markdownify` and `requests` to run this tool: for instance run `pip install markdownify requests`."24            ) from e25        try:26            # Send a GET request to the URL with a 20-second timeout27            response = requests.get(url, timeout=20)28            response.raise_for_status()  # Raise an exception for bad status codes29 30            # Convert the HTML content to Markdown31            markdown_content = markdownify(response.text).strip()32 33            # Remove multiple line breaks34            markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)35 36            return truncate_content(markdown_content, 500)37 38        except requests.exceptions.Timeout:39            return "The request timed out. Please try again later or check the URL."40        except RequestException as e:41            return f"Error fetching the webpage: {str(e)}"42        except Exception as e:43            return f"An unexpected error occurred: {str(e)}"44 45    def __init__(self, *args, **kwargs):46        self.is_initialized = False47