File size: 2,645 Bytes
378c064
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff95a9a
 
378c064
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff95a9a
 
378c064
 
 
ff95a9a
 
378c064
 
 
 
 
8476b8a
378c064
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
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