vallurimahanth commited on
Commit
f009b91
·
verified ·
1 Parent(s): 98a5928

Update core_agent.py

Browse files
Files changed (1) hide show
  1. core_agent.py +79 -1
core_agent.py CHANGED
@@ -301,8 +301,86 @@ class GAIAAgent:
301
  import os
302
  file_ext=os.path.splitext(task_file_path)[1].lower()
303
  context=f"""Question: {question} This question has an associated file. Here is the file content:```{file_ext} {file_content}``` Analyze the file content above to answer the question."""
304
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
306
 
307
 
308
 
 
301
  import os
302
  file_ext=os.path.splitext(task_file_path)[1].lower()
303
  context=f"""Question: {question} This question has an associated file. Here is the file content:```{file_ext} {file_content}``` Analyze the file content above to answer the question."""
304
+ except Exception as file_e:
305
+ context = f"""Question: {question} This question has an associated file at path: {task_file_path}However, there was an error reading the file: {file_e}You can still try to answer the question based on the information provided."""
306
+
307
+ # Add a prompt to ensure precise answers
308
+ full_prompt = f"""{context}
309
+ When answering, provide ONLY the precise answer requested.
310
+ Do not include explanations, steps, reasoning, or additional text.
311
+ Be direct and specific. GAIA benchmark requires exact matching answers.
312
+ For example, if asked "What is the capital of France?", respond simply with "Paris".
313
+ """
314
+ answer = self.agent.run(full_prompt)
315
+ answer = self._clean_answer(answer)
316
+
317
+ if self.verbose:
318
+ print(f"Generated answer: {answer}")
319
+
320
+ return answer
321
+
322
+ except Exception as e:
323
+ error_msg = f"Error answering question: {e}"
324
+ if self.verbose:
325
+ print(error_msg)
326
+ return error_msg
327
+
328
+ def _clean_answer(self, answer: any) -> str:
329
+ """
330
+ Clean up the answer to remove common prefixes and formatting
331
+ that models often add but that can cause exact match failures.
332
+
333
+ Args:
334
+ answer: The raw answer from the model
335
 
336
+ Returns:
337
+ The cleaned answer as a string
338
+ """
339
+ # Convert non-string types to strings
340
+ if not isinstance(answer, str):
341
+ # Handle numeric types (float, int)
342
+ if isinstance(answer, float):
343
+ # Format floating point numbers properly
344
+ # Check if it's an integer value in float form (e.g., 12.0)
345
+ if answer.is_integer():
346
+ formatted_answer = str(int(answer))
347
+ else:
348
+ # For currency values that might need formatting
349
+ if abs(answer) >= 1000:
350
+ formatted_answer = f"${answer:,.2f}"
351
+ else:
352
+ formatted_answer = str(answer)
353
+ return formatted_answer
354
+ elif isinstance(answer, int):
355
+ return str(answer)
356
+ else:
357
+ # For any other type
358
+ return str(answer)
359
+
360
+ # Now we know answer is a string, so we can safely use string methods
361
+ # Normalize whitespace
362
+ answer = answer.strip()
363
+
364
+ # Remove common prefixes and formatting that models add
365
+ prefixes_to_remove = [
366
+ "The answer is ",
367
+ "Answer: ",
368
+ "Final answer: ",
369
+ "The result is ",
370
+ "To answer this question: ",
371
+ "Based on the information provided, ",
372
+ "According to the information: ",
373
+ ]
374
+
375
+ for prefix in prefixes_to_remove:
376
+ if answer.startswith(prefix):
377
+ answer = answer[len(prefix):].strip()
378
+
379
+ # Remove quotes if they wrap the entire answer
380
+ if (answer.startswith('"') and answer.endswith('"')) or (answer.startswith("'") and answer.endswith("'")):
381
+ answer = answer[1:-1].strip()
382
+
383
+ return answer
384
 
385
 
386