Spaces:
Runtime error
Runtime error
import os | |
import requests | |
from smolagents import CodeAgent, tool, OpenAIServerModel | |
# ------------------------ | |
# Constants | |
# ------------------------ | |
API_URL = "https://agents-course-unit4-scoring.hf.space" | |
# ------------------------ | |
# Tool definitions | |
# ------------------------ | |
def fetch_questions() -> list: | |
""" | |
Fetch the full list of GAIA evaluation questions. | |
:return: List of question dicts, each containing 'task_id' and 'question'. | |
""" | |
resp = requests.get(f"{API_URL}/questions", timeout=15) | |
resp.raise_for_status() | |
return resp.json() | |
def fetch_random_question() -> dict: | |
""" | |
Fetch a single random GAIA question. | |
:return: A dict with keys 'task_id' and 'question'. | |
""" | |
resp = requests.get(f"{API_URL}/random-question", timeout=15) | |
resp.raise_for_status() | |
return resp.json() | |
def fetch_file(task_id: str) -> bytes: | |
""" | |
Download a file associated with a given GAIA task. | |
:param task_id: The GAIA task ID whose file should be downloaded. | |
:return: Raw bytes of the file content. | |
""" | |
resp = requests.get(f"{API_URL}/files/{task_id}", timeout=15) | |
resp.raise_for_status() | |
return resp.content | |
def submit_answers( | |
username: str, | |
agent_code: str, | |
answers: list | |
) -> dict: | |
""" | |
Submit the agent's answers to the GAIA API and retrieve scoring results. | |
:param username: Hugging Face username for submission identification. | |
:param agent_code: URL linking to the code repository of this Space. | |
:param answers: List of dicts, each with 'task_id' and 'submitted_answer'. | |
:return: Dict with keys 'username', 'score', 'correct_count', 'total_attempted', 'message', etc. | |
""" | |
payload = { | |
"username": username, | |
"agent_code": agent_code, | |
"answers": answers | |
} | |
resp = requests.post(f"{API_URL}/submit", json=payload, timeout=60) | |
resp.raise_for_status() | |
return resp.json() | |
def create_agent() -> CodeAgent: | |
""" | |
Factory that initializes and returns a configured CodeAgent using OpenAI GPT-3.5-turbo. | |
Expects OPENAI_API_KEY set in the environment. | |
:return: Configured CodeAgent instance. | |
""" | |
model = OpenAIServerModel(model_name="gpt-3.5-turbo") | |
agent = CodeAgent( | |
tools=[fetch_questions, fetch_random_question, fetch_file, submit_answers], | |
model=model, | |
prompt_template=( | |
"Here is a GAIA question:\n" | |
"{question}\n" | |
"Provide only the exact answer (exact-match), with no additional explanation." | |
) | |
) | |
return agent | |