-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_script_template.py
More file actions
288 lines (241 loc) · 9.47 KB
/
test_script_template.py
File metadata and controls
288 lines (241 loc) · 9.47 KB
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
import sys
import traceback
import os
import json
import datetime
import inspect
import functools
import importlib.util
# Add the scripts directory to the path
sys.path.append("{scripts_dir}")
# Configure tracing
trace_file = "{trace_file}"
os.makedirs(os.path.dirname(trace_file), exist_ok=True)
def call_llm(prompt, system_instruction=None):
try:
from google import genai
from google.genai import types
import os # Import the os module
# Initialize the Gemini client
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
# Call the API with system instruction if provided
if system_instruction:
response = client.models.generate_content(
model="gemini-2.0-flash",
config=types.GenerateContentConfig(
system_instruction=system_instruction
),
contents=prompt
)
else:
response = client.models.generate_content(
model="gemini-2.0-flash",
contents=prompt
)
return response.text
except Exception as e:
print("Error calling Gemini API: " + str(e))
return "Error: " + str(e)
def execute_code(code_str, timeout=10):
"""Execute Python code with automatic package installation and proper scoping"""
import sys
import re
import subprocess
from io import StringIO
print(" [SYSTEM] Auto-installing execute_code() with scope fix")
# Clean markdown formatting
patterns = [
r'```python\s*\n(.*?)\n```',
r'```python\s*(.*?)```',
r'```\s*\n(.*?)\n```',
r'```\s*(.*?)```'
]
cleaned_code = code_str.strip()
for pattern in patterns:
match = re.search(pattern, code_str, re.DOTALL | re.IGNORECASE)
if match:
cleaned_code = match.group(1).strip()
print(" [CLEANING] Removed markdown")
break
# Function to install a package
def install_package(package_name):
try:
print(" [INSTALLING] Installing " + package_name + "...")
result = subprocess.run([
sys.executable, "-m", "pip", "install", package_name
], capture_output=True, text=True, timeout=30)
if result.returncode == 0:
print(" [SUCCESS] " + package_name + " installed successfully")
return True
else:
print(" [FAILED] Could not install " + package_name + ": " + result.stderr)
return False
except Exception as e:
print(" [ERROR] Installation error: " + str(e))
return False
# Execute with proper scoping and auto-installation retry
max_install_attempts = 3
attempt = 0
while attempt <= max_install_attempts:
old_stdout = sys.stdout
old_stderr = sys.stderr
stdout_capture = StringIO()
stderr_capture = StringIO()
try:
sys.stdout = stdout_capture
sys.stderr = stderr_capture
# CRITICAL FIX: Provide explicit globals and locals
# This ensures imports are available to functions defined in the code
exec_namespace = {{}}
exec(cleaned_code, exec_namespace, exec_namespace)
# Success!
sys.stdout = old_stdout
sys.stderr = old_stderr
output = stdout_capture.getvalue().strip()
return output if output else "Code executed successfully"
except ModuleNotFoundError as e:
sys.stdout = old_stdout
sys.stderr = old_stderr
# Extract the missing module name
module_name = str(e).split("'")[1] if "'" in str(e) else None
if module_name and attempt < max_install_attempts:
print(" [MISSING] Module '" + module_name + "' not found, attempting to install...")
# Try to install the missing package
if install_package(module_name):
attempt += 1
print(" [RETRY] Retrying code execution (attempt " + str(attempt + 1) + ")...")
continue
else:
return "Error: Could not install required package '" + module_name + "'"
else:
return "Error: " + str(e)
except Exception as e:
sys.stdout = old_stdout
sys.stderr = old_stderr
return "Error: " + str(e)
attempt += 1
return "Error: Maximum installation attempts exceeded"
# Trace entry for execution start
with open(trace_file, 'a', encoding='utf-8') as f:
start_entry = {{
"timestamp": datetime.datetime.now().isoformat(),
"event": "execution_start",
"iteration": {current_iteration},
"sample_id": "{sample_id}",
"question": {question_repr}
}}
f.write(json.dumps(start_entry) + "\n")
# More reliable method for getting caller information
def get_real_caller():
"""Get information about the caller, skipping intermediate functions like wrappers and decorators."""
frames = inspect.stack()
# Skip first 2 frames (this function and immediate caller)
for frame_info in frames[2:]:
# Get the frame's module
frame_module = frame_info.frame.f_globals.get('__name__', '')
# If this frame is from our module (not from system libraries)
if frame_module == 'current_script_{current_iteration}':
# Check if it's not the call_llm function itself
if frame_info.function != 'call_llm' and 'wrapper' not in frame_info.function:
return {{
"function": frame_info.function,
"filename": frame_info.filename,
"lineno": frame_info.lineno
}}
# Fallback if we can't find a suitable caller
return {{"function": "unknown", "filename": "unknown", "lineno": 0}}
# Create a tracing decorator for call_llm
def trace_call_llm(func):
@functools.wraps(func)
def wrapper(prompt, system_instruction=None):
# Get caller information using our improved method
caller_info = get_real_caller()
# Create trace entry with caller information
trace_entry = {{
"timestamp": datetime.datetime.now().isoformat(),
"event": "llm_call",
"iteration": {current_iteration},
"sample_id": "{sample_id}",
"function": "call_llm",
"caller": caller_info,
"input": {{
"prompt": prompt,
"system_instruction": system_instruction
}}
}}
# Call the original function
try:
result = func(prompt, system_instruction)
# Log successful response
trace_entry["output"] = result
trace_entry["status"] = "success"
with open(trace_file, 'a', encoding='utf-8') as f:
f.write(json.dumps(trace_entry) + "\n")
return result
except Exception as e:
# Log error
trace_entry["error"] = str(e)
trace_entry["status"] = "error"
trace_entry["traceback"] = traceback.format_exc()
with open(trace_file, 'a', encoding='utf-8') as f:
f.write(json.dumps(trace_entry) + "\n")
raise
return wrapper
try:
# Import the script as a module
spec = importlib.util.spec_from_file_location(
"current_script_{current_iteration}",
"{script_path}"
)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
# INJECT BOTH FUNCTIONS
module.execute_code = execute_code
module.call_llm = call_llm
# Patch call_llm function if it exists
if hasattr(module, 'call_llm'):
original_call_llm = module.call_llm
module.call_llm = trace_call_llm(original_call_llm)
# Also patch any other functions that might call LLM directly
for name, obj in inspect.getmembers(module):
if inspect.isfunction(obj) and obj.__module__ == module.__name__:
try:
source = inspect.getsource(obj)
if 'generate_content' in source and obj is not getattr(module, 'call_llm', None):
setattr(module, name, trace_call_llm(obj))
except:
pass
# Execute the main function with the question string
question = {question_repr}
# Call the main function and get the answer
answer = module.main(question)
# Log execution completion
with open(trace_file, 'a', encoding='utf-8') as f:
end_entry = {{
"timestamp": datetime.datetime.now().isoformat(),
"event": "execution_complete",
"iteration": {current_iteration},
"sample_id": "{sample_id}",
"answer": str(answer)
}}
f.write(json.dumps(end_entry) + "\n")
# Print the answer for capture
print("ANSWER_START")
print(answer)
print("ANSWER_END")
except Exception as e:
# Log the error
with open(trace_file, 'a', encoding='utf-8') as f:
error_entry = {{
"timestamp": datetime.datetime.now().isoformat(),
"event": "execution_error",
"iteration": {current_iteration},
"sample_id": "{sample_id}",
"error": str(e),
"traceback": traceback.format_exc()
}}
f.write(json.dumps(error_entry) + "\n")
print("ERROR_START")
print(str(e))
print(traceback.format_exc())
print("ERROR_END")