-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpythoness.py
More file actions
453 lines (394 loc) · 18.7 KB
/
Copy pathpythoness.py
File metadata and controls
453 lines (394 loc) · 18.7 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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import inspect
import traceback
from pythoness.util import runtime_bounds_testing
from .util import assistant
from .util import database
from .util import timeout
from .util import helper_funcs
from .util import logger
from .util import testing
from .util import prompt_helpers
from .util import config
from .util import runtime_testing
from contextlib import nullcontext
from functools import wraps
import sys
import signal
import termcolor
import re
# exec() pushes function to the global scope
# globals_no_print ensures they aren't included twice in the prompt
globals_no_print = []
cdb = database.CodeDatabase("pythoness-cache.db")
cost = 0
time = 0
def spec(
spec_string,
model="gpt-4o",
runtime=False,
tolerance=1,
replace=None,
tests=None,
test_descriptions=None,
max_runtime=None, # ms
max_retries=3,
verbose=None,
output=False,
regenerate=False,
related_objs=None,
timeout_seconds=0,
pure=True,
length_func=None,
time_bound=None,
mem_bound=None,
generate_func=None,
range=None,
# the next fields are really for recursive calls from run-time test failures
# TODO: move these to a separate data structure, perhaps stored in the db...
generation_reason: str | None = None,
function_template: str | None = None,
llm_tests=True,
):
"""Main logic of Pythoness"""
if verbose is None:
verbose = config.config.verbose_flag
if replace is None:
replace = config.config.replace_flag
# These options conflict with each other when it comes to inserting
# run-time checks for the function.
if max_runtime is not None and time_bound is not None:
raise ValueError("Cannot specify both max_runtime and time_bound")
# Store Pythoness settings to propagate in generated function
all_params = locals()
initial_pythoness_args = {k: v for k, v in all_params.items()}
def pythoness_args_as_string(**updated_kwargs):
return (
"("
+ (
", ".join(
f"{k}={repr(v)}" if isinstance(v, str) else f"{k}={v}"
for k, v in {**initial_pythoness_args, **updated_kwargs}.items()
)
)
+ ")"
)
def decorator(func):
cached_function = None
log = logger.Logger(quiet=config.config.quiet_flag)
# enables interrelated function generation
if func.__doc__:
func.__doc__ += spec_string
else:
func.__doc__ = spec_string
@wraps(func)
def wrapper(*args, **kwargs):
nonlocal cached_function
if regenerate:
# Clear the cached function if we are regenerating.
cached_function = None
# If we've already built this function and cached it, just
# run it
if cached_function:
return cached_function(*args, **kwargs)
with log("Start") if verbose else nullcontext():
client = assistant.Assistant(model=model)
# We need to generate a function from the spec.
# We populate the prompt with the function's name, argument name and types, and the return type.
with (
log("[Pythoness] Getting function info...")
if verbose
else nullcontext()
):
try:
# function_info = helper_funcs.get_function_info(func, *args, **kwargs)
function_info = helper_funcs.get_function_info(func)
except Exception as e:
print("Exception", str(e))
traceback.print_exc()
raise e
nonlocal initial_pythoness_args
nonlocal function_template
if function_template is None:
function_template = inspect.getsource(func)
match = re.search(r"\n\s*def ", function_template)
if match:
start = match.start()
else:
start = -1
t = function_template[start:]
initial_pythoness_args["function_template"] = t
with (
log("[Pythoness] Creating prompt and checking the DB...")
if verbose
else nullcontext()
):
prompt = prompt_helpers.create_prompt(
function_info,
spec_string,
tests,
time_bound,
mem_bound,
func,
related_objs,
globals_no_print,
generation_reason,
initial_pythoness_args["function_template"],
)
function_info = helper_funcs.setup_info(
function_info, func, spec_string, prompt
)
# See if we already have code corresponding with that prompt in the database.
if regenerate:
# Force regeneration by ignoring any existing code in the database
function_def = None
else:
function_def = cdb.get_code(prompt)
if verbose and not function_def:
log.log("[Pythoness] Prompt:\n", prompt)
# We have previously loaded the function. Just execute it and return.
if function_def:
if verbose:
log.log(
"[Pythoness] Retrieved function from database:\n",
function_def,
)
if replace:
with (
log("[Pythoness] Replacing...")
if verbose
else nullcontext()
):
helper_funcs.replace_func(func, function_def)
globals_no_print.append(function_info["function_name"])
fn = helper_funcs.database_compile(
function_info,
function_def,
length_func,
time_bound,
mem_bound,
)
return fn(*args, **kwargs)
with (
log("[Pythoness] Generating code...") if verbose else nullcontext()
):
while function_info["retries"] < max_retries:
signal.signal(signal.SIGALRM, timeout.timeout_handler)
signal.alarm(timeout_seconds)
try:
function_info["retries"] += 1
if verbose:
log.log(
f'[Pythoness] Attempt {function_info["retries"]}'
)
if function_info["retries"] > 0:
log.log(f"[Pythoness] New prompt:\n\n{prompt}\n")
with (
log("[Pythoness] Parsing...")
if verbose
else nullcontext()
):
function_info = helper_funcs.parse_func(
function_info,
client,
prompt,
verbose,
log,
)
# store code in database so the tests can run it
cdb.insert_code(
function_info["original_prompt"],
function_info["function_def"],
)
with (
log("[Pythoness] Compiling and executing...")
if verbose
else nullcontext()
):
function_info = helper_funcs.compile_func(function_info)
function_info = helper_funcs.execute_func(
function_info, max_runtime
)
fn = function_info["globals"][
function_info["function_name"]
]
with (
log("[Pythoness] Validating types...")
if verbose
else nullcontext()
):
testing.validate_types(func, fn, function_info)
all_tests = []
property_tests = []
# If tests are provided, generate them
if (tests or test_descriptions) and pure:
with (
log("[Pythoness] Generating tests...")
if verbose
else nullcontext()
):
all_tests, property_tests = (
testing.generate_user_tests(
function_info,
tests,
test_descriptions,
client,
log,
verbose,
llm_tests,
)
)
# If tests are not provided, but function is pure, ask LLM to generate
elif pure:
with (
log("[Pythoness] Generating tests...")
if verbose
else nullcontext()
):
all_tests, property_tests = (
testing.generate_llm_tests(
function_info,
client,
log,
verbose,
llm_tests,
)
)
# Vaildate all tests, if any generated
if all_tests:
with (
log("[Pythoness] Validating tests...")
if verbose
else nullcontext()
):
testing.validate_tests(
function_info,
all_tests,
log,
verbose,
)
if time_bound or mem_bound:
with (
log("[Pythoness] Validating time/mem bound...")
if verbose
else nullcontext()
):
# interpreter gets weird when I reassign generate_func
if isinstance(generate_func, str):
generator = testing.generate_generator_func(
generate_func,
fn,
function_info,
model,
log,
verbose,
)
else:
generator = generate_func
testing.validate_runtime(
function_info,
generator,
length_func,
range,
time_bound,
mem_bound,
log,
verbose,
)
if runtime:
with (
log(
"[Pythoness] Adding runtime bounds checks..."
)
if verbose
else nullcontext()
):
function_info = runtime_bounds_testing.add_runtime_bounds_testing(
function_info,
initial_pythoness_args,
fn,
log,
verbose,
cdb,
)
function_info["globals"][
function_info["function_name"]
] = function_info["compiled"]
# Do not add runtime testing if not turned on, if no property tests, or if impure
if not runtime or not property_tests or not pure:
# Validated. Cache the function and persist it
cached_function = fn
else:
if verbose:
log.log(
"[Pythoness] Adding runtime testing framework..."
)
function_info = runtime_testing.add_runtime_testing(
function_info,
property_tests,
pythoness_args_as_string(),
tolerance,
max_runtime,
client,
func,
log,
verbose,
cdb,
)
cached_function = function_info["globals"][
function_info["function_name"]
]
if output:
print(
termcolor.colored(
f"\n[Pythoness] Output:\n{function_info['function_def']}\n",
"green",
),
file=sys.stdout,
)
# TODO: this won't have the check() wrapper
if replace:
with (
log("[Pythoness] Replacing...")
if verbose
else nullcontext()
):
helper_funcs.replace_func(
func, function_info["function_def"]
)
globals_no_print.append(function_info["function_name"])
return cached_function(*args, **kwargs)
except Exception as e:
print("Exception", str(e))
traceback.print_exc()
try:
func.__globals__.pop(function_info["function_name"])
except:
pass
# code is stored in database so tests can run it, need to make sure it's cleared
cdb.delete_code(function_info["original_prompt"])
prompt = helper_funcs.exception_handler(
e, verbose, log, func, related_objs, globals_no_print
)
continue
# ensures regeneration on future attempts
# KeyboardIntterupts are not caught by general exceptions
except KeyboardInterrupt:
cdb.delete_code(function_info["original_prompt"])
sys.exit()
except SystemExit:
cdb.delete_code(function_info["original_prompt"])
sys.exit()
finally:
signal.alarm(0)
if verbose:
global cost, time
cost += assistant.Assistant.total_cost
time += assistant.Assistant.total_time
log.log(f"\n[Total cost so far: ~${cost:.2f} USD]")
log.log(f"\n[Total time so far: {time}]")
# If we got here, we had too many retries.
# ensure that nothing is in the DB to interfere with a future call
cdb.delete_code(function_info["original_prompt"])
raise Exception(f"Maximum number of retries exceeded ({max_retries}).")
return wrapper
return decorator