Sauten commited on
Commit
796c04e
·
verified ·
1 Parent(s): 25e6cf1

Create tool_fetch_task_file.py

Browse files
Files changed (1) hide show
  1. tool_fetch_task_file.py +42 -0
tool_fetch_task_file.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents import tool
2
+ import os
3
+ import requests
4
+
5
+ DEFAULT_API_URL = os.getenv("AGENT_API_URL", "https://agents-course-unit4-scoring.hf.space")
6
+
7
+ @tool
8
+ def fetch_task_file(task_id: str) -> dict:
9
+ """
10
+ Fetches the file associated with a task ID using the API URL from env.
11
+
12
+ Args:
13
+ task_id: The task ID to fetch.
14
+
15
+ Returns:
16
+ dict: task_id, content, status
17
+ """
18
+ full_url = f"{DEFAULT_API_URL}/files/{task_id}"
19
+ print(f"📥 Tool:fetch_task_file requesting {full_url}")
20
+
21
+ try:
22
+ response = requests.get(full_url, timeout=10)
23
+
24
+ if response.status_code == 200:
25
+ return {
26
+ "task_id": task_id,
27
+ "content": response.text[:5000],
28
+ "status": "Success"
29
+ }
30
+
31
+ return {
32
+ "task_id": task_id,
33
+ "content": "",
34
+ "status": f"{response.status_code} - {response.reason}"
35
+ }
36
+
37
+ except Exception as e:
38
+ return {
39
+ "task_id": task_id,
40
+ "content": "",
41
+ "status": f"Error: {str(e)}"
42
+ }