lcaignae/testAlfredAgent
0
1from typing import Any, Optional2from smolagents.tools import Tool3import requests4import markdownify5import re6 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 __init__(self, max_output_length: int = 40000):14 super().__init__()15 self.max_output_length = max_output_length16 17 def _truncate_content(self, content: str, max_length: int) -> str:18 if len(content) <= max_length:19 return content20 return (21 content[: max_length // 2]22 + f"\n..._This content has been truncated to stay below {max_length} characters_...\n"23 + content[-max_length // 2 :]24 )25 26 def forward(self, url: str) -> str:27 try:28 import re29 30 import requests31 from markdownify import markdownify32 from requests.exceptions import RequestException33 except ImportError as e:34 raise ImportError(35 "You must install packages `markdownify` and `requests` to run this tool: for instance run `pip install markdownify requests`."36 ) from e37 try:38 # Send a GET request to the URL with a 20-second timeout39 response = requests.get(url, timeout=20)40 response.raise_for_status() # Raise an exception for bad status codes41 42 # Convert the HTML content to Markdown43 markdown_content = markdownify(response.text).strip()44 45 # Remove multiple line breaks46 markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)47 48 return self._truncate_content(markdown_content, self.max_output_length)49 50 except requests.exceptions.Timeout:51 return "The request timed out. Please try again later or check the URL."52 except RequestException as e:53 return f"Error fetching the webpage: {str(e)}"54 except Exception as e:55 return f"An unexpected error occurred: {str(e)}"56 