Balabrahmam7 commited on
Commit
03da455
·
verified ·
1 Parent(s): 03d9b2d

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +15 -16
agent.py CHANGED
@@ -7,7 +7,8 @@ from pathlib import Path
7
  from typing import Optional
8
 
9
  from dotenv import load_dotenv
10
- from smolagents import ToolCallingAgent, tool, LiteLLMModel
 
11
 
12
  # Import your custom logic
13
  from tools import (
@@ -44,8 +45,6 @@ class RateLimitedModel(LiteLLMModel):
44
  )
45
 
46
  # ----------- 2. smolagents Tool Definitions -----------
47
- # smolagents relies heavily on type hints and docstrings to build the system prompt.
48
-
49
  @tool
50
  def enhanced_web_search(query: str) -> str:
51
  """Enhanced web search with intelligent query processing. Use for recent/broad web info.
@@ -105,7 +104,6 @@ def extract_youtube(url: str) -> str:
105
  return extract_youtube_info(url)
106
 
107
  # ----------- 3. Enhanced File Processing -----------
108
- # (Kept exactly as your original logic, as it accurately handles metadata and caching)
109
  def detect_file_type(file_path: str) -> Optional[str]:
110
  ext = Path(file_path).suffix.lower()
111
  file_type_mapping = {
@@ -163,19 +161,16 @@ def process_file(task_id: str, question_text: str) -> str:
163
 
164
  # ----------- 4. Agent Class -----------
165
  class GaiaAgent:
166
- """GAIA Agent powered by smolagents"""
167
 
168
  def __init__(self):
169
- # Using LiteLLM format.
170
- # Note: If you want to use your free NVIDIA endpoint, change model_id to:
171
- # "openrouter/nvidia/nemotron-3-ultra-550b-a55b:free"
172
  self.model = RateLimitedModel(
173
  model_id=os.getenv("GEMINI_MODEL", "gemini/gemini-2.5-flash"),
174
- api_key=os.getenv("GEMINI_API_KEY") # Or os.getenv("OPENROUTER_API_KEY")
175
  )
176
 
177
- # ToolCallingAgent natively loops through tools until it solves the prompt
178
- self.agent = ToolCallingAgent(
179
  tools=[
180
  enhanced_web_search,
181
  enhanced_wikipedia,
@@ -186,10 +181,14 @@ class GaiaAgent:
186
  extract_youtube
187
  ],
188
  model=self.model,
189
- max_steps=6 # Prevents infinite agent loops if tools fail
 
 
 
 
190
  )
191
 
192
- print("✓ smolagents GAIA Agent initialized")
193
  print("✓ 4-Second Rate Limit Protection Active")
194
 
195
  def __call__(self, task_id: str, question: str) -> str:
@@ -200,7 +199,7 @@ class GaiaAgent:
200
  processed_question = process_file(task_id, question)
201
 
202
  try:
203
- # 2. smolagents executes the ReAct loop automatically
204
  result = self.agent.run(processed_question)
205
 
206
  print(f"[{task_id}] FINAL ANSWER: {result}")
@@ -211,10 +210,10 @@ class GaiaAgent:
211
  error_msg = f"Critical error in execution: {str(e)}"
212
  print(f"[{task_id}] {error_msg}")
213
 
214
- # Fallback
215
  try:
216
  print("Attempting fallback direct response...")
217
- return self.model([{"role": "user", "content": question}]).content
218
  except:
219
  return error_msg
220
 
 
7
  from typing import Optional
8
 
9
  from dotenv import load_dotenv
10
+ # --- MODIFIED: Switched ToolCallingAgent to CodeAgent ---
11
+ from smolagents import CodeAgent, tool, LiteLLMModel
12
 
13
  # Import your custom logic
14
  from tools import (
 
45
  )
46
 
47
  # ----------- 2. smolagents Tool Definitions -----------
 
 
48
  @tool
49
  def enhanced_web_search(query: str) -> str:
50
  """Enhanced web search with intelligent query processing. Use for recent/broad web info.
 
104
  return extract_youtube_info(url)
105
 
106
  # ----------- 3. Enhanced File Processing -----------
 
107
  def detect_file_type(file_path: str) -> Optional[str]:
108
  ext = Path(file_path).suffix.lower()
109
  file_type_mapping = {
 
161
 
162
  # ----------- 4. Agent Class -----------
163
  class GaiaAgent:
164
+ """GAIA Agent powered by smolagents CodeAgent"""
165
 
166
  def __init__(self):
 
 
 
167
  self.model = RateLimitedModel(
168
  model_id=os.getenv("GEMINI_MODEL", "gemini/gemini-2.5-flash"),
169
+ api_key=os.getenv("GEMINI_API_KEY")
170
  )
171
 
172
+ # --- UPGRADED: Instantiated as CodeAgent with dependency authorization ---
173
+ self.agent = CodeAgent(
174
  tools=[
175
  enhanced_web_search,
176
  enhanced_wikipedia,
 
181
  extract_youtube
182
  ],
183
  model=self.model,
184
+ # Increased max_steps to 12. Code-writing agents require a few extra internal
185
+ # iterations to verify, execute, and format complex multi-modal answers.
186
+ max_steps=12,
187
+ # Crucial for GAIA spreadsheet parsing and data manipulation questions
188
+ additional_authorized_imports=["pandas", "numpy", "re", "math", "json", "collections"]
189
  )
190
 
191
+ print("✓ smolagents CodeAgent Architecture initialized")
192
  print("✓ 4-Second Rate Limit Protection Active")
193
 
194
  def __call__(self, task_id: str, question: str) -> str:
 
199
  processed_question = process_file(task_id, question)
200
 
201
  try:
202
+ # 2. Executing code loop sequence
203
  result = self.agent.run(processed_question)
204
 
205
  print(f"[{task_id}] FINAL ANSWER: {result}")
 
210
  error_msg = f"Critical error in execution: {str(e)}"
211
  print(f"[{task_id}] {error_msg}")
212
 
213
+ # Fallback direct query if agent framework loop experiences structural issues
214
  try:
215
  print("Attempting fallback direct response...")
216
+ return self.model(messages=[{"role": "user", "content": question}]).content
217
  except:
218
  return error_msg
219