Spaces:
Sleeping
Sleeping
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. | |
Args: | |
None | |
Returns: | |
list: A list of question dicts, each with 'task_id' and 'question'. | |
""" | |
response = requests.get(f"{API_URL}/questions", timeout=15) | |
response.raise_for_status() | |
return response.json() | |
def fetch_random_question() -> dict: | |
""" | |
Fetch a single random GAIA question. | |
Args: | |
None | |
Returns: | |
dict: A dict containing 'task_id' and 'question'. | |
""" | |
response = requests.get(f"{API_URL}/random-question", timeout=15) | |
response.raise_for_status() | |
return response.json() | |
def submit_answers( | |
username: str, | |
agent_code: str, | |
answers: list | |
) -> dict: | |
""" | |
Submit the agent's answers to GAIA and retrieve scoring. | |
Args: | |
username (str): The Hugging Face username identifying the submission. | |
agent_code (str): URL to your Space code repository for verification purposes. | |
answers (list): List of dicts, each with 'task_id' and 'submitted_answer'. | |
Returns: | |
dict: Contains 'score', 'correct_count', 'total_attempted', 'message', and other metadata. | |
""" | |
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: | |
""" | |
Build and return a configured CodeAgent using OpenAI GPT-3.5 Turbo. | |
Requires the environment variable OPENAI_API_KEY to be set. | |
Args: | |
None | |
Returns: | |
CodeAgent: A SmolAgents CodeAgent instance with configured tools and prompt. | |
""" | |
model = OpenAIServerModel(model_name="gpt-3.5-turbo") | |
agent = CodeAgent( | |
tools=[fetch_questions, fetch_random_question, submit_answers], | |
model=model, | |
prompt_template=( | |
"Here is a GAIA question:\n" | |
"{question}\n" | |
"Provide ONLY the exact answer (exact-match), with no extra text." | |
) | |
) | |
return agent | |