Mehedi2 commited on
Commit
107e713
·
verified ·
1 Parent(s): 0329868

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +9 -39
agent.py CHANGED
@@ -1,8 +1,6 @@
1
  import os
2
  from pathlib import Path
3
  from typing import Optional, Union
4
- import smolagents
5
-
6
  import pandas as pd
7
  from dotenv import load_dotenv
8
  from smolagents import (
@@ -12,33 +10,16 @@ from smolagents import (
12
  from smolagents.tools import Tool
13
  from tabulate import tabulate
14
 
15
- # Load environment variables
16
  load_dotenv()
17
 
18
- # ----------------------------
19
- # Initialize the Google Gemini LLM
20
- # ----------------------------
21
- model_name = os.getenv("GEMINI_MODEL", "google/gemini-2.5-flash-preview-09-2025")
22
- g_api_key = os.getenv("GEMINI_API_KEY")
23
- vertex_project = os.getenv("VERTEX_PROJECT")
24
- vertex_location = os.getenv("VERTEX_LOCATION", "us-central1")
25
-
26
- if not g_api_key:
27
- raise RuntimeError(
28
- "Google API key not found. Set GEMINI_API_KEY in your Space secrets."
29
- )
30
-
31
- # LiteLLM expects provider prefix
32
  model = LiteLLMModel(
33
- model_id=f"vertex_ai/gemini/{model_name}",
34
- api_key=g_api_key,
35
- # Optional: pass project/location if supported by smolagents
36
- # provider_config={"vertex_project": vertex_project, "vertex_location": vertex_location}
37
  )
38
 
39
- # ----------------------------
40
- # Custom tool: Excel to Markdown
41
- # ----------------------------
42
  class ExcelToTextTool(Tool):
43
  """Render an Excel worksheet as a Markdown table."""
44
 
@@ -66,6 +47,7 @@ class ExcelToTextTool(Tool):
66
  output_type = "string"
67
 
68
  def forward(self, excel_path: str, sheet_name: Optional[str] = None) -> str:
 
69
  file_path = Path(excel_path).expanduser().resolve()
70
  if not file_path.is_file():
71
  return f"Error: Excel file not found at {file_path}"
@@ -88,9 +70,6 @@ class ExcelToTextTool(Tool):
88
  return f"Error reading Excel file: {e}"
89
 
90
 
91
- # ----------------------------
92
- # GaiaAgent: handles multiple tools
93
- # ----------------------------
94
  class GaiaAgent:
95
  """An agent capable of using tools to answer general questions."""
96
 
@@ -116,18 +95,9 @@ class GaiaAgent:
116
  print(f"Agent received task_id='{task_id}' | question='{question[:50]}...'")
117
  try:
118
  answer = self.agent.run(question)
 
 
119
  except Exception as e:
120
- err_text = str(e)
121
- # Catch common LLM errors
122
- if "Insufficient credits" in err_text:
123
- answer = "⚠️ LLM request failed: insufficient Google AI Studio credits."
124
- elif "Provider NOT provided" in err_text or "vertex" in err_text.lower():
125
- answer = f"⚠️ LLM provider error: {err_text}"
126
- else:
127
- answer = f"⚠️ Agent failed: {err_text}"
128
-
129
- if not answer:
130
- answer = "⚠️ Sorry, I couldn't generate a response."
131
-
132
  print(f"Agent returning answer: {answer}")
133
  return answer
 
1
  import os
2
  from pathlib import Path
3
  from typing import Optional, Union
 
 
4
  import pandas as pd
5
  from dotenv import load_dotenv
6
  from smolagents import (
 
10
  from smolagents.tools import Tool
11
  from tabulate import tabulate
12
 
13
+ # Load environment variables (make sure GEMINI_API_KEY is set in your Space secrets)
14
  load_dotenv()
15
 
16
+ # Initialize the Gemini model using Google AI Studio API key
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  model = LiteLLMModel(
18
+ model_id="vertex_ai/gemini/google/gemini-2.5-flash-preview-09-2025",
19
+ api_key=os.getenv("GEMINI_API_KEY"),
 
 
20
  )
21
 
22
+
 
 
23
  class ExcelToTextTool(Tool):
24
  """Render an Excel worksheet as a Markdown table."""
25
 
 
47
  output_type = "string"
48
 
49
  def forward(self, excel_path: str, sheet_name: Optional[str] = None) -> str:
50
+ """Load the Excel file and return the sheet as a Markdown table."""
51
  file_path = Path(excel_path).expanduser().resolve()
52
  if not file_path.is_file():
53
  return f"Error: Excel file not found at {file_path}"
 
70
  return f"Error reading Excel file: {e}"
71
 
72
 
 
 
 
73
  class GaiaAgent:
74
  """An agent capable of using tools to answer general questions."""
75
 
 
95
  print(f"Agent received task_id='{task_id}' | question='{question[:50]}...'")
96
  try:
97
  answer = self.agent.run(question)
98
+ if not answer:
99
+ answer = "⚠️ Sorry, I could not generate a valid response."
100
  except Exception as e:
101
+ answer = f"⚠️ Agent failed with error: {e}"
 
 
 
 
 
 
 
 
 
 
 
102
  print(f"Agent returning answer: {answer}")
103
  return answer