Romain Lembo commited on
Commit
b48e08c
·
1 Parent(s): fc9e2f8

Add file handling tools: save_read_file and download_file

Browse files
Files changed (3) hide show
  1. agent.py +4 -0
  2. tools/download_file.py +40 -0
  3. tools/save_read_file.py +24 -0
agent.py CHANGED
@@ -26,6 +26,8 @@ from tools.power import power
26
  from tools.subtract import subtract
27
  from tools.square_root import square_root
28
  from tools.execute_code_multilang import execute_code_multilang
 
 
29
  # from tools.audio_transcription import audio_transcription
30
 
31
  load_dotenv()
@@ -67,6 +69,8 @@ tools = [
67
  power,
68
  subtract,
69
  square_root,
 
 
70
  # audio_transcription,
71
  ]
72
 
 
26
  from tools.subtract import subtract
27
  from tools.square_root import square_root
28
  from tools.execute_code_multilang import execute_code_multilang
29
+ from tools.save_read_file import save_read_file
30
+ from tools.download_file import download_file
31
  # from tools.audio_transcription import audio_transcription
32
 
33
  load_dotenv()
 
69
  power,
70
  subtract,
71
  square_root,
72
+ save_read_file,
73
+ download_file,
74
  # audio_transcription,
75
  ]
76
 
tools/download_file.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import uuid
3
+ import requests
4
+ from langchain_core.tools import tool
5
+ from typing import Optional
6
+ import tempfile
7
+ from urllib.parse import urlparse
8
+
9
+ @tool
10
+ def download_file(url: str, filename: Optional[str] = None) -> str:
11
+ """
12
+ Download a file from a URL and save it to a temporary location.
13
+ Args:
14
+ url (str): the URL of the file to download.
15
+ filename (str, optional): the name of the file. If not provided, a random name file will be created.
16
+ """
17
+ try:
18
+ # Parse URL to get filename if not provided
19
+ if not filename:
20
+ path = urlparse(url).path
21
+ filename = os.path.basename(path)
22
+ if not filename:
23
+ filename = f"downloaded_{uuid.uuid4().hex[:8]}"
24
+
25
+ # Create temporary file
26
+ temp_dir = tempfile.gettempdir()
27
+ filepath = os.path.join(temp_dir, filename)
28
+
29
+ # Download the file
30
+ response = requests.get(url, stream=True)
31
+ response.raise_for_status()
32
+
33
+ # Save the file
34
+ with open(filepath, "wb") as f:
35
+ for chunk in response.iter_content(chunk_size=8192):
36
+ f.write(chunk)
37
+
38
+ return f"File downloaded to {filepath}. You can read this file to process its contents."
39
+ except Exception as e:
40
+ return f"Error downloading file: {str(e)}"
tools/save_read_file.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from langchain_core.tools import tool
3
+ from typing import Optional
4
+ import tempfile
5
+
6
+ @tool
7
+ def save_read_file(content: str, filename: Optional[str] = None) -> str:
8
+ """
9
+ Save content to a file and return the path.
10
+ Args:
11
+ content (str): the content to save to the file
12
+ filename (str, optional): the name of the file. If not provided, a random name file will be created.
13
+ """
14
+ temp_dir = tempfile.gettempdir()
15
+ if filename is None:
16
+ temp_file = tempfile.NamedTemporaryFile(delete=False, dir=temp_dir)
17
+ filepath = temp_file.name
18
+ else:
19
+ filepath = os.path.join(temp_dir, filename)
20
+
21
+ with open(filepath, "w") as f:
22
+ f.write(content)
23
+
24
+ return f"File saved to {filepath}. You can read this file to process its contents."