import requests import json from typing import List, Dict, Any def fetch_questions(api_url: str = "https://agents-course-unit4-scoring.hf.space") -> List[Dict[str, Any]]: """Fetch all questions from the GAIA API.""" try: response = requests.get(f"{api_url}/questions", timeout=15) response.raise_for_status() return response.json() except Exception as e: print(f"Error fetching questions: {e}") return [] def fetch_random_question(api_url: str = "https://agents-course-unit4-scoring.hf.space") -> Dict[str, Any]: """Fetch a random question from the GAIA API.""" try: response = requests.get(f"{api_url}/random-question", timeout=15) response.raise_for_status() return response.json() except Exception as e: print(f"Error fetching random question: {e}") return {} def submit_answers(username: str, agent_code: str, answers: List[Dict[str, str]], api_url: str = "https://agents-course-unit4-scoring.hf.space") -> Dict[str, Any]: """Submit answers to the GAIA API for scoring.""" try: submission_data = { "username": username.strip(), "agent_code": agent_code, "answers": answers } response = requests.post(f"{api_url}/submit", json=submission_data, timeout=60) response.raise_for_status() return response.json() except Exception as e: print(f"Error submitting answers: {e}") return {"error": str(e)} def format_gaia_answer(raw_answer: str) -> str: """Format the agent's raw answer for GAIA submission (exact match).""" import re # Look for FINAL ANSWER: pattern (case insensitive) final_answer_pattern = r'FINAL ANSWER:\s*(.+?)(?:\n|$)' match = re.search(final_answer_pattern, raw_answer, re.IGNORECASE | re.DOTALL) if match: answer = match.group(1).strip() else: # Fallback: try to extract from common patterns fallback_patterns = [ r'(?:The\s+)?(?:final\s+)?answer\s+is:?\s*(.+?)(?:\n|$)', r'(?:Answer|Result):\s*(.+?)(?:\n|$)', ] answer = raw_answer.strip() for pattern in fallback_patterns: match = re.search(pattern, answer, re.IGNORECASE) if match: answer = match.group(1).strip() break # Apply GAIA formatting rules answer = answer.strip() # Remove trailing punctuation that might not be in ground truth while answer and answer[-1] in '.!?': answer = answer[:-1].strip() # Remove quotes if they wrap the entire answer if len(answer) >= 2 and answer[0] == answer[-1] and answer[0] in '"\'': answer = answer[1:-1].strip() # Additional cleanup for common issues # Remove "approximately" or similar qualifiers answer = re.sub(r'^(?:approximately|about|roughly|around)\s+', '', answer, flags=re.IGNORECASE) # For numbers, ensure no commas (as per GAIA rules) if re.match(r'^[\d,]+(?:\.\d+)?$', answer): answer = answer.replace(',', '') return answer def log_agent_step(step: str, result: str, step_number: int = None): """Log agent execution steps for debugging.""" prefix = f"Step {step_number}: " if step_number else "" print(f"\nšŸ¤– {prefix}{step}") print(f"šŸ“ Result: {result[:200]}{'...' if len(result) > 200 else ''}")