rnrahate007 commited on
Commit
9c2a00b
·
verified ·
1 Parent(s): 1e1685f

Update tools/visit_webpage.py

Browse files
Files changed (1) hide show
  1. tools/visit_webpage.py +100 -29
tools/visit_webpage.py CHANGED
@@ -1,45 +1,116 @@
1
- from typing import Any, Optional
2
- from smolagents.tools import Tool
3
  import requests
4
- import markdownify
5
- import smolagents
 
 
 
 
6
 
7
  class 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 forward(self, url: str) -> str:
 
 
 
 
 
 
 
 
 
 
 
14
  try:
15
- import requests
16
- from markdownify import markdownify
17
- from requests.exceptions import RequestException
18
-
19
- from smolagents.utils import truncate_content
20
- except ImportError as e:
21
- raise ImportError(
22
- "You must install packages `markdownify` and `requests` to run this tool: for instance run `pip install markdownify requests`."
23
- ) from e
24
- try:
25
- # Send a GET request to the URL with a 20-second timeout
26
- response = requests.get(url, timeout=20)
27
- response.raise_for_status() # Raise an exception for bad status codes
28
 
29
- # Convert the HTML content to Markdown
30
- markdown_content = markdownify(response.text).strip()
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- # Remove multiple line breaks
33
- markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
34
 
35
- return truncate_content(markdown_content, 10000)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  except requests.exceptions.Timeout:
38
- return "The request timed out. Please try again later or check the URL."
 
 
 
 
 
 
 
 
 
 
39
  except RequestException as e:
40
- return f"Error fetching the webpage: {str(e)}"
 
 
 
 
41
  except Exception as e:
42
- return f"An unexpected error occurred: {str(e)}"
43
 
44
- def __init__(self, *args, **kwargs):
45
- self.is_initialized = False
 
 
1
+ import re
 
2
  import requests
3
+ from markdownify import markdownify
4
+
5
+ from requests.exceptions import RequestException
6
+ from smolagents.tools import Tool
7
+ from smolagents.utils import truncate_content
8
+
9
 
10
  class VisitWebpageTool(Tool):
11
+ """
12
+ Visits a webpage and converts its contents into Markdown.
13
+ """
14
+
15
  name = "visit_webpage"
16
+
17
+ description = (
18
+ "Visits a webpage given its URL and returns the readable "
19
+ "markdown version of the webpage. Use this after performing "
20
+ "a web search to extract detailed information."
21
+ )
22
+
23
+ inputs = {
24
+ "url": {
25
+ "type": "string",
26
+ "description": "URL of the webpage to visit."
27
+ }
28
+ }
29
+
30
  output_type = "string"
31
 
32
+ def __init__(self):
33
+
34
+ super().__init__()
35
+
36
  def forward(self, url: str) -> str:
37
+
38
+ headers = {
39
+ "User-Agent": (
40
+ "Mozilla/5.0 "
41
+ "(Windows NT 10.0; Win64; x64) "
42
+ "AppleWebKit/537.36 "
43
+ "(KHTML, like Gecko) "
44
+ "Chrome/138.0 Safari/537.36"
45
+ )
46
+ }
47
+
48
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
+ response = requests.get(
51
+
52
+ url,
53
+
54
+ headers=headers,
55
+
56
+ timeout=20,
57
+
58
+ allow_redirects=True,
59
+
60
+ )
61
+
62
+ response.raise_for_status()
63
 
64
+ markdown = markdownify(
 
65
 
66
+ response.text,
67
+
68
+ heading_style="ATX"
69
+
70
+ )
71
+
72
+ markdown = re.sub(
73
+
74
+ r"\n{3,}",
75
+
76
+ "\n\n",
77
+
78
+ markdown
79
+
80
+ )
81
+
82
+ markdown = markdown.strip()
83
+
84
+ markdown = truncate_content(
85
+
86
+ markdown,
87
+
88
+ 20000
89
+
90
+ )
91
+
92
+ return markdown
93
 
94
  except requests.exceptions.Timeout:
95
+
96
+ return (
97
+ "The webpage request timed out."
98
+ )
99
+
100
+ except requests.exceptions.HTTPError as e:
101
+
102
+ return (
103
+ f"HTTP Error: {e}"
104
+ )
105
+
106
  except RequestException as e:
107
+
108
+ return (
109
+ f"Request failed: {e}"
110
+ )
111
+
112
  except Exception as e:
 
113
 
114
+ return (
115
+ f"Unexpected error: {e}"
116
+ )