vallurimahanth commited on
Commit
fbb6bac
·
verified ·
1 Parent(s): a99c241

Update core_agent.py

Browse files
Files changed (1) hide show
  1. core_agent.py +175 -0
core_agent.py CHANGED
@@ -128,6 +128,181 @@ def analyze_csv_file(file_path: str, query: str) -> str:
128
  except Exception as e:
129
  return f"Error analyzing CSV file: {str(e)}"
130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
 
132
 
133
 
 
128
  except Exception as e:
129
  return f"Error analyzing CSV file: {str(e)}"
130
 
131
+
132
+ @tool
133
+ def analyze_excel_file(file_path: str, query: str) -> str:
134
+ """
135
+ Analyze an Excel file using pandas and answer a question about it.
136
+
137
+ Args:
138
+ file_path: Path to the Excel file
139
+ query: Question about the data
140
+
141
+ Returns:
142
+ Analysis result or error message
143
+ """
144
+ try:
145
+ import pandas as pd
146
+
147
+ # Read the Excel file
148
+ df = pd.read_excel(file_path)
149
+
150
+ # Run various analyses based on the query
151
+ result = f"Excel file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
152
+ result += f"Columns: {', '.join(df.columns)}\n\n"
153
+
154
+ # Add summary statistics
155
+ result += "Summary statistics:\n"
156
+ result += str(df.describe())
157
+
158
+ return result
159
+ except ImportError:
160
+ return "Error: pandas and openpyxl are not installed. Please install them with 'pip install pandas openpyxl'."
161
+ except Exception as e:
162
+ return f"Error analyzing Excel file: {str(e)}"
163
+
164
+
165
+ class GAIAAgent:
166
+ def __init__(
167
+ self,
168
+ model_type: str = "HfApiModel",
169
+ model_id: Optional[str] = None,
170
+ api_key: Optional[str] = None,
171
+ api_base: Optional[str] = None,
172
+ temperature: float = 0.2,
173
+ executor_type: str = "local", # Changed from use_e2b to executor_type
174
+ additional_imports: List[str] = None,
175
+ additional_tools: List[Any] = None,
176
+ system_prompt: Optional[str] = None, # We'll still accept this parameter but not use it directly
177
+ verbose: bool = False,
178
+ provider: Optional[str] = None, # Add provider for InferenceClientModel
179
+ timeout: Optional[int] = None
180
+ ):
181
+ """
182
+ Initialize a GAIAAgent with specified configuration
183
+
184
+ Args:
185
+ model_type: Type of model to use (HfApiModel, LiteLLMModel, OpenAIServerModel, InferenceClientModel)
186
+ model_id: ID of the model to use
187
+ api_key: API key for the model provider
188
+ api_base: Base URL for API calls
189
+ temperature: Temperature for text generation
190
+ executor_type: Type of executor for code execution ('local' or 'e2b')
191
+ additional_imports: Additional Python modules to allow importing
192
+ additional_tools: Additional tools to provide to the agent
193
+ system_prompt: Custom system prompt to use (not directly used, kept for backward compatibility)
194
+ verbose: Enable verbose logging
195
+ provider: Provider for InferenceClientModel (e.g., "hf-inference")
196
+ timeout: Timeout in seconds for API calls
197
+ """
198
+ self.verbose = verbose
199
+ self.system_prompt = system_prompt # Store for potential future use
200
+
201
+ if model_type == "HfApiModel":
202
+ if api_key is None:
203
+ api_key = os.getenv("HUGGINGFACEHUB_API_TOKEN")
204
+ if not api_key:
205
+ raise ValueError("No Hugging Face token provided. Please set HUGGINGFACEHUB_API_TOKEN environment variable or pass api_key parameter.")
206
+
207
+ if self.verbose:
208
+ print(f"Using Hugging Face token: {api_key[:5]}...")
209
+
210
+ self.model = HfApiModel(
211
+ model_id=model_id or "meta-llama/Llama-3-70B-Instruct",
212
+ token=api_key,
213
+ temperature=temperature
214
+ )
215
+ else:
216
+ raise ValueError(f"Unknown model type: {model_type}")
217
+
218
+ if self.verbose:
219
+ print(f"Initialized model: {model_type} - {model_id}")
220
+
221
+ # Initialize default tools
222
+ self.tools = [
223
+ DuckDuckGoSearchTool(),
224
+ PythonInterpreterTool(),
225
+ save_file,
226
+ download_file_from_url,
227
+ analyze_csv_file,
228
+ analyze_excel_file
229
+ ]
230
+
231
+ try:
232
+ from langchain.document_loaders import UnstructuredImageLoader
233
+ self.tools.append(extract_text_from_image)
234
+ if self.verbose:
235
+ print("Added image processing tool")
236
+ except ImportError:
237
+ if self.verbose:
238
+ print("Image processing libraries not available")
239
+
240
+ if additional_tools:
241
+ self.tools.extend(additional_tools)
242
+
243
+ if self.verbose:
244
+ print(f"Initialized with {len(self.tools)} tools")
245
+
246
+ self.imports = ["pandas", "numpy", "datetime", "json", "re", "math", "os", "requests", "csv", "urllib"]
247
+ if additional_imports:
248
+ self.imports.extend(additional_imports)
249
+
250
+ # Initialize the CodeAgent
251
+ executor_kwargs = {}
252
+ if executor_type == "e2b":
253
+ try:
254
+ # Try to import e2b dependencies to check if they're available
255
+ from e2b_code_interpreter import Sandbox
256
+ if self.verbose:
257
+ print("Using e2b executor")
258
+ except ImportError:
259
+ if self.verbose:
260
+ print("e2b dependencies not found, falling back to local executor")
261
+ executor_type = "local" # Fallback to local if e2b is not available
262
+
263
+ self.agent = CodeAgent(
264
+ tools=self.tools,
265
+ model=self.model,
266
+ additional_authorized_imports=self.imports,
267
+ executor_type=executor_type,
268
+ executor_kwargs=executor_kwargs,
269
+ verbosity_level=2 if self.verbose else 0
270
+ )
271
+
272
+ if self.verbose:
273
+ print("Agent initialized and ready")
274
+
275
+
276
+ def answer_question(self,question:str,task_file_path: Optional[str]=None):
277
+ """
278
+ Process a GAIA benchmark question and return the answer
279
+
280
+ Args:
281
+ question: The question to answer
282
+ task_file_path: Optional path to a file associated with the question
283
+
284
+ Returns:
285
+ The answer to the question
286
+ """
287
+ try:
288
+ if self.verbose:
289
+ print(f"Processing Question {question}")
290
+ if task_file_path:
291
+ print(f"with associated task file {task_file_path}")
292
+
293
+ context=question
294
+ fileContent=None
295
+
296
+ if task_file_path:
297
+ try:
298
+ with open(task_file_path,'r') as f:
299
+ fileContent=f.read()
300
+
301
+ import os
302
+ file_ext=os.path.splitext(task_file_path)[1].lower()
303
+ context=f"""Question: {question} This question has an associated file. Here is the file content:```{file_ext} {file_content}``` Analyze the file content above to answer the question."""
304
+
305
+
306
 
307
 
308