File size: 1,989 Bytes
10e9b7d
eccf8e4
60f0482
 
 
 
 
 
 
 
 
 
 
 
31243f4
60f0482
31243f4
60f0482
 
 
e80aab9
60f0482
 
df283f3
60f0482
df283f3
60f0482
 
 
df283f3
60f0482
 
 
 
 
 
 
 
df283f3
60f0482
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e80aab9
60f0482
 
 
 
 
 
e80aab9
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
import os
import requests
from smolagents import CodeAgent, tool, OpenAIServerModel

# ------------------------
# Constants
# ------------------------
API_URL = "https://agents-course-unit4-scoring.hf.space"

# ------------------------
# Tool definitions
# ------------------------
@tool
def fetch_questions() -> list:
    """
    Fetch the full list of GAIA evaluation questions.
    """
    response = requests.get(f"{API_URL}/questions", timeout=15)
    response.raise_for_status()
    return response.json()

@tool
def fetch_random_question() -> dict:
    """
    Fetch a single random GAIA question.
    """
    response = requests.get(f"{API_URL}/random-question", timeout=15)
    response.raise_for_status()
    return response.json()

@tool
def fetch_file(task_id: str) -> bytes:
    """
    Download a file associated with a given task_id.
    """
    response = requests.get(f"{API_URL}/files/{task_id}", timeout=15)
    response.raise_for_status()
    return response.content

@tool
def submit_answers(username: str, agent_code: str, answers: list) -> dict:
    """
    Submit the agent's answers to GAIA and return the scoring.
    """
    payload = {
        "username": username,
        "agent_code": agent_code,
        "answers": answers
    }
    response = requests.post(f"{API_URL}/submit", json=payload, timeout=60)
    response.raise_for_status()
    return response.json()

# ------------------------
# Agent factory
# ------------------------
def create_agent() -> CodeAgent:
    """
    Factory that returns a configured CodeAgent instance.
    Requires OPENAI_API_KEY in environment.
    """
    # Initialize the LLM with OpenAI API
    llm = OpenAIServerModel(
        model_id=os.getenv("OPENAI_MODEL_ID", "gpt-3.5-turbo"),
        api_key=os.getenv("OPENAI_API_KEY")
    )
    # Create agent with defined tools
    agent = CodeAgent(
        tools=[fetch_questions, fetch_random_question, fetch_file, submit_answers],
        model=llm
    )
    return agent