Frazer2810 commited on
Commit
395ccfe
Β·
verified Β·
1 Parent(s): 04ef9ec

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +22 -41
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, OpenAIModel
12
  from tools import WikipediaTool, ArxivTool
13
  import openai
14
 
15
- # ─── API key check ──────────────────────────────────────────────
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
- # ─── Prompt di sistema rigido (exact-match) ─────────────────────
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
- # ─── Costruzione del β€œcore” CodeAgent ───────────────────────────
33
- model = OpenAIModel(
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=model,
47
- tools=tools,
48
- max_steps=6, # previene loop infiniti
49
- scratchpad="minimal" # log conciso
50
- #stream_outputs=False
 
 
 
 
 
51
  )
52
 
53
- # ─── Thin wrapper usato da app.py ───────────────────────────────
54
- class BasicAgent: # (mantiene lo stesso nome giΓ  importato in app.py)
55
  def __init__(self):
56
- print("βœ… smolagents BasicAgent inizializzato")
57
 
58
  def __call__(self, question: str) -> str:
59
- """
60
- Esegue CodeAgent e restituisce SOLO la prima riga,
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]