CoolFace
Apppublic

softveda/AlfredAgent

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
visit_webpage.py50 linesDownload Raw Back to tools
1from typing import Any, Optional2from smolagents.tools import Tool3import requests4import re5import markdownify6import smolagents7 8class VisitWebpageTool(Tool):9    name = "visit_webpage"10    description = "Visits a webpage at the given url and reads its content as a markdown string. Use this to browse webpages."11    inputs = {'url': {'type': 'string', 'description': 'The url of the webpage to visit.'}}12    output_type = "string"13 14    def __init__(self, max_output_length: int = 40000):15        super().__init__()16        self.max_output_length = max_output_length17 18    def forward(self, url: str) -> str:19        try:20            import re21 22            import requests23            from markdownify import markdownify24            from requests.exceptions import RequestException25 26            from smolagents.utils import truncate_content27        except ImportError as e:28            raise ImportError(29                "You must install packages `markdownify` and `requests` to run this tool: for instance run `pip install markdownify requests`."30            ) from e31        try:32            # Send a GET request to the URL with a 20-second timeout33            response = requests.get(url, timeout=20)34            response.raise_for_status()  # Raise an exception for bad status codes35 36            # Convert the HTML content to Markdown37            markdown_content = markdownify(response.text).strip()38 39            # Remove multiple line breaks40            markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)41 42            return truncate_content(markdown_content, self.max_output_length)43 44        except requests.exceptions.Timeout:45            return "The request timed out. Please try again later or check the URL."46        except RequestException as e:47            return f"Error fetching the webpage: {str(e)}"48        except Exception as e:49            return f"An unexpected error occurred: {str(e)}"50