CoderBurntt/First_agent_template
0
1from typing import Any, Optional2from smolagents.tools import Tool3import requests4import markdownify5import smolagents6import re7 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, *args, **kwargs):15 super().__init__(*args, **kwargs)16 self.is_initialized = True17 18 def forward(self, url: str) -> str:19 try:20 import requests21 from markdownify import markdownify22 from requests.exceptions import RequestException23 24 from smolagents.utils import truncate_content25 except ImportError as e:26 raise ImportError(27 "You must install packages `markdownify` and `requests` to run this tool: for instance run `pip install markdownify requests`."28 ) from e29 try:30 # Add User-Agent header to mimic a browser31 headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}32 response = requests.get(url, headers=headers, timeout=20)33 response.raise_for_status()34 35 markdown_content = markdownify(response.text).strip()36 markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)37 38 return truncate_content(markdown_content, 10000)39 40 except requests.exceptions.Timeout:41 return "The request timed out. Please try again later or check the URL."42 except RequestException as e:43 return f"Error fetching the webpage: {str(e)}"44 except Exception as e:45 return f"An unexpected error occurred: {str(e)}"