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. :return: A list of question dictionaries, each with 'task_id' and 'question'. """ 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. :return: A dict with keys 'task_id' and '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. :param task_id: The ID of the GAIA task whose file you want to download. :return: The file content as raw bytes. """ 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. :param username: Your Hugging Face username (identifies the submission). :param agent_code: URL to your HF Space code (for verification). :param answers: List of dicts each with 'task_id' and 'submitted_answer'. :return: A dict containing 'score', 'correct_count', 'total_attempted', 'message', etc. """ 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() def create_agent() -> CodeAgent: """ Factory that returns a configured CodeAgent instance using OpenAI. Expects OPENAI_API_KEY in the environment. """ openai_model = OpenAIServerModel(model_name="gpt-3.5-turbo") agent = CodeAgent( tools=[fetch_questions, fetch_random_question, fetch_file, submit_answers], model=openai_model, prompt_template=( "Here is a GAIA question:\n" "{question}\n" "Provide ONLY the exact answer (exact-match) with no extra text." ) ) return agent