Delete tools/file/download_file_from_url.py
Browse files
tools/file/download_file_from_url.py
DELETED
|
@@ -1,41 +0,0 @@
|
|
| 1 |
-
from langchain_core.tools import tool
|
| 2 |
-
from typing import Optional
|
| 3 |
-
import os
|
| 4 |
-
from urllib.parse import urlparse
|
| 5 |
-
import requests
|
| 6 |
-
import uuid
|
| 7 |
-
import tempfile
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
@tool
|
| 11 |
-
def download_file_from_url(url: str, filename: Optional[str] = None) -> str:
|
| 12 |
-
"""
|
| 13 |
-
Download a file from a URL and save it to a temporary location.
|
| 14 |
-
Args:
|
| 15 |
-
url (str): the URL of the file to download.
|
| 16 |
-
filename (str, optional): the name of the file. If not provided, a random name file will be created.
|
| 17 |
-
"""
|
| 18 |
-
try:
|
| 19 |
-
# Parse URL to get filename if not provided
|
| 20 |
-
if not filename:
|
| 21 |
-
path = urlparse(url).path
|
| 22 |
-
filename = os.path.basename(path)
|
| 23 |
-
if not filename:
|
| 24 |
-
filename = f"downloaded_{uuid.uuid4().hex[:8]}"
|
| 25 |
-
|
| 26 |
-
# Create temporary file
|
| 27 |
-
temp_dir = tempfile.gettempdir()
|
| 28 |
-
filepath = os.path.join(temp_dir, filename)
|
| 29 |
-
|
| 30 |
-
# Download the file
|
| 31 |
-
response = requests.get(url, stream=True)
|
| 32 |
-
response.raise_for_status()
|
| 33 |
-
|
| 34 |
-
# Save the file
|
| 35 |
-
with open(filepath, "wb") as f:
|
| 36 |
-
for chunk in response.iter_content(chunk_size=8192):
|
| 37 |
-
f.write(chunk)
|
| 38 |
-
|
| 39 |
-
return f"File downloaded to {filepath}. You can read this file to process its contents."
|
| 40 |
-
except Exception as e:
|
| 41 |
-
return f"Error downloading file: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|