Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -15,14 +15,30 @@ from tabulate import tabulate
|
|
| 15 |
# Load environment variables
|
| 16 |
load_dotenv()
|
| 17 |
|
| 18 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
model = LiteLLMModel(
|
| 20 |
-
model_id=f"
|
| 21 |
-
api_key=
|
| 22 |
-
|
|
|
|
| 23 |
)
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
class ExcelToTextTool(Tool):
|
| 27 |
"""Render an Excel worksheet as a Markdown table."""
|
| 28 |
|
|
@@ -72,6 +88,9 @@ class ExcelToTextTool(Tool):
|
|
| 72 |
return f"Error reading Excel file: {e}"
|
| 73 |
|
| 74 |
|
|
|
|
|
|
|
|
|
|
| 75 |
class GaiaAgent:
|
| 76 |
"""An agent capable of using tools to answer general questions."""
|
| 77 |
|
|
@@ -98,11 +117,17 @@ class GaiaAgent:
|
|
| 98 |
try:
|
| 99 |
answer = self.agent.run(question)
|
| 100 |
except Exception as e:
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
if not answer:
|
| 104 |
-
answer = "⚠️ Sorry, I
|
| 105 |
|
| 106 |
print(f"Agent returning answer: {answer}")
|
| 107 |
return answer
|
| 108 |
-
|
|
|
|
| 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 |
|
|
|
|
| 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 |
|
|
|
|
| 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
|
|
|