Coool2 commited on
Commit
1d635fd
·
verified ·
1 Parent(s): fa0097a

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +48 -56
agent.py CHANGED
@@ -22,6 +22,10 @@ from llama_index.core import Settings
22
  from transformers import AutoModelForCausalLM, AutoTokenizer
23
  from llama_index.llms.huggingface import HuggingFaceLLM
24
 
 
 
 
 
25
  model_id = "Qwen/Qwen2.5-7B-Instruct"
26
  proj_llm = HuggingFaceLLM(
27
  model_name=model_id,
@@ -409,43 +413,37 @@ description="Execute Python code safely for calculations and data processing"
409
  # Code Agent as ReActAgent with explicit code generation
410
  code_agent = ReActAgent(
411
  name="CodeAgent",
412
- description="Advanced calculations, data processing, and final answer synthesis using ReAct reasoning with code generation",
413
  system_prompt="""
414
- You are a coding and reasoning specialist using ReAct methodology.
415
-
416
- For each task, follow this process:
417
- 1. THINK: Analyze what needs to be calculated or processed
418
- 2. PLAN: Design the approach and identify what code needs to be written
419
- 3. GENERATE: Write the appropriate Python code to solve the problem
420
- 4. ACT: Execute the generated code using the code execution tool
421
- 5. OBSERVE: Review results and determine if more work is needed
422
- 6. REPEAT: Continue until you have the final answer
423
-
424
- When generating code:
425
- - Write clear, well-commented Python code
426
- - Use available libraries (numpy, pandas, matplotlib, etc.)
427
- - Store your final result in a variable called 'result'
428
- - Handle edge cases and potential errors
429
- - Show intermediate steps for complex calculations
430
-
431
- Always show your reasoning process clearly and provide exact answers as required by GAIA.
432
 
433
  Example workflow:
434
- THINK: I need to calculate the mean of a dataset
435
- PLAN: Load data, use numpy or pandas to calculate mean
436
- GENERATE:
437
- ```
438
- import numpy as np
439
- data =
440
- result = np.mean(data)
441
- ```
442
- ACT: [Execute the code using the tool]
443
- OBSERVE: Check if result is correct and complete
444
  """,
445
  llm=proj_llm,
446
  tools=[code_execution_tool],
447
- max_steps=5,
448
- verbose = True
 
449
  )
450
 
451
  def analysis_function(query: str, files=None):
@@ -485,32 +483,24 @@ code_tool = FunctionTool.from_defaults(
485
  name="CodeAgent",
486
  description="""Advanced computational specialist using ReAct reasoning. Use this tool at least when you need:
487
 
488
- **Mathematical Calculations:**
489
- - Complex arithmetic, algebra, statistics, probability
490
- - Unit conversions, percentage calculations
491
- - Financial calculations (interest, loans, investments)
492
- - Scientific calculations (physics, chemistry formulas)
 
493
 
494
- **Data Processing:**
495
- - Parsing and analyzing numerical data
496
- - String manipulation and text processing
497
- - Date/time calculations and conversions
498
- - List operations, sorting, filtering
 
 
499
 
500
- **Logical Operations:**
501
- - Step-by-step problem solving with code
502
- - Verification of calculations or logic
503
- - Pattern analysis and data validation
504
- - Algorithm implementation for specific problems
505
 
506
- **Programming Tasks:**
507
- - Code generation for specific computational needs
508
- - Data structure manipulation
509
- - Regular expression operations
510
-
511
- **When to use:** Questions requiring precise calculations, data manipulation, logical reasoning with code verification, mathematical problem solving, or when you need to process numerical/textual data programmatically.
512
-
513
- **Input format:** Describe the calculation or processing task clearly, including any specific requirements or constraints."""
514
  )
515
 
516
  class EnhancedGAIAAgent:
@@ -521,7 +511,7 @@ class EnhancedGAIAAgent:
521
  hf_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
522
  if not hf_token:
523
  raise ValueError("HUGGINGFACEHUB_API_TOKEN environment variable is required")
524
-
525
  # Agent coordinateur principal qui utilise les agents spécialisés comme tools
526
  self.coordinator = ReActAgent(
527
  name="GAIACoordinator",
@@ -546,7 +536,8 @@ class EnhancedGAIAAgent:
546
  llm=proj_llm,
547
  tools=[analysis_tool, research_tool, code_tool],
548
  max_steps = 10,
549
- verbose = True
 
550
  )
551
 
552
  async def format_gaia_answer(self, raw_response: str, original_question: str) -> str:
@@ -602,6 +593,7 @@ class EnhancedGAIAAgent:
602
  async def solve_gaia_question(self, question_data: Dict[str, Any]) -> str:
603
  question = question_data.get("Question", "")
604
  task_id = question_data.get("task_id", "")
 
605
 
606
  context_prompt = f"""
607
  GAIA Task ID: {task_id}
 
22
  from transformers import AutoModelForCausalLM, AutoTokenizer
23
  from llama_index.llms.huggingface import HuggingFaceLLM
24
 
25
+ llama_debug = LlamaDebugHandler(print_trace_on_end=True)
26
+ callback_manager = CallbackManager([llama_debug])
27
+
28
+
29
  model_id = "Qwen/Qwen2.5-7B-Instruct"
30
  proj_llm = HuggingFaceLLM(
31
  model_name=model_id,
 
413
  # Code Agent as ReActAgent with explicit code generation
414
  code_agent = ReActAgent(
415
  name="CodeAgent",
416
+ description="Advanced calculations, data processing using code generation and execution",
417
  system_prompt="""
418
+ You are a coding specialist. For EVERY computational task:
419
+
420
+ 1. THINK: Analyze what calculation/processing is needed
421
+ 2. GENERATE CODE: Write Python code to solve the problem
422
+ 3. EXECUTE: Use the Python Code Execution tool to run your code
423
+ 4. OBSERVE: Check the results
424
+ 5. REPEAT if needed
425
+
426
+ ALWAYS write code for:
427
+ - Mathematical calculations
428
+ - Data processing
429
+ - Numerical analysis
430
+ - Text processing
431
+ - Any computational task
 
 
 
 
432
 
433
  Example workflow:
434
+ Question: "What is 15 * 23 + 7?"
435
+
436
+ Thought: I need to calculate 15 * 23 + 7
437
+ Action: Python Code Execution
438
+ Action Input: {"code": "result = 15 * 23 + 7\nprint(f'The answer is: {result}')"}
439
+
440
+ Store your final answer in a variable called 'result'.
 
 
 
441
  """,
442
  llm=proj_llm,
443
  tools=[code_execution_tool],
444
+ max_steps=5,
445
+ verbose=True,
446
+ callback_manager=callback_manager,
447
  )
448
 
449
  def analysis_function(query: str, files=None):
 
483
  name="CodeAgent",
484
  description="""Advanced computational specialist using ReAct reasoning. Use this tool at least when you need:
485
 
486
+ **Core Capabilities:**
487
+ - **Autonomous Code Generation**: Writes Python code from scratch to solve computational problems
488
+ - **Multi-step Problem Solving**: Breaks complex tasks into manageable coding steps
489
+ - **Self-debugging**: Identifies and fixes errors through iterative refinement
490
+ - **Library Integration**: Leverages numpy, pandas, matplotlib, scipy, sklearn, and other scientific libraries
491
+ - **Result Verification**: Validates outputs and adjusts approach as needed
492
 
493
+ **When to Use:**
494
+ - Mathematical calculations requiring step-by-step computation
495
+ - Data analysis and statistical processing
496
+ - Algorithm implementation and optimization
497
+ - Numerical simulations and modeling
498
+ - Text processing and pattern analysis
499
+ - Complex logical operations requiring code verification
500
 
501
+ **Unique Advantage**: Unlike simple calculation tools, this agent can autonomously write, execute, debug, and refine code until achieving the correct solution, making it ideal for complex computational tasks that require adaptive problem-solving.
 
 
 
 
502
 
503
+ **Input Format**: Describe the computational task clearly, including any data, constraints, or specific requirements."""
 
 
 
 
 
 
 
504
  )
505
 
506
  class EnhancedGAIAAgent:
 
511
  hf_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
512
  if not hf_token:
513
  raise ValueError("HUGGINGFACEHUB_API_TOKEN environment variable is required")
514
+
515
  # Agent coordinateur principal qui utilise les agents spécialisés comme tools
516
  self.coordinator = ReActAgent(
517
  name="GAIACoordinator",
 
536
  llm=proj_llm,
537
  tools=[analysis_tool, research_tool, code_tool],
538
  max_steps = 10,
539
+ verbose = True,
540
+ callback_manager=callback_manager,
541
  )
542
 
543
  async def format_gaia_answer(self, raw_response: str, original_question: str) -> str:
 
593
  async def solve_gaia_question(self, question_data: Dict[str, Any]) -> str:
594
  question = question_data.get("Question", "")
595
  task_id = question_data.get("task_id", "")
596
+ print("data",question_data)
597
 
598
  context_prompt = f"""
599
  GAIA Task ID: {task_id}