Update agent.py
Browse files
agent.py
CHANGED
|
@@ -1,26 +1,18 @@
|
|
| 1 |
"""
|
| 2 |
-
GAIA Level-1 agent powered by smolagents.
|
| 3 |
-
|
| 4 |
-
* Planner/esecutore: CodeAgent (smolagents)
|
| 5 |
-
* LLM backend : GPT-4.1 via OpenAI
|
| 6 |
-
* Tools : DuckDuckGo (builtin), WikipediaTool, ArxivTool
|
| 7 |
-
* Output : UNA sola riga (exact-match)
|
| 8 |
"""
|
| 9 |
from __future__ import annotations
|
| 10 |
import os, textwrap
|
| 11 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool
|
| 12 |
from tools import WikipediaTool, ArxivTool
|
| 13 |
import openai
|
| 14 |
|
| 15 |
-
# βββ API key
|
| 16 |
openai.api_key = os.getenv("OPENAI_API_KEY") or ""
|
| 17 |
if not openai.api_key:
|
| 18 |
-
raise EnvironmentError(
|
| 19 |
-
"OPENAI_API_KEY non impostata: aggiungila nei Secrets dello Space "
|
| 20 |
-
"o in un file .env locale."
|
| 21 |
-
)
|
| 22 |
|
| 23 |
-
# βββ
|
| 24 |
SYSTEM_PROMPT = textwrap.dedent("""
|
| 25 |
You are a helpful assistant tasked with answering questions using a set of tools.
|
| 26 |
Your final answer should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
|
|
@@ -29,38 +21,27 @@ If you are asked for a string, don't use articles, neither abbreviations, and wr
|
|
| 29 |
Return ONLY the final answer line.
|
| 30 |
""").strip()
|
| 31 |
|
| 32 |
-
# βββ
|
| 33 |
-
|
| 34 |
-
model_id="gpt-4.1",
|
| 35 |
-
temperature=0,
|
| 36 |
-
system_prompt=SYSTEM_PROMPT
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
tools = [
|
| 40 |
-
DuckDuckGoSearchTool(), # incorporato in smolagents
|
| 41 |
-
WikipediaTool(),
|
| 42 |
-
ArxivTool(),
|
| 43 |
-
]
|
| 44 |
|
| 45 |
core_agent = CodeAgent(
|
| 46 |
-
model=
|
| 47 |
-
tools=
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
)
|
| 52 |
|
| 53 |
-
# βββ Thin wrapper
|
| 54 |
-
class BasicAgent:
|
| 55 |
def __init__(self):
|
| 56 |
-
print("β
smolagents BasicAgent
|
| 57 |
|
| 58 |
def __call__(self, question: str) -> str:
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
così il grader riceve una stringa exact-match.
|
| 62 |
-
"""
|
| 63 |
-
raw_answer: str = core_agent.run(question)
|
| 64 |
-
answer = raw_answer.strip().split("\n", 1)[0]
|
| 65 |
-
print(f"[ANSWER] {answer}")
|
| 66 |
-
return answer
|
|
|
|
| 1 |
"""
|
| 2 |
+
GAIA Level-1 agent powered by smolagents β₯ 0.6
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
"""
|
| 4 |
from __future__ import annotations
|
| 5 |
import os, textwrap
|
| 6 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool
|
| 7 |
from tools import WikipediaTool, ArxivTool
|
| 8 |
import openai
|
| 9 |
|
| 10 |
+
# βββ API key βββββββββββββββββββββββββββββββββββββββ
|
| 11 |
openai.api_key = os.getenv("OPENAI_API_KEY") or ""
|
| 12 |
if not openai.api_key:
|
| 13 |
+
raise EnvironmentError("OPENAI_API_KEY non trovata β aggiungila nei Secrets dello Space.")
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# βββ System prompt (exact-match) βββββββββββββββββββ
|
| 16 |
SYSTEM_PROMPT = textwrap.dedent("""
|
| 17 |
You are a helpful assistant tasked with answering questions using a set of tools.
|
| 18 |
Your final answer should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
|
|
|
|
| 21 |
Return ONLY the final answer line.
|
| 22 |
""").strip()
|
| 23 |
|
| 24 |
+
# βββ Build CodeAgent βββββββββββββββββββββββββββββββ
|
| 25 |
+
MODEL_ID = os.getenv("GAIA_MODEL_ID", "gpt-4o") # override se hai βgpt-4.1β
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
core_agent = CodeAgent(
|
| 28 |
+
model=MODEL_ID,
|
| 29 |
+
tools=[
|
| 30 |
+
DuckDuckGoSearchTool(),
|
| 31 |
+
WikipediaTool(),
|
| 32 |
+
ArxivTool(),
|
| 33 |
+
],
|
| 34 |
+
max_steps=6,
|
| 35 |
+
temperature=0,
|
| 36 |
+
system_prompt=SYSTEM_PROMPT,
|
| 37 |
+
stream_outputs=False,
|
| 38 |
)
|
| 39 |
|
| 40 |
+
# βββ Thin wrapper per app.py βββββββββββββββββββββββ
|
| 41 |
+
class BasicAgent:
|
| 42 |
def __init__(self):
|
| 43 |
+
print("β
smolagents BasicAgent avviato")
|
| 44 |
|
| 45 |
def __call__(self, question: str) -> str:
|
| 46 |
+
raw = core_agent.run(question)
|
| 47 |
+
return raw.strip().split("\n", 1)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|