@@ -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