File size: 1,109 Bytes
6220346
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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"