File size: 1,152 Bytes
8fe992b
487a7fe
64d5dc8
 
 
8fe992b
 
 
64d5dc8
 
8fe992b
 
 
 
64d5dc8
 
 
 
 
 
8fe992b
 
64d5dc8
 
 
 
 
 
 
 
 
8fe992b
64d5dc8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from smolagents.tools import Tool
import requests
from markdownify import markdownify
import re
from urllib.parse import urlparse

class VisitWebpageTool(Tool):
    name = "visit_webpage"
    description = "Visits a webpage and returns its content as markdown."
    inputs = {'url': {'type': 'string', 'description': 'The URL to visit'}}
    output_type = "string"

    def forward(self, url: str) -> str:
        try:
            if not re.match(r'^https?://', url):
                return "Error: Invalid URL protocol"
                
            parsed = urlparse(url)
            if not parsed.netloc:
                return "Error: Invalid URL format"

            response = requests.get(url, timeout=20)
            response.raise_for_status()
            
            content = markdownify(response.text)
            content = re.sub(r'\n{3,}', '\n\n', content.strip())
            
            return content[:10000]  # Limit output length
            
        except requests.exceptions.RequestException as e:
            return f"Error fetching page: {str(e)}"
        except Exception as e:
            return f"Unexpected error: {str(e)}"