Frazer2810's picture
Update agent.py
8476b8a verified
raw
history blame
2.65 kB
"""
GAIA Level-1 agent powered by smolagents.
* Planner/esecutore: CodeAgent (smolagents)
* LLM backend : GPT-4.1 via OpenAI
* Tools : DuckDuckGo (builtin), WikipediaTool, ArxivTool
* Output : UNA sola riga (exact-match)
"""
from __future__ import annotations
import os, textwrap
from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIModel
from tools import WikipediaTool, ArxivTool
import openai
# ─── API key check ──────────────────────────────────────────────
openai.api_key = os.getenv("OPENAI_API_KEY") or ""
if not openai.api_key:
raise EnvironmentError(
"OPENAI_API_KEY non impostata: aggiungila nei Secrets dello Space "
"o in un file .env locale."
)
# ─── Prompt di sistema rigido (exact-match) ─────────────────────
SYSTEM_PROMPT = textwrap.dedent("""
You are a helpful assistant tasked with answering questions using a set of tools.
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 comma to write your number neither use units such as $ or percent sign unless specified otherwise.
If you are asked for a string, don't use articles, neither abbreviations, and write digits in plain text unless specified otherwise.
Return ONLY the final answer line.
""").strip()
# ─── Costruzione del β€œcore” CodeAgent ───────────────────────────
model = OpenAIModel(
model_id="gpt-4.1",
temperature=0,
system_prompt=SYSTEM_PROMPT
)
tools = [
DuckDuckGoSearchTool(), # incorporato in smolagents
WikipediaTool(),
ArxivTool(),
]
core_agent = CodeAgent(
model=model,
tools=tools,
max_steps=6, # previene loop infiniti
scratchpad="minimal" # log conciso
#stream_outputs=False
)
# ─── Thin wrapper usato da app.py ───────────────────────────────
class BasicAgent: # (mantiene lo stesso nome giΓ  importato in app.py)
def __init__(self):
print("βœ… smolagents BasicAgent inizializzato")
def __call__(self, question: str) -> str:
"""
Esegue CodeAgent e restituisce SOLO la prima riga,
così il grader riceve una stringa exact-match.
"""
raw_answer: str = core_agent.run(question)
answer = raw_answer.strip().split("\n", 1)[0]
print(f"[ANSWER] {answer}")
return answer