Spaces:
Sleeping
Sleeping
Renzo
commited on
Commit
·
65822ef
1
Parent(s):
4f87fc6
Refactor agent.py to remove unused functions and streamline tool imports
Browse files
agent.py
CHANGED
@@ -1,22 +1,11 @@
|
|
1 |
-
import os
|
2 |
from textwrap import dedent
|
3 |
-
|
4 |
-
import httpx
|
5 |
from agno.agent import Agent
|
6 |
from agno.models.openai import OpenAIChat
|
7 |
-
from agno.tools.reasoning import ReasoningTools
|
8 |
-
from agno.tools.duckduckgo import DuckDuckGoTools
|
9 |
-
from agno.tools.wikipedia import WikipediaTools
|
10 |
from agno.models.xai import xAI
|
11 |
from agno.models.google import Gemini
|
12 |
from agno.models.openrouter import OpenRouter
|
13 |
|
14 |
-
|
15 |
-
BASE_STORAGE_ROOT = os.getenv("AGENT_STORAGE_ROOT", os.path.join(os.getcwd(), "agent_storage"))
|
16 |
-
|
17 |
-
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
18 |
-
|
19 |
-
test_question = "On June 6, 2023, an article by Carolyn Collins Petersen was published in Universe Today. This article mentions a team that produced a paper about their observations, linked at the bottom of the article. Find this paper. Under what NASA award number was the work performed by R. G. Arendt supported by?"
|
20 |
|
21 |
model = {
|
22 |
"grok": xAI(id="grok-3-beta"),
|
@@ -32,56 +21,6 @@ def get_current_date() -> str:
|
|
32 |
return "The current date is " + date
|
33 |
|
34 |
|
35 |
-
def get_file_from_task_id(task_id: str) -> str:
|
36 |
-
"""
|
37 |
-
Use this tool to **download** the file linked to a given `task_id`.
|
38 |
-
|
39 |
-
Args:
|
40 |
-
task_id (str): Identifier that points to the remote file.
|
41 |
-
|
42 |
-
Returns:
|
43 |
-
str: task_id to be used by other tools to read the file
|
44 |
-
"""
|
45 |
-
# ensure storage directory exists
|
46 |
-
task_dir = os.path.join(BASE_STORAGE_ROOT, task_id)
|
47 |
-
os.makedirs(task_dir, exist_ok=True)
|
48 |
-
|
49 |
-
# filename derived from task_id
|
50 |
-
filename = task_id
|
51 |
-
file_path = os.path.join(task_dir, filename)
|
52 |
-
|
53 |
-
# if file already exists, return
|
54 |
-
if os.path.exists(file_path):
|
55 |
-
print("[INFO] Using cached file:", file_path)
|
56 |
-
return file_path
|
57 |
-
|
58 |
-
# fetch content from remote
|
59 |
-
response = httpx.get(f"{DEFAULT_API_URL}/files/{task_id}", timeout=15, follow_redirects=True)
|
60 |
-
response.raise_for_status()
|
61 |
-
|
62 |
-
# write content to file
|
63 |
-
with open(file_path, "wb") as f:
|
64 |
-
f.write(response.content)
|
65 |
-
|
66 |
-
return file_path
|
67 |
-
|
68 |
-
|
69 |
-
def read_file_from_task_id(task_id: str) -> str:
|
70 |
-
"""
|
71 |
-
Args:
|
72 |
-
task_id (str): Identifier that points to the remote file.
|
73 |
-
|
74 |
-
Returns:
|
75 |
-
str: Content of the downloaded (or cached) file.
|
76 |
-
"""
|
77 |
-
# expected local path
|
78 |
-
file_path = os.path.join(BASE_STORAGE_ROOT, task_id, task_id)
|
79 |
-
|
80 |
-
with open(file_path, "r", encoding="utf-8") as f:
|
81 |
-
print("[INFO] Reading file:", file_path)
|
82 |
-
return f.read()
|
83 |
-
|
84 |
-
|
85 |
agent = Agent(
|
86 |
model=model["grok"],
|
87 |
markdown=False,
|
@@ -97,13 +36,7 @@ agent = Agent(
|
|
97 |
If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
98 |
The questions come in the following format: "<task_id>: <question>", you can use the task_id to retrieve attached files related to the question (only if available).
|
99 |
"""),
|
100 |
-
tools=
|
101 |
-
ReasoningTools(think=True, add_few_shot=True),
|
102 |
-
DuckDuckGoTools(fixed_max_results=5),
|
103 |
-
WikipediaTools(),
|
104 |
-
read_file_from_task_id,
|
105 |
-
get_file_from_task_id
|
106 |
-
],
|
107 |
context={"current_time": get_current_date},
|
108 |
add_context=True,
|
109 |
show_tool_calls=True
|
|
|
|
|
1 |
from textwrap import dedent
|
|
|
|
|
2 |
from agno.agent import Agent
|
3 |
from agno.models.openai import OpenAIChat
|
|
|
|
|
|
|
4 |
from agno.models.xai import xAI
|
5 |
from agno.models.google import Gemini
|
6 |
from agno.models.openrouter import OpenRouter
|
7 |
|
8 |
+
from tools import tools
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
model = {
|
11 |
"grok": xAI(id="grok-3-beta"),
|
|
|
21 |
return "The current date is " + date
|
22 |
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
agent = Agent(
|
25 |
model=model["grok"],
|
26 |
markdown=False,
|
|
|
36 |
If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
37 |
The questions come in the following format: "<task_id>: <question>", you can use the task_id to retrieve attached files related to the question (only if available).
|
38 |
"""),
|
39 |
+
tools=tools,
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
context={"current_time": get_current_date},
|
41 |
add_context=True,
|
42 |
show_tool_calls=True
|
tools.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
import httpx
|
4 |
+
from agno.tools.duckduckgo import DuckDuckGoTools
|
5 |
+
from agno.tools.reasoning import ReasoningTools
|
6 |
+
from agno.tools.wikipedia import WikipediaTools
|
7 |
+
|
8 |
+
BASE_STORAGE_ROOT = os.getenv("AGENT_STORAGE_ROOT", os.path.join(os.getcwd(), "agent_storage"))
|
9 |
+
|
10 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
11 |
+
|
12 |
+
|
13 |
+
def get_file_from_task_id(task_id: str) -> str:
|
14 |
+
"""
|
15 |
+
Use this tool to **download** the file linked to a given `task_id`.
|
16 |
+
|
17 |
+
Args:
|
18 |
+
task_id (str): Identifier that points to the remote file.
|
19 |
+
|
20 |
+
Returns:
|
21 |
+
str: task_id to be used by other tools to read the file
|
22 |
+
"""
|
23 |
+
# ensure storage directory exists
|
24 |
+
task_dir = os.path.join(BASE_STORAGE_ROOT, task_id)
|
25 |
+
os.makedirs(task_dir, exist_ok=True)
|
26 |
+
|
27 |
+
# filename derived from task_id
|
28 |
+
filename = task_id
|
29 |
+
file_path = os.path.join(task_dir, filename)
|
30 |
+
|
31 |
+
# if file already exists, return
|
32 |
+
if os.path.exists(file_path):
|
33 |
+
print("[INFO] Using cached file:", file_path)
|
34 |
+
return file_path
|
35 |
+
|
36 |
+
# fetch content from remote
|
37 |
+
response = httpx.get(f"{DEFAULT_API_URL}/files/{task_id}", timeout=15, follow_redirects=True)
|
38 |
+
response.raise_for_status()
|
39 |
+
|
40 |
+
# write content to file
|
41 |
+
with open(file_path, "wb") as f:
|
42 |
+
f.write(response.content)
|
43 |
+
|
44 |
+
return file_path
|
45 |
+
|
46 |
+
|
47 |
+
def read_file_from_task_id(task_id: str) -> str:
|
48 |
+
"""
|
49 |
+
Args:
|
50 |
+
task_id (str): Identifier that points to the remote file.
|
51 |
+
|
52 |
+
Returns:
|
53 |
+
str: Content of the downloaded (or cached) file.
|
54 |
+
"""
|
55 |
+
# expected local path
|
56 |
+
file_path = os.path.join(BASE_STORAGE_ROOT, task_id, task_id)
|
57 |
+
|
58 |
+
with open(file_path, "r", encoding="utf-8") as f:
|
59 |
+
print("[INFO] Reading file:", file_path)
|
60 |
+
return f.read()
|
61 |
+
|
62 |
+
|
63 |
+
tools = [
|
64 |
+
ReasoningTools(think=True, add_few_shot=True),
|
65 |
+
DuckDuckGoTools(fixed_max_results=5),
|
66 |
+
WikipediaTools(),
|
67 |
+
get_file_from_task_id,
|
68 |
+
read_file_from_task_id
|
69 |
+
]
|