Spaces:
Configuration error
Configuration error
oremaz
commited on
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -347,21 +347,146 @@ research_tool = FunctionTool.from_defaults(
|
|
| 347 |
**Input format:** Provide the research query with any relevant context."""
|
| 348 |
)
|
| 349 |
|
| 350 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 351 |
name="CodeAgent",
|
| 352 |
-
description="Advanced calculations, data processing, and final answer synthesis using ReAct reasoning",
|
| 353 |
system_prompt="""
|
| 354 |
You are a coding and reasoning specialist using ReAct methodology.
|
| 355 |
-
|
| 356 |
-
For each task:
|
| 357 |
1. THINK: Analyze what needs to be calculated or processed
|
| 358 |
-
2.
|
| 359 |
-
3.
|
| 360 |
-
4.
|
|
|
|
|
|
|
| 361 |
|
| 362 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
""",
|
| 364 |
-
llm=proj_llm,
|
|
|
|
|
|
|
| 365 |
)
|
| 366 |
|
| 367 |
analysis_tool = FunctionTool.from_defaults(
|
|
|
|
| 347 |
**Input format:** Provide the research query with any relevant context."""
|
| 348 |
)
|
| 349 |
|
| 350 |
+
def execute_python_code(code: str) -> str:
|
| 351 |
+
try:
|
| 352 |
+
safe_globals = {
|
| 353 |
+
"__builtins__": {
|
| 354 |
+
"len": len, "str": str, "int": int, "float": float,
|
| 355 |
+
"list": list, "dict": dict, "sum": sum, "max": max, "min": min,
|
| 356 |
+
"round": round, "abs": abs, "sorted": sorted, "enumerate": enumerate,
|
| 357 |
+
"range": range, "zip": zip, "map": map, "filter": filter,
|
| 358 |
+
"any": any, "all": all, "type": type, "isinstance": isinstance,
|
| 359 |
+
"print": print, "open": open, "bool": bool, "set": set, "tuple": tuple
|
| 360 |
+
},
|
| 361 |
+
# Core Python modules
|
| 362 |
+
"math": __import__("math"),
|
| 363 |
+
"datetime": __import__("datetime"),
|
| 364 |
+
"re": __import__("re"),
|
| 365 |
+
"os": __import__("os"),
|
| 366 |
+
"sys": __import__("sys"),
|
| 367 |
+
"json": __import__("json"),
|
| 368 |
+
"csv": __import__("csv"),
|
| 369 |
+
"random": __import__("random"),
|
| 370 |
+
"itertools": __import__("itertools"),
|
| 371 |
+
"collections": __import__("collections"),
|
| 372 |
+
"functools": __import__("functools"),
|
| 373 |
+
|
| 374 |
+
# Data Science and Numerical Computing
|
| 375 |
+
"numpy": __import__("numpy"),
|
| 376 |
+
"np": __import__("numpy"),
|
| 377 |
+
"pandas": __import__("pandas"),
|
| 378 |
+
"pd": __import__("pandas"),
|
| 379 |
+
"scipy": __import__("scipy"),
|
| 380 |
+
|
| 381 |
+
# Visualization
|
| 382 |
+
"matplotlib": __import__("matplotlib"),
|
| 383 |
+
"plt": __import__("matplotlib.pyplot"),
|
| 384 |
+
"seaborn": __import__("seaborn"),
|
| 385 |
+
"sns": __import__("seaborn"),
|
| 386 |
+
"plotly": __import__("plotly"),
|
| 387 |
+
|
| 388 |
+
# Machine Learning
|
| 389 |
+
"sklearn": __import__("sklearn"),
|
| 390 |
+
"xgboost": __import__("xgboost"),
|
| 391 |
+
"lightgbm": __import__("lightgbm"),
|
| 392 |
+
|
| 393 |
+
# Statistics
|
| 394 |
+
"statistics": __import__("statistics"),
|
| 395 |
+
"statsmodels": __import__("statsmodels"),
|
| 396 |
+
|
| 397 |
+
# Image Processing
|
| 398 |
+
"PIL": __import__("PIL"),
|
| 399 |
+
"cv2": __import__("cv2"),
|
| 400 |
+
"skimage": __import__("skimage"),
|
| 401 |
+
|
| 402 |
+
# Network and Web
|
| 403 |
+
"requests": __import__("requests"),
|
| 404 |
+
"urllib": __import__("urllib"),
|
| 405 |
+
|
| 406 |
+
# Text Processing
|
| 407 |
+
"nltk": __import__("nltk"),
|
| 408 |
+
"spacy": __import__("spacy"),
|
| 409 |
+
|
| 410 |
+
# Time Series
|
| 411 |
+
"pytz": __import__("pytz"),
|
| 412 |
+
|
| 413 |
+
# Utilities
|
| 414 |
+
"tqdm": __import__("tqdm"),
|
| 415 |
+
"pickle": __import__("pickle"),
|
| 416 |
+
"gzip": __import__("gzip"),
|
| 417 |
+
"base64": __import__("base64"),
|
| 418 |
+
"hashlib": __import__("hashlib"),
|
| 419 |
+
"uuid": __import__("uuid"),
|
| 420 |
+
|
| 421 |
+
# Scientific Computing
|
| 422 |
+
"sympy": __import__("sympy"),
|
| 423 |
+
"networkx": __import__("networkx"),
|
| 424 |
+
|
| 425 |
+
# Database
|
| 426 |
+
"sqlite3": __import__("sqlite3"),
|
| 427 |
+
|
| 428 |
+
# Parallel Processing
|
| 429 |
+
"multiprocessing": __import__("multiprocessing"),
|
| 430 |
+
"threading": __import__("threading"),
|
| 431 |
+
"concurrent": __import__("concurrent"),
|
| 432 |
+
}
|
| 433 |
+
|
| 434 |
+
exec_locals = {}
|
| 435 |
+
exec(code, safe_globals, exec_locals)
|
| 436 |
+
|
| 437 |
+
if 'result' in exec_locals:
|
| 438 |
+
return str(exec_locals['result'])
|
| 439 |
+
else:
|
| 440 |
+
return "Code executed successfully"
|
| 441 |
+
|
| 442 |
+
except Exception as e:
|
| 443 |
+
return f"Code execution failed: {str(e)}"
|
| 444 |
+
|
| 445 |
+
code_execution_tool = FunctionTool.from_defaults(
|
| 446 |
+
fn=execute_python_code,
|
| 447 |
+
name="Python Code Execution",
|
| 448 |
+
description="Execute Python code safely for calculations and data processing"
|
| 449 |
+
)
|
| 450 |
+
|
| 451 |
+
# Code Agent as ReActAgent with explicit code generation
|
| 452 |
+
code_agent = ReActAgent(
|
| 453 |
name="CodeAgent",
|
| 454 |
+
description="Advanced calculations, data processing, and final answer synthesis using ReAct reasoning with code generation",
|
| 455 |
system_prompt="""
|
| 456 |
You are a coding and reasoning specialist using ReAct methodology.
|
| 457 |
+
|
| 458 |
+
For each task, follow this process:
|
| 459 |
1. THINK: Analyze what needs to be calculated or processed
|
| 460 |
+
2. PLAN: Design the approach and identify what code needs to be written
|
| 461 |
+
3. GENERATE: Write the appropriate Python code to solve the problem
|
| 462 |
+
4. ACT: Execute the generated code using the code execution tool
|
| 463 |
+
5. OBSERVE: Review results and determine if more work is needed
|
| 464 |
+
6. REPEAT: Continue until you have the final answer
|
| 465 |
|
| 466 |
+
When generating code:
|
| 467 |
+
- Write clear, well-commented Python code
|
| 468 |
+
- Use available libraries (numpy, pandas, matplotlib, etc.)
|
| 469 |
+
- Store your final result in a variable called 'result'
|
| 470 |
+
- Handle edge cases and potential errors
|
| 471 |
+
- Show intermediate steps for complex calculations
|
| 472 |
+
|
| 473 |
+
Always show your reasoning process clearly and provide exact answers as required by GAIA.
|
| 474 |
+
|
| 475 |
+
Example workflow:
|
| 476 |
+
THINK: I need to calculate the mean of a dataset
|
| 477 |
+
PLAN: Load data, use numpy or pandas to calculate mean
|
| 478 |
+
GENERATE:
|
| 479 |
+
```
|
| 480 |
+
import numpy as np
|
| 481 |
+
data =
|
| 482 |
+
result = np.mean(data)
|
| 483 |
+
```
|
| 484 |
+
ACT: [Execute the code using the tool]
|
| 485 |
+
OBSERVE: Check if result is correct and complete
|
| 486 |
""",
|
| 487 |
+
llm=proj_llm,
|
| 488 |
+
tools=[code_execution_tool],
|
| 489 |
+
max_steps=5
|
| 490 |
)
|
| 491 |
|
| 492 |
analysis_tool = FunctionTool.from_defaults(
|