Spaces:
Sleeping
Sleeping
File size: 13,082 Bytes
f504b2e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
"""
Code Execution Framework for GAIA Agent
Provides safe Python code execution for math/data processing questions.
Uses local execution with timeout and safety constraints.
Expected Impact: +15-20% accuracy improvement on math/calculation questions
"""
import re
import os
import sys
import time
import subprocess
import tempfile
from dataclasses import dataclass
from typing import Optional, List
from pathlib import Path
@dataclass
class ExecutionResult:
"""Result of code execution"""
success: bool
output: Optional[str]
error: Optional[str]
execution_time: float
def should_use_code_execution(question: str) -> bool:
"""
Determine if a question would benefit from code execution.
Args:
question: The question text
Returns:
True if code execution should be used
"""
question_lower = question.lower()
# EXCLUSIONS: Research questions that should NOT use code
research_indicators = [
'who', 'when', 'where', 'which person', 'which company',
'published by', 'written by', 'created by', 'founded by',
'according to', 'wikipedia', 'article', 'biography',
'history of', 'year of', 'born in', 'died in'
]
# If it's clearly a research/lookup question, don't use code
if any(indicator in question_lower for indicator in research_indicators):
# Exception: if it has actual numbers to calculate WITH
# e.g., "Who scored 25 + 30 points?" should use code for the math
has_math_operators = any(op in question for op in ['+', '-', '*', '/', '='])
if not has_math_operators:
return False
# Math keywords - direct operations
math_keywords = [
'calculate', 'compute', 'sum', 'average', 'mean', 'median',
'multiply', 'divide', 'subtract', 'add', 'total',
'square root', 'power', 'factorial', 'prime',
'+', '-', '*', '/', '%', '^', '='
]
# Check for math operations
if any(keyword in question_lower for keyword in math_keywords):
return True
# Data processing keywords - only for provided data
data_processing_indicators = [
'from the csv', 'in the file', 'in the spreadsheet',
'from the table', 'in the data', 'given the values',
'calculate from', 'based on the following'
]
if any(indicator in question_lower for indicator in data_processing_indicators):
return True
# Check for explicit number sequences that need calculation
# e.g., "What is 123 * 456" or "Sum of 10, 20, 30"
numbers = re.findall(r'\d+', question)
has_operators = any(op in question for op in ['+', '-', '*', '/', '=', 'x'])
if len(numbers) >= 2 and has_operators:
return True
return False
class CodeExecutor:
"""
Safe Python code executor with timeout and safety constraints.
Uses subprocess isolation to prevent harmful operations.
"""
def __init__(self, timeout: int = 10, openrouter_client=None, model: str = "x-ai/grok-4.1-fast"):
"""
Initialize code executor.
Args:
timeout: Maximum execution time in seconds
openrouter_client: OpenAI client for OpenRouter (for code generation)
model: Model to use for code generation
"""
self.timeout = timeout
self.openrouter_client = openrouter_client
self.model = model
def generate_code(self, question: str, context: Optional[str] = None) -> str:
"""
Generate Python code to answer the question.
Args:
question: The question to solve
context: Optional context/data for the question
Returns:
Python code as string
"""
# If we have OpenRouter, use LLM to generate code
if self.openrouter_client:
return self._generate_code_with_llm(question, context)
# Fallback: Simple code generation for basic math
return self._generate_code_simple(question)
def _generate_code_with_llm(self, question: str, context: Optional[str] = None) -> str:
"""Generate code using LLM"""
prompt = f"""Generate Python code to answer this question. Output ONLY the Python code, no explanations.
The code must print the final answer using print().
Question: {question}"""
if context:
prompt += f"\n\nContext/Data: {context}"
prompt += """
Requirements:
1. Use only Python standard library (math, statistics, etc.)
2. Print the final answer
3. Keep it simple and direct
4. No external imports except math, statistics
5. Handle edge cases
Code:"""
try:
response = self.openrouter_client.chat.completions.create(
model=self.model,
messages=[{"role": "user", "content": prompt}],
max_tokens=500,
temperature=0.1
)
code = response.choices[0].message.content.strip()
# Extract code from markdown if present
if "```python" in code:
code = code.split("```python")[1].split("```")[0].strip()
elif "```" in code:
code = code.split("```")[1].split("```")[0].strip()
return code
except Exception as e:
print(f"❌ LLM code generation failed: {e}")
return self._generate_code_simple(question)
def _generate_code_simple(self, question: str) -> str:
"""
Generate simple code without LLM (fallback).
This handles basic arithmetic expressions.
"""
# Try to extract a math expression
# Remove common words
expr = question.lower()
for word in ['what is', 'calculate', 'compute', 'the result of', '?', 'equal', 'equals']:
expr = expr.replace(word, ' ')
expr = expr.strip()
# Convert word operations to symbols
replacements = {
' plus ': '+',
' minus ': '-',
' times ': '*',
' divided by ': '/',
' multiply ': '*',
' divide ': '/',
' add ': '+',
' subtract ': '-'
}
for word, symbol in replacements.items():
expr = expr.replace(word, symbol)
# Clean up spaces
expr = re.sub(r'\s+', '', expr)
# Basic validation
if re.match(r'^[\d+\-*/().\s]+$', expr):
return f"result = {expr}\nprint(int(result) if result == int(result) else result)"
# Fallback for square root
if 'square root' in question.lower():
match = re.search(r'\d+', question)
if match:
num = match.group()
return f"import math\nresult = math.sqrt({num})\nprint(int(result) if result == int(result) else result)"
# Fallback for average
if 'average' in question.lower() or 'mean' in question.lower():
numbers = re.findall(r'\d+', question)
if numbers:
# Convert to integers explicitly
numbers_list = [int(n) for n in numbers]
return f"values = {numbers_list}\nresult = sum(values) / len(values)\nprint(int(result) if result == int(result) else result)"
# Default fallback
return "print('Unable to generate code for this question')"
def execute(self, code: str) -> ExecutionResult:
"""
Execute Python code safely with timeout.
Args:
code: Python code to execute
Returns:
ExecutionResult with output or error
"""
start_time = time.time()
# Safety check: block dangerous operations
dangerous_patterns = {
'import os': 'os module',
'import subprocess': 'subprocess module',
'import sys': 'sys module',
'import urllib': 'urllib module',
'import requests': 'requests module',
'import http': 'http module',
'import socket': 'socket module',
'open(': 'file operations',
'__import__': '__import__ function',
'eval(': 'eval function',
'exec(': 'exec function',
'compile(': 'compile function',
}
code_lower = code.lower()
# Check for dangerous patterns
for pattern, name in dangerous_patterns.items():
if pattern in code_lower:
# Only allow math and statistics imports
if 'import' in pattern and pattern not in ['import math', 'import statistics']:
# Check if it's actually importing something safe
if not any(safe in code_lower for safe in ['import math', 'import statistics', 'import random', 'import datetime']):
if pattern in code_lower:
return ExecutionResult(
success=False,
output=None,
error=f"Security: {name} is not allowed",
execution_time=time.time() - start_time
)
# Block file/exec operations outright
elif pattern in ['open(', '__import__', 'eval(', 'exec(', 'compile(']:
return ExecutionResult(
success=False,
output=None,
error=f"Security: {name} is not allowed",
execution_time=time.time() - start_time
)
# Create temporary file for code
try:
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
f.write(code)
code_file = f.name
# Execute with timeout using subprocess
try:
result = subprocess.run(
[sys.executable, code_file],
capture_output=True,
text=True,
timeout=self.timeout,
env={**os.environ, 'PYTHONPATH': str(Path(__file__).parent)}
)
execution_time = time.time() - start_time
if result.returncode == 0:
output = result.stdout.strip()
return ExecutionResult(
success=True,
output=output,
error=None,
execution_time=execution_time
)
else:
return ExecutionResult(
success=False,
output=None,
error=result.stderr.strip(),
execution_time=execution_time
)
except subprocess.TimeoutExpired:
return ExecutionResult(
success=False,
output=None,
error=f"Execution timeout ({self.timeout}s)",
execution_time=self.timeout
)
except Exception as e:
return ExecutionResult(
success=False,
output=None,
error=str(e),
execution_time=time.time() - start_time
)
finally:
# Clean up temp file
try:
if 'code_file' in locals():
os.unlink(code_file)
except:
pass
def solve_question(self, question: str, context: Optional[str] = None) -> Optional[str]:
"""
Complete workflow: generate code, execute, return answer.
Args:
question: Question to solve
context: Optional context
Returns:
Answer string or None if failed
"""
print(f" 🧮 CODE EXECUTION: {question[:60]}...")
# Generate code
code = self.generate_code(question, context)
print(f" 📝 Generated code ({len(code)} chars)")
# Execute code
result = self.execute(code)
if result.success and result.output:
print(f" ✅ Execution successful: {result.output}")
return result.output
else:
print(f" ❌ Execution failed: {result.error}")
return None
if __name__ == "__main__":
# Test the code executor
print("=" * 60)
print("Code Executor Test")
print("=" * 60)
executor = CodeExecutor()
# Test 1: Simple arithmetic
question1 = "What is 123 * 456?"
print(f"\nTest 1: {question1}")
answer1 = executor.solve_question(question1)
print(f"Answer: {answer1}")
# Test 2: Average
question2 = "What is the average of 10, 20, 30, 40, 50?"
print(f"\nTest 2: {question2}")
answer2 = executor.solve_question(question2)
print(f"Answer: {answer2}")
# Test 3: Square root
question3 = "What is the square root of 144?"
print(f"\nTest 3: {question3}")
answer3 = executor.solve_question(question3)
print(f"Answer: {answer3}")
|