Mehedi2 commited on
Commit
8cb70b7
·
verified ·
1 Parent(s): a070471

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +12 -26
agent.py CHANGED
@@ -5,18 +5,21 @@ import smolagents
5
 
6
  import pandas as pd
7
  from dotenv import load_dotenv
8
- from smolagents import (CodeAgent, DuckDuckGoSearchTool, FinalAnswerTool,
9
- LiteLLMModel, PythonInterpreterTool,
10
- WikipediaSearchTool)
 
11
  from smolagents.tools import Tool
12
  from tabulate import tabulate
13
 
14
  # Load environment variables
15
  load_dotenv()
16
 
17
- # Initialize the model
18
  model = LiteLLMModel(
19
- model_id=os.getenv("GEMINI_MODEL"), api_key=os.getenv("GEMINI_API_KEY")
 
 
20
  )
21
 
22
 
@@ -47,16 +50,6 @@ class ExcelToTextTool(Tool):
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
-
52
- Args:
53
- excel_path: Path to the Excel file.
54
- sheet_name: Optional name or index of the sheet to read. If None, reads the first sheet.
55
-
56
- Returns:
57
- A Markdown table representing the Excel sheet, or an error message if the file is not found or cannot be read.
58
- """
59
-
60
  file_path = Path(excel_path).expanduser().resolve()
61
  if not file_path.is_file():
62
  return f"Error: Excel file not found at {file_path}"
@@ -83,8 +76,6 @@ class GaiaAgent:
83
  """An agent capable of using tools to answer general questions."""
84
 
85
  def __init__(self):
86
- """Initializes the GaiaAgent with a set of tools."""
87
-
88
  print("GaiaAgent initialized with tools.")
89
 
90
  tools = [
@@ -99,20 +90,15 @@ class GaiaAgent:
99
  model=model,
100
  tools=tools,
101
  add_base_tools=True,
 
102
  additional_authorized_imports=["pandas", "numpy", "csv", "subprocess"],
103
  )
104
 
105
  def __call__(self, task_id: str, question: str) -> str:
106
- """Processes a question using the agent and its tools.
107
-
108
- Args:
109
- task_id: A unique identifier for the task.
110
- question: The question to be answered.
111
-
112
- Returns:
113
- The answer generated by the agent.
114
- """
115
  print(f"Agent received task_id='{task_id}' | question='{question[:50]}...'")
116
  answer = self.agent.run(question)
 
 
117
  print(f"Agent returning answer: {answer}")
118
  return answer
 
 
5
 
6
  import pandas as pd
7
  from dotenv import load_dotenv
8
+ from smolagents import (
9
+ CodeAgent, DuckDuckGoSearchTool, FinalAnswerTool,
10
+ LiteLLMModel, PythonInterpreterTool, WikipediaSearchTool
11
+ )
12
  from smolagents.tools import Tool
13
  from tabulate import tabulate
14
 
15
  # Load environment variables
16
  load_dotenv()
17
 
18
+ # Initialize the model with OpenRouter Gemini
19
  model = LiteLLMModel(
20
+ model_id=os.getenv("GEMINI_MODEL", "google/gemini-2.5-flash-preview-09-2025"),
21
+ api_key=os.getenv("GEMINI_API_KEY"), # your OpenRouter API key
22
+ base_url="https://openrouter.ai/api/v1" # required for OpenRouter
23
  )
24
 
25
 
 
50
  output_type = "string"
51
 
52
  def forward(self, excel_path: str, sheet_name: Optional[str] = None) -> str:
 
 
 
 
 
 
 
 
 
 
53
  file_path = Path(excel_path).expanduser().resolve()
54
  if not file_path.is_file():
55
  return f"Error: Excel file not found at {file_path}"
 
76
  """An agent capable of using tools to answer general questions."""
77
 
78
  def __init__(self):
 
 
79
  print("GaiaAgent initialized with tools.")
80
 
81
  tools = [
 
90
  model=model,
91
  tools=tools,
92
  add_base_tools=True,
93
+ verbose=True, # show reasoning in logs
94
  additional_authorized_imports=["pandas", "numpy", "csv", "subprocess"],
95
  )
96
 
97
  def __call__(self, task_id: str, question: str) -> str:
 
 
 
 
 
 
 
 
 
98
  print(f"Agent received task_id='{task_id}' | question='{question[:50]}...'")
99
  answer = self.agent.run(question)
100
+ if not answer:
101
+ answer = "⚠️ Sorry, I could not generate a valid response."
102
  print(f"Agent returning answer: {answer}")
103
  return answer
104
+