-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
executable file
·563 lines (495 loc) · 20.7 KB
/
run.py
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#!/usr/bin/env python3
"""
This is a work in progress
"""
import re
import os
from copy import copy
import json
import datetime
import argparse
from subprocess import check_call
from openai_helper import (
ask_chatgpt,
NO_CODE_GEN,
NO_TEMPLATE_REGEN,
SKELETON_REGEN,
SKELETON_REGEN2,
SKELETON_REGEN3,
BASE_HTML,
)
file_dir = os.path.dirname(__file__)
parser = argparse.ArgumentParser(description="Generate a program using TSL")
parser.add_argument(
"-d", "--dir", required=True, help="Directory of the prompts to generate from"
)
parser.add_argument(
"-m",
"--method",
default="nl",
choices=["nl", "nls"],
help='"nl" means generate using only the natural language promps, "nls" means also use the TSL spec, and "nl+spec+synth" means also use the synthesized code.',
)
parser.add_argument(
"-l",
"--lang",
default="JavaScript",
help="Target generated implementation programming language (default: JavaScript)",
)
parser.add_argument(
"--no-openai",
action="store_true",
help="Do not query the openai api and instead output prompts for the user to paste into AI chats.",
)
parser.add_argument(
"--spec-prompt-file",
default="Spec_template.prompt",
help="filename to use for the spec prompt",
)
parser.add_argument(
"--log-results",
action="store_true",
help='Copy the affected "{Experiment}/computed" dir into "results" with the timestamp"',
)
parser.add_argument(
"--num-iter", default=1, help="repeat the experiment [num-iter] times"
)
parser.add_argument(
"--regen-html", default=False, action="store_true", help="regenerate the template?"
)
parser.add_argument( # defaults to 3.5-t for debug and testing
"--model", default="gpt-3.5-turbo", help="which model to use from openai api"
)
parser.add_argument("--trusted", default=False, action="store_true")
parser.add_argument("--llm-only", default=False, action="store_true")
parser.add_argument("--llmtsl", default="tsl")
args = parser.parse_args()
>>>>>>> big
>>>>>>> 5035c3b09a82170526a4fd85bf615b9e7f9269d6
def run_with_args(args):
print(datetime.datetime.now(), "Running with args", args)
# handle repeated iterations
if 1 < int(args.num_iter):
for _ in range(int(args.num_iter)):
new_args = copy(args)
new_args.num_iter = 1
run_with_args(new_args)
return
# handle "run all benchmarks"
if args.dir.lower() == "all":
for benchmark in json.load(open(os.path.join(file_dir, "benchmarks.json"))):
new_args = copy(args)
new_args.dir = benchmark
run_with_args(new_args)
return
# handle LOPSTR experiment
if args.spec_prompt_file.lower() == "lopstr":
for prompt_file in [
"Spec_template.prompt",
"Spec_onlyNlAndSummary_template.prompt",
"Spec_onlySummaryAndHeaders_template.prompt",
]:
new_args = copy(args)
new_args.spec_prompt_file = prompt_file
run_with_args(new_args)
return
to_interpolate_tsl = {
# "few_shot_header": "shotPrompt.txt",
"functions_and_predicates": "Headers.txt",
"natural_language_summary": "NL.summary.txt",
"natural_language_description": "NL.txt",
"wrapper_api": "Wrapper_api.js",
}
to_interpolate_llm = {
"functions_and_predicates": "Headers.txt",
"natural_language_summary": "NL.summary.txt",
"natural_language_description": "NL.txt",
"html_temp": "wrapper_template.html",
}
computed_dir = os.path.join(args.dir, "computed")
if not os.path.exists(computed_dir):
os.makedirs(computed_dir)
with open(os.path.join(computed_dir, "args.json"), "w") as file:
json.dump(args.__dict__, file, indent=2)
def check_content_between_markers(filename, start_marker, end_marker):
with open(os.path.join(args.dir, filename), "r") as file:
content = file.read()
start_index = content.find(start_marker) + len(start_marker)
end_index = content.find(end_marker)
if start_index == -1 or end_index == -1 or end_index <= start_index:
return False # Markers not found or in the wrong order
return content[start_index:end_index].strip() == ""
use_no_assumptions = check_content_between_markers(
"NL.txt", "Assumptions:", "Guarantees:"
)
use_no_functions = check_content_between_markers(
"Headers.txt", "Functions:", "Predicates:"
)
if use_no_assumptions and use_no_functions:
spec_template = "Spec_withoutFA_template.prompt"
impl_template = "Impl_withoutFunctions_template.prompt"
elif use_no_assumptions:
spec_template = "Spec_withoutAssumptions_template.prompt"
elif use_no_functions:
spec_template = "Spec_withoutFunctions_template.prompt"
impl_template = "Impl_withoutFunctions_template.prompt"
else:
spec_template = "Spec_template.prompt"
impl_template = "Impl_template.prompt"
# override with filename
if args.spec_prompt_file != "Spec_template.prompt":
spec_template = args.spec_prompt_file
def load_file_and_interpolate(filename, llm=False):
to_interpolate = to_interpolate_llm if llm else to_interpolate_tsl
with open(filename) as file:
result = file.read()
for name, filename in to_interpolate.items():
path = os.path.join(args.dir, filename)
with open(path) as file:
replacement = file.read()
for to_replace in [
"[[" + name.upper() + "_GO_HERE]]",
"[[" + name.upper() + "_GOES_HERE]]",
]:
result = result.replace(to_replace, replacement)
result = result.replace("[[LANGUAGE_GOES_HERE]]", args.lang)
return result
def output_error(message):
print(message)
with open(os.path.join(computed_dir, "err.log"), "a") as file:
file.writelines([message])
log_results()
<<<<<<< HEAD
def log_results():
if args.method == "no_tsl":
template_filename = "No_tsl.prompt"
else:
template_filename = spec_template
log_outer_dir = os.path.join(
file_dir, "results", "by_benchmark", args.dir, template_filename
=======
def log_results(exp=args.llmtsl, trusted=args.trusted):
log_outer_dir = os.path.join(
file_dir,
"results",
f"{exp}_by_benchmark_{'regen_with_import' if trusted else 'regen_no_import'}",
args.dir,
spec_template,
args.model,
>>>>>>> big
)
if not os.path.exists(log_outer_dir):
os.makedirs(log_outer_dir)
log_dir = os.path.join(log_outer_dir, datetime.datetime.now().isoformat())
if args.verbose:
print("Moving computed dir to", log_dir)
check_call(["mv", computed_dir, log_dir])
print(f"{log_dir}")
def extract_first_code_block(text):
match = re.search(r"```.*?\n((?:.|\n)+?)```", text)
if match == None:
return None
else:
return match[1]
if args.method == "nl":
num_retries = 10
tries = 0
realized = False
while tries < num_retries and not realized:
# TODO add retry loop for ill formed specs here. Add some prompt that passes the erorr message with a tesmplate back to llm
spec_template_path = os.path.join(
file_dir, spec_template
) # use dynamically chosen spec template
spec_filename = os.path.join(computed_dir, "Spec.tsl")
synth_filename = os.path.join(computed_dir, "Synth.js")
spec_response_filename = os.path.join(computed_dir, "Spec_response.txt")
spec_prompt = load_file_and_interpolate(spec_template_path)
spec_prompt_filename = os.path.join(computed_dir, "Spec.prompt")
with open(spec_prompt_filename, "w") as file:
file.write(spec_prompt)
if args.no_openai:
print(
f"Please paste the contents of this file into the TSL GPT:\n\n {spec_prompt_filename}\n\nThen, paste the spec in the response into:\n\n {spec_filename}\n\nWhen you have done this, press Enter"
)
input()
else:
response = ask_chatgpt(spec_prompt, args.model)
code_block = extract_first_code_block(
response.choices[0].message.content
)
with open(spec_response_filename, "w") as file:
file.write(response.choices[0].message.content)
if code_block == None:
return output_error(
f"No valid code block in response. See {spec_response_filename}"
)
else:
with open(spec_filename, "w") as file:
file.write(code_block)
try:
check_call(
[
"tsl",
"synthesize",
"-i",
spec_filename,
"--js",
"-o",
synth_filename,
]
)
realized = True
except BaseException as e:
print(f"Error synthesizing: {str(e)}")
tries += 1
if tries == num_retries:
return output_error(str(e))
elif args.method == "nls":
# prompt_templates = (
# SKELETON_REGEN.format(spec_prompt, wrapper_contents, code_block),
# )
spec_template_path = os.path.join(file_dir, spec_template)
spec_filename = os.path.join(computed_dir, "Spec.tsl")
synth_filename = os.path.join(computed_dir, "Synth.js")
spec_response_filename = os.path.join(computed_dir, "Spec_response.txt")
with open(os.path.join(file_dir, args.dir, "Headers.txt"), "r") as headers_file:
headers = headers_file.read()
with open(
os.path.join(file_dir, args.dir, "NL.summary.txt"), "r"
) as nl_summ_file:
nl_summ = nl_summ_file.read()
with open(os.path.join(file_dir, args.dir, "NL.txt"), "r") as nl_desc_file:
nl_desc = nl_desc_file.read()
with open(
os.path.join(file_dir, args.dir, "wrapper_template.html"), "r"
) as html_temp_file:
html_wrapp = html_temp_file.read()
spec_prompt = SKELETON_REGEN.format(nl_summ, nl_desc, headers, " ")
response = ask_chatgpt(
f"{spec_prompt}\nGenerate using the above as inspiration a working html file which implements the described program",
args.model,
)
code_block = extract_first_code_block(response.choices[0].message.content)
with open(spec_response_filename, "w") as file:
file.write(response.choices[0].message.content)
if code_block == None:
return output_error(
f"No valid code block in response. See {spec_response_filename}"
)
else:
dir_name = os.path.basename(os.path.normpath(args.dir))
output_html_filename = f"{dir_name}.html"
output_html_path = os.path.join(computed_dir, output_html_filename)
with open(output_html_path, "w") as file:
file.write(code_block)
log_results()
return 0
<<<<<<< HEAD
try:
check_call(
["timeout", str(args.tsl_timeout), "tsl", "synthesize", "-i", spec_filename, "--js", "-o", synth_filename]
)
except BaseException as e:
return output_error(str(e))
elif args.method == "no_tsl":
impl_template_path = os.path.join(
file_dir, "No_tsl.prompt"
)
impl_response_filename = os.path.join(computed_dir, "Impl_response.txt")
impl_filename = os.path.join(computed_dir, "Impl.js")
impl_prompt = load_file_and_interpolate(impl_template_path)
impl_prompt_filename = os.path.join(computed_dir, "No_tsl.prompt")
with open(impl_prompt_filename, "w") as file:
file.write(impl_prompt)
if args.no_openai:
print(
f"Please paste the contents of this file into the TSL GPT:\n\n {impl_prompt_filename}\n\nThen, paste the first code block in the response into:\n\n {impl_filename}\n\nWhen you have done this, press Enter"
)
input()
else:
response = ask_chatgpt(impl_prompt, args.model)
code_block = extract_first_code_block(response.choices[0].message.content)
with open(impl_response_filename, "w") as file:
file.write(response.choices[0].message.content)
if code_block == None:
return output_error(
f"No valid code block in response. See {impl_response_filename}"
)
else:
with open(impl_filename, "w") as file:
file.write(code_block)
=======
>>>>>>> big
else:
spec_filename = os.path.join(args.dir, spec_template)
raise Exception('the only method implemented is "nl". the others are TODO.')
if args.method != 'no_tsl':
impl_prompt_filename = os.path.join(computed_dir, "Impl.prompt")
impl_filename = os.path.join(computed_dir, "Impl.js")
impl_template_path = os.path.join(file_dir, impl_template)
impl_prompt = load_file_and_interpolate(impl_template_path)
impl_response_filename = os.path.join(computed_dir, "Impl_response.txt")
with open(impl_prompt_filename, "w") as file:
file.write(impl_prompt)
if args.no_openai:
print(
f"Please paste the contents of this file into the TSL GPT:\n\n {impl_prompt_filename}\n\nThen, paste the spec in the response into:\n\n {impl_filename}\n\nWhen you have done this, press Enter"
)
input()
else:
response = ask_chatgpt(impl_prompt)
code_block = extract_first_code_block(response.choices[0].message.content)
with open(impl_response_filename, "w") as file:
file.write(response.choices[0].message.content)
if code_block == None:
return output_error(
f"No valid code block in response. See {impl_response_filename}"
)
else:
with open(impl_filename, "w") as file:
file.write(code_block)
dir_name = os.path.basename(os.path.normpath(args.dir))
output_html_filename = f"{dir_name}.html"
wrapper_template_path = os.path.join(args.dir, "wrapper_template.html")
output_html_path = os.path.join(
computed_dir, output_html_filename
) # Output file path in the computed directory with the directory name
if os.path.exists(wrapper_template_path):
with open(wrapper_template_path, "r") as file:
wrapper_contents = file.read()
# Nik - added new flag, uses code block to seed gpt generation of code
if args.regen_html:
print("Using synthesized code to regenerate html")
# synth filename and impl filename
spec_template_path = os.path.join(file_dir, spec_template)
spec_filename = os.path.join(computed_dir, "Spec.tsl")
synth_filename = os.path.join(computed_dir, "Synth.js")
spec_response_filename = os.path.join(computed_dir, "Spec_response.txt")
with open(
os.path.join(file_dir, args.dir, "Headers.txt"), "r"
) as headers_file:
headers = headers_file.read()
with open(
os.path.join(file_dir, args.dir, "NL.summary.txt"), "r"
) as nl_summ_file:
nl_summ = nl_summ_file.read()
with open(os.path.join(file_dir, args.dir, "NL.txt"), "r") as nl_desc_file:
nl_desc = nl_desc_file.read()
with open(
os.path.join(file_dir, args.dir, "computed/Synth.js"), "r"
) as html_temp_file:
synth_code = html_temp_file.read()
with open(
os.path.join(file_dir, args.dir, "computed/Impl.js"), "r"
) as html_temp_file:
impl_code = html_temp_file.read()
with open(
os.path.join(file_dir, args.dir, "computed/Spec.tsl"), "r"
) as html_temp_file:
spectsl = html_temp_file.read()
if args.trusted:
regen_prompt = SKELETON_REGEN2.format(
spec_prompt, impl_prompt, "", synth_code, spectsl
)
else:
regen_prompt = SKELETON_REGEN3.format(
synth_code, impl_code, spectsl, nl_summ, nl_desc
)
response = ask_chatgpt(f"{regen_prompt}", args.model)
code_block = extract_first_code_block(response.choices[0].message.content)
with open(spec_response_filename, "w") as file:
file.write(response.choices[0].message.content)
if code_block == None:
return output_error(
f"No valid code block in response. See {spec_response_filename}"
)
else:
dir_name = os.path.basename(os.path.normpath(args.dir))
output_html_filename = f"{dir_name}.html"
output_html_path = os.path.join(computed_dir, output_html_filename)
with open(output_html_path, "w") as file:
file.write(code_block)
log_results()
return 0
print("Inserting generated code into base template")
with open(impl_filename, "r") as file:
impl_contents = file.read()
placeholder_comment = "//[[GENERATED_FUNCTIONS_AND_PREDICATES_GO_HERE]]"
if placeholder_comment in wrapper_contents:
modified_wrapper = wrapper_contents.replace(
placeholder_comment, impl_contents
)
with open(output_html_path, "w") as file:
file.write(modified_wrapper)
print(
f"The Impl.js contents have been successfully inserted into {output_html_path}."
)
else:
return output_error(
"The placeholder comment does not match in the wrapper_template.html. Please ensure it is '//[[GENERATED_FUNCTIONS_AND_PREDICATES_GO_HERE]]'."
)
else:
return output_error(
"Please include a wrapper_template.html file with a line of comment '//[[GENERATED_FUNCTIONS_AND_PREDICATES_GO_HERE]]' inside the file to get the generated wrapper HTML."
)
log_results()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Generate a program using TSL")
parser.add_argument(
"-d", "--dir", required=True, help="Directory of the prompts to generate from"
)
parser.add_argument(
"-m",
"--method",
default="nl",
choices=["nl", "nl+spec", "nl+spec+synth", "no_tsl"],
help='"nl" means generate using only the natural language promps, "nl+spec" means also use the TSL spec, and "nl+spec+synth" means also use the synthesized code.',
)
parser.add_argument(
"-l",
"--lang",
default="JavaScript",
help="Target generated implementation programming language (default: JavaScript)",
)
parser.add_argument(
"--no-openai",
action="store_true",
help="Do not query the openai api and instead output prompts for the user to paste into AI chats.",
)
parser.add_argument(
"--model",
default='gpt-4-turbo',
help="The openai model (e.g. \"gpt-4-turbo\")",
)
parser.add_argument(
"--spec-prompt-file",
default="Spec_template.prompt",
help="filename to use for the spec prompt",
)
parser.add_argument(
"--log-results",
action="store_true",
help='Copy the affected "{Experiment}/computed" dir into "results" with the timestamp"',
)
parser.add_argument(
"--num-iter", default=1, help="repeat the experiment [num-iter] times"
)
parser.add_argument(
"--regen-html",
default=False,
action="store_true",
help="regenerate the template?",
)
parser.add_argument(
"--verbose",
action="store_true",
help="print messages other than errors.",
)
parser.add_argument(
"--tsl-timeout",
type=int,
default=120,
help="TSL synthesis timeout",
)
args = parser.parse_args()
run_with_args(args)