Spaces:
Runtime error
Runtime error
import os | |
import requests | |
from smolagents import CodeAgent, tool, OpenAIServerModel | |
API_URL = "https://agents-course-unit4-scoring.hf.space" | |
def fetch_questions() -> list: | |
""" | |
Fetch the full list of GAIA evaluation questions. | |
Returns: | |
list: A list of question dicts, each with '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. | |
Returns: | |
dict: 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 submit_answers(username: str, agent_code: str, answers: list) -> dict: | |
""" | |
Submit the agent's answers to GAIA and get the scoring. | |
Args: | |
username (str): HF username for the submission. | |
agent_code (str): URL of your Space repo for verification. | |
answers (list): List of dicts, each with 'task_id' and 'submitted_answer'. | |
Returns: | |
dict: Contains '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: | |
""" | |
Build and return a configured CodeAgent using OpenAI GPT-3.5 Turbo, | |
with a few-shot system prompt tailored to GAIA Level-1 exact-match requirements. | |
Requires OPENAI_API_KEY in the environment. | |
""" | |
# 1. Instantiate model | |
model = OpenAIServerModel(model_id="gpt-3.5-turbo") | |
# 2. Create the agent with default prompt | |
agent = CodeAgent( | |
tools=[fetch_questions, fetch_random_question, submit_answers], | |
model=model | |
) | |
# 3. Override its system prompt to include paper’s instructions + few-shot | |
agent.system_prompt_template = """ | |
You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: | |
FINAL ANSWER: [YOUR FINAL ANSWER]. | |
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. | |
- If you are asked for a number, don't use commas, symbols or units (e.g. %, $, km) unless explicitly asked. | |
- If you are asked for a string, don't use articles ("a", "the"), abbreviations (e.g. "NYC"), or extra words; write digits in plain text unless specified otherwise. | |
- If you are asked for a comma separated list, apply the above rules to each element. | |
Example 1: | |
Question: What is 2 + 2? | |
Thought: simple arithmetic | |
FINAL ANSWER: 4 | |
Example 2: | |
Question: What is the capital of France? | |
Thought: common geography | |
FINAL ANSWER: Paris | |
Now it’s your turn. | |
Question: {task} | |
""" | |
return agent | |