| import os | |
| import requests | |
| from smolagents import Tool | |
| from managers.file_manager import FileManager | |
| class FetchURLContentTool(Tool): | |
| name = "fetch_url_content" | |
| description = "Downloads the content or file at the given url and returns the local path of the downloaded file." | |
| inputs = { | |
| "url": { | |
| "type": "string", | |
| "description": "The url of the content or file to download." | |
| } | |
| } | |
| output_type = "string" | |
| def forward(self, url: str) -> str: | |
| try: | |
| headers = { | |
| 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' | |
| } | |
| response = requests.get(url, headers=headers) | |
| response.raise_for_status() | |
| suffix = os.path.splitext(url)[-1] or '.bin' | |
| return "The path of the downloaded file is: " + FileManager.create_temp_file(response.content, suffix) | |
| except Exception as e: | |
| return f"Error fetch_url_content is not working properly, error: {e}, please skip this tool" | |