Ubik80's picture
Update agent.py
179d569 verified
raw
history blame
2.1 kB
# agent.py
import os
import requests
from smolagents import CodeAgent, tool, OpenAIServerModel
API_URL = "https://agents-course-unit4-scoring.hf.space"
@tool
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()
@tool
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()
@tool
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.
Requires OPENAI_API_KEY in the environment.
"""
model = OpenAIServerModel(model_id="gpt-3.5-turbo")
agent = CodeAgent(
tools=[fetch_questions, fetch_random_question, submit_answers],
model=model,
# Use `system_prompt`, not `prompt_template`
system_prompt=(
"You are a concise assistant.\n"
"Here is a GAIA question:\n"
"{task}\n"
"Respond with only the exact answer (exact-match), no extra text.\n"
)
)
return agent