Renzo commited on
Commit
4f87fc6
·
1 Parent(s): 2e527e0

tool: get and read tools

Browse files
Files changed (4) hide show
  1. .gitignore +2 -1
  2. agent.py +74 -6
  3. app.py +5 -3
  4. requirements.txt +2 -1
.gitignore CHANGED
@@ -1,4 +1,5 @@
1
  .venv
2
  .idea
3
  .env
4
- questions.txt
 
 
1
  .venv
2
  .idea
3
  .env
4
+ questions.txt
5
+ /agent_storage/
agent.py CHANGED
@@ -1,3 +1,7 @@
 
 
 
 
1
  from agno.agent import Agent
2
  from agno.models.openai import OpenAIChat
3
  from agno.tools.reasoning import ReasoningTools
@@ -7,6 +11,11 @@ from agno.models.xai import xAI
7
  from agno.models.google import Gemini
8
  from agno.models.openrouter import OpenRouter
9
 
 
 
 
 
 
10
  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?"
11
 
12
  model = {
@@ -23,20 +32,79 @@ def get_current_date() -> str:
23
  return "The current date is " + date
24
 
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  agent = Agent(
27
- model=model["open_router"],
28
  markdown=False,
29
  debug_mode=True,
30
- instructions="You are a general AI assistant. I will ask you a question. Report your thoughts, but your final answer must be only the answer itself, with nothing else. The answer should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. 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.",
 
 
 
 
 
 
 
 
 
 
31
  tools=[
32
  ReasoningTools(think=True, add_few_shot=True),
33
  DuckDuckGoTools(fixed_max_results=5),
34
- WikipediaTools()
 
 
35
  ],
36
  context={"current_time": get_current_date},
37
  add_context=True,
38
  show_tool_calls=True
39
  )
40
-
41
- # Print the response in the terminal
42
- # agent.print_response("Waht is greater 9.9 or 9.11?", strem=True)
 
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
 
11
  from agno.models.google import Gemini
12
  from agno.models.openrouter import OpenRouter
13
 
14
+ # configurable storage root for downloaded files
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 = {
 
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,
88
  debug_mode=True,
89
+ instructions=dedent(
90
+ """
91
+ You are a general AI assistant. I will ask you a question.
92
+ Report your thoughts, but your final answer must be only the answer itself, with nothing else.
93
+ The answer should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
94
+ If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise.
95
+ If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities),
96
+ and write the digits in plain text unless specified otherwise.
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
110
  )
 
 
 
app.py CHANGED
@@ -19,8 +19,9 @@ class BasicAgent:
19
  def __init__(self):
20
  pass
21
 
22
- def __call__(self, question: str) -> str:
23
- return asyncio.run(_async_answer(question))
 
24
 
25
 
26
  def run_agent(profile: gr.OAuthProfile | None, task_id: str | None = None, submit: bool = True):
@@ -33,6 +34,7 @@ def run_agent(profile: gr.OAuthProfile | None, task_id: str | None = None, submi
33
  api_url = DEFAULT_API_URL
34
  questions_url = f"{api_url}/questions"
35
  submit_url = f"{api_url}/submit"
 
36
 
37
  try:
38
  agent_instance = BasicAgent()
@@ -61,7 +63,7 @@ def run_agent(profile: gr.OAuthProfile | None, task_id: str | None = None, submi
61
  if not tid or qtext is None:
62
  continue
63
  try:
64
- submitted_answer = agent_instance(qtext)
65
  answers_payload.append({"task_id": tid, "submitted_answer": submitted_answer})
66
  results_log.append({"Task ID": tid, "Question": qtext, "Submitted Answer": submitted_answer})
67
  except Exception as e:
 
19
  def __init__(self):
20
  pass
21
 
22
+ def __call__(self, task_id: str, question: str) -> str:
23
+ print("[INFO] Answering question: >>>", question)
24
+ return asyncio.run(_async_answer(f"{task_id}: {question}"))
25
 
26
 
27
  def run_agent(profile: gr.OAuthProfile | None, task_id: str | None = None, submit: bool = True):
 
34
  api_url = DEFAULT_API_URL
35
  questions_url = f"{api_url}/questions"
36
  submit_url = f"{api_url}/submit"
37
+ files_url = f"{api_url}/files/{task_id}"
38
 
39
  try:
40
  agent_instance = BasicAgent()
 
63
  if not tid or qtext is None:
64
  continue
65
  try:
66
+ submitted_answer = agent_instance(task_id, qtext)
67
  answers_payload.append({"task_id": tid, "submitted_answer": submitted_answer})
68
  results_log.append({"Task ID": tid, "Question": qtext, "Submitted Answer": submitted_answer})
69
  except Exception as e:
requirements.txt CHANGED
@@ -5,4 +5,5 @@ openai
5
  duckduckgo-search
6
  wikipedia
7
  google
8
- google-genai
 
 
5
  duckduckgo-search
6
  wikipedia
7
  google
8
+ google-genai
9
+ httpx