Skip to content

Commit 36783c3

Browse files
keetrapParteek Kambojalbertvillanova
authored
Allow VisitWebpageTool to run on remote executors without installing smolagents (#1288)
Co-authored-by: Parteek Kamboj <[email protected]> Co-authored-by: Albert Villanova del Moral <[email protected]>
1 parent 0dfac36 commit 36783c3

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/smolagents/default_tools.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,22 @@ def __init__(self, max_output_length: int = 40000):
227227
super().__init__()
228228
self.max_output_length = max_output_length
229229

230+
def _truncate_content(self, content: str, max_length: int) -> str:
231+
if len(content) <= max_length:
232+
return content
233+
return (
234+
content[: max_length // 2]
235+
+ f"\n..._This content has been truncated to stay below {max_length} characters_...\n"
236+
+ content[-max_length // 2 :]
237+
)
238+
230239
def forward(self, url: str) -> str:
231240
try:
232241
import re
233242

234243
import requests
235244
from markdownify import markdownify
236245
from requests.exceptions import RequestException
237-
238-
from smolagents.utils import truncate_content
239246
except ImportError as e:
240247
raise ImportError(
241248
"You must install packages `markdownify` and `requests` to run this tool: for instance run `pip install markdownify requests`."
@@ -251,7 +258,7 @@ def forward(self, url: str) -> str:
251258
# Remove multiple line breaks
252259
markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
253260

254-
return truncate_content(markdown_content, self.max_output_length)
261+
return self._truncate_content(markdown_content, self.max_output_length)
255262

256263
except requests.exceptions.Timeout:
257264
return "The request timed out. Please try again later or check the URL."

0 commit comments

Comments
 (0)