File size: 3,462 Bytes
f9a7c9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5c9e62
f9a7c9b
a5c9e62
 
 
f9a7c9b
a5c9e62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9a7c9b
 
 
 
 
a5c9e62
 
 
 
 
 
 
f9a7c9b
a5c9e62
 
 
f9a7c9b
a5c9e62
f9a7c9b
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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 ''}")