at1300 commited on
Commit
656e85a
·
verified ·
1 Parent(s): aaa8b70

Create tools.py

Browse files
Files changed (1) hide show
  1. tools.py +34 -0
tools.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import requests
3
+ from markdownify import markdownify
4
+ from requests.exceptions import RequestException
5
+ from smolagents import tool
6
+
7
+
8
+ @tool
9
+ def visit_webpage(url: str) -> str:
10
+ """Visits a webpage at the given URL and returns its content as a markdown string.
11
+
12
+ Args:
13
+ url: The URL of the webpage to visit.
14
+
15
+ Returns:
16
+ The content of the webpage converted to Markdown, or an error message if the request fails.
17
+ """
18
+ try:
19
+ # Send a GET request to the URL
20
+ response = requests.get(url)
21
+ response.raise_for_status() # Raise an exception for bad status codes
22
+
23
+ # Convert the HTML content to Markdown
24
+ markdown_content = markdownify(response.text).strip()
25
+
26
+ # Remove multiple line breaks
27
+ markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
28
+
29
+ return markdown_content
30
+
31
+ except RequestException as e:
32
+ return f"Error fetching the webpage: {str(e)}"
33
+ except Exception as e:
34
+ return f"An unexpected error occurred: {str(e)}"