Final-Assignment / tool_fetch_task_file.py
Sauten's picture
Create tool_fetch_task_file.py
796c04e verified
from smolagents import tool
import os
import requests
DEFAULT_API_URL = os.getenv("AGENT_API_URL", "https://agents-course-unit4-scoring.hf.space")
@tool
def fetch_task_file(task_id: str) -> dict:
"""
Fetches the file associated with a task ID using the API URL from env.
Args:
task_id: The task ID to fetch.
Returns:
dict: task_id, content, status
"""
full_url = f"{DEFAULT_API_URL}/files/{task_id}"
print(f"📥 Tool:fetch_task_file requesting {full_url}")
try:
response = requests.get(full_url, timeout=10)
if response.status_code == 200:
return {
"task_id": task_id,
"content": response.text[:5000],
"status": "Success"
}
return {
"task_id": task_id,
"content": "",
"status": f"{response.status_code} - {response.reason}"
}
except Exception as e:
return {
"task_id": task_id,
"content": "",
"status": f"Error: {str(e)}"
}