Artsem Radzevich
commited on
Commit
·
6c4000b
1
Parent(s):
1786917
Fixed download_file tool.
Browse files- agent_config.py +21 -16
agent_config.py
CHANGED
|
@@ -6,6 +6,7 @@ import requests
|
|
| 6 |
import re
|
| 7 |
import tempfile
|
| 8 |
from pathlib import Path
|
|
|
|
| 9 |
|
| 10 |
@tool
|
| 11 |
def check_answer(answer: str) -> str:
|
|
@@ -34,37 +35,41 @@ def reverse_sentence_tool(reverse_sentence: str) -> str:
|
|
| 34 |
return " ".join(correct_words)
|
| 35 |
|
| 36 |
@tool
|
| 37 |
-
def download_file_if_any(base_api_url: str, task_id: str) -> str
|
| 38 |
"""
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
"""
|
| 44 |
url = f"{base_api_url}/files/{task_id}"
|
| 45 |
try:
|
| 46 |
resp = requests.get(url, timeout=30)
|
| 47 |
if resp.status_code == 404:
|
| 48 |
-
return None
|
| 49 |
-
resp.raise_for_status()
|
| 50 |
except requests.exceptions.HTTPError as e:
|
| 51 |
-
# propagate
|
| 52 |
-
raise e
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
# Try to keep original extension from Content-Disposition if present.
|
| 56 |
cdisp = resp.headers.get("content-disposition", "")
|
| 57 |
-
filename = task_id
|
| 58 |
if "filename=" in cdisp:
|
| 59 |
-
|
| 60 |
-
if
|
| 61 |
-
filename =
|
| 62 |
|
| 63 |
tmp_dir = Path(tempfile.gettempdir()) / "gaia_files"
|
| 64 |
-
tmp_dir.mkdir(exist_ok=True)
|
| 65 |
file_path = tmp_dir / filename
|
|
|
|
| 66 |
with open(file_path, "wb") as f:
|
| 67 |
f.write(resp.content)
|
|
|
|
| 68 |
return str(file_path)
|
| 69 |
|
| 70 |
def create_agent():
|
|
|
|
| 6 |
import re
|
| 7 |
import tempfile
|
| 8 |
from pathlib import Path
|
| 9 |
+
from typing import Optional
|
| 10 |
|
| 11 |
@tool
|
| 12 |
def check_answer(answer: str) -> str:
|
|
|
|
| 35 |
return " ".join(correct_words)
|
| 36 |
|
| 37 |
@tool
|
| 38 |
+
def download_file_if_any(base_api_url: str, task_id: str) -> Optional[str]:
|
| 39 |
"""
|
| 40 |
+
Downloads a file from a remote API using a task ID.
|
| 41 |
+
|
| 42 |
+
Args:
|
| 43 |
+
base_api_url (str): Base URL of the API to fetch files from (e.g., "https://example.com/api").
|
| 44 |
+
task_id (str): Task identifier used to locate the file.
|
| 45 |
+
|
| 46 |
+
Returns:
|
| 47 |
+
Optional[str]: Path to the downloaded file, or None if the file is not found (404).
|
| 48 |
"""
|
| 49 |
url = f"{base_api_url}/files/{task_id}"
|
| 50 |
try:
|
| 51 |
resp = requests.get(url, timeout=30)
|
| 52 |
if resp.status_code == 404:
|
| 53 |
+
return None # file not found
|
| 54 |
+
resp.raise_for_status() # raise other errors
|
| 55 |
except requests.exceptions.HTTPError as e:
|
| 56 |
+
raise e # propagate for logging/debugging
|
|
|
|
| 57 |
|
| 58 |
+
# Extract filename from Content-Disposition if present
|
|
|
|
| 59 |
cdisp = resp.headers.get("content-disposition", "")
|
| 60 |
+
filename = task_id # fallback
|
| 61 |
if "filename=" in cdisp:
|
| 62 |
+
match = re.search(r'filename="([^"]+)"', cdisp)
|
| 63 |
+
if match:
|
| 64 |
+
filename = match.group(1)
|
| 65 |
|
| 66 |
tmp_dir = Path(tempfile.gettempdir()) / "gaia_files"
|
| 67 |
+
tmp_dir.mkdir(parents=True, exist_ok=True)
|
| 68 |
file_path = tmp_dir / filename
|
| 69 |
+
|
| 70 |
with open(file_path, "wb") as f:
|
| 71 |
f.write(resp.content)
|
| 72 |
+
|
| 73 |
return str(file_path)
|
| 74 |
|
| 75 |
def create_agent():
|