Yago Bolivar commited on
Commit
64a3746
·
1 Parent(s): de9051c

ref(app.py, prompts.yaml, final_answer_tool.py): adapt the code to submission requirements

Browse files
Files changed (3) hide show
  1. app.py +12 -2
  2. prompts.yaml +8 -1
  3. src/final_answer_tool.py +59 -4
app.py CHANGED
@@ -244,11 +244,21 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
244
 
245
  try:
246
  submitted_answer = agent(question_text)
 
 
 
 
 
 
 
 
 
 
247
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
248
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
249
  except Exception as e:
250
- print(f"Error running agent on task {task_id}: {e}")
251
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
252
 
253
  if not answers_payload:
254
  print("Agent did not produce any answers to submit.")
 
244
 
245
  try:
246
  submitted_answer = agent(question_text)
247
+ # Ensure submitted_answer is a simple string/number/float
248
+ if isinstance(submitted_answer, dict):
249
+ # Extract meaningful value or convert to string
250
+ if len(submitted_answer) == 1:
251
+ submitted_answer = list(submitted_answer.values())[0]
252
+ else:
253
+ submitted_answer = str(submitted_answer)
254
+ elif not isinstance(submitted_answer, (str, int, float)):
255
+ submitted_answer = str(submitted_answer)
256
+
257
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
258
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
259
  except Exception as e:
260
+ print(f"Error running agent on task {task_id}: {e}")
261
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
262
 
263
  if not answers_payload:
264
  print("Agent did not produce any answers to submit.")
prompts.yaml CHANGED
@@ -1,6 +1,6 @@
1
  system_prompt:
2
  main: |-
3
- You are an expert assistant who can solve any task using code blobs. You will be given a task to solve as best you can.
4
  To do so, you have been given access to a list of tools: these tools are basically Python functions which you can call with code.
5
  To solve the task, you must plan forward to proceed in a series of steps, in a cycle of 'Thought:', 'Code:', and 'Observation:' sequences.
6
 
@@ -10,6 +10,13 @@ system_prompt:
10
  These print outputs will then appear in the 'Observation:' field, which will be available as input for the next step.
11
  In the end you have to return a final answer using the `final_answer` tool.
12
 
 
 
 
 
 
 
 
13
  You have access to these tools:
14
  {%- for tool in tools.values() %}
15
  - {{ tool.name }}: {{ tool.description }}
 
1
  system_prompt:
2
  main: |-
3
+ You are a general AI assistant who can solve any task using code blobs. You will be given a task to solve as best you can.
4
  To do so, you have been given access to a list of tools: these tools are basically Python functions which you can call with code.
5
  To solve the task, you must plan forward to proceed in a series of steps, in a cycle of 'Thought:', 'Code:', and 'Observation:' sequences.
6
 
 
10
  These print outputs will then appear in the 'Observation:' field, which will be available as input for the next step.
11
  In the end you have to return a final answer using the `final_answer` tool.
12
 
13
+ IMPORTANT FORMATTING RULES for final answers:
14
+ - YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings
15
+ - If asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise
16
+ - If asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise
17
+ - If 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
18
+ - Return ONLY the direct answer, not a dictionary or complex object
19
+
20
  You have access to these tools:
21
  {%- for tool in tools.values() %}
22
  - {{ tool.name }}: {{ tool.description }}
src/final_answer_tool.py CHANGED
@@ -1,14 +1,69 @@
1
  from typing import Any, Optional
2
  from smolagents.tools import Tool
 
3
 
4
  class FinalAnswerTool(Tool):
5
  name = "final_answer"
6
- description = "Provides a final answer to the given problem."
7
  inputs = {'answer': {'type': 'any', 'description': 'The final answer to the problem'}}
8
  output_type = "any"
9
 
10
- def forward(self, answer: Any) -> Any:
11
- return answer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  def __init__(self, *args, **kwargs):
14
- self.is_initialized = False
 
 
1
  from typing import Any, Optional
2
  from smolagents.tools import Tool
3
+ import re
4
 
5
  class FinalAnswerTool(Tool):
6
  name = "final_answer"
7
+ description = "Provides a final answer to the given problem in GAIA benchmark format."
8
  inputs = {'answer': {'type': 'any', 'description': 'The final answer to the problem'}}
9
  output_type = "any"
10
 
11
+ def forward(self, answer: Any) -> str:
12
+ """
13
+ Process the answer to ensure it follows GAIA benchmark formatting rules.
14
+ Returns a clean string that matches expected format.
15
+ """
16
+ # Convert complex objects to simple strings
17
+ if isinstance(answer, dict):
18
+ # Try to extract meaningful value from dictionary
19
+ if len(answer) == 1:
20
+ answer = list(answer.values())[0]
21
+ elif 'answer' in answer:
22
+ answer = answer['answer']
23
+ elif 'result' in answer:
24
+ answer = answer['result']
25
+ elif 'value' in answer:
26
+ answer = answer['value']
27
+ else:
28
+ # Join values as comma-separated list
29
+ values = [str(v) for v in answer.values() if v is not None]
30
+ answer = ", ".join(values)
31
+
32
+ elif isinstance(answer, list):
33
+ # Convert list to comma-separated string
34
+ answer = ", ".join(str(item) for item in answer if item is not None)
35
+
36
+ # Convert to string and apply GAIA formatting rules
37
+ answer_str = str(answer).strip()
38
+
39
+ # Remove common formatting issues
40
+ answer_str = self._clean_gaia_format(answer_str)
41
+
42
+ return answer_str
43
+
44
+ def _clean_gaia_format(self, text: str) -> str:
45
+ """Apply GAIA benchmark formatting rules."""
46
+ # Remove "FINAL ANSWER:" prefix if present
47
+ text = re.sub(r'^(FINAL\s*ANSWER\s*:\s*)', '', text, flags=re.IGNORECASE).strip()
48
+
49
+ # Remove quotes if they wrap the entire answer
50
+ if (text.startswith('"') and text.endswith('"')) or (text.startswith("'") and text.endswith("'")):
51
+ text = text[1:-1]
52
+
53
+ # Remove articles for strings (a, an, the) at the beginning
54
+ text = re.sub(r'^(a|an|the)\s+', '', text, flags=re.IGNORECASE)
55
+
56
+ # Remove units symbols unless they might be part of the answer
57
+ # Be conservative - only remove obvious currency and percent
58
+ if not any(char.isalpha() for char in text.replace('$', '').replace('%', '')):
59
+ text = text.replace('$', '').replace('%', '')
60
+
61
+ # Remove commas from numbers (but not from lists)
62
+ if re.match(r'^\d{1,3}(,\d{3})+(\.\d+)?$', text):
63
+ text = text.replace(',', '')
64
+
65
+ return text.strip()
66
 
67
  def __init__(self, *args, **kwargs):
68
+ super().__init__(*args, **kwargs)
69
+ self.is_initialized = True