-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_gpt_prompt.py
643 lines (497 loc) · 21.2 KB
/
run_gpt_prompt.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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
import json
import random
import ast
import re
import sys
sys.path.append('../')
from utils.settings import *
from utils.global_methods import *
from generative_agent.prompt_template.gpt_structure import *
from generative_agent.prompt_template.print_prompt import *
prompt_dir = "generative_agent/prompt_template/prompts"
##############################################################################
####### HELPER FUNCTIONS #######
##############################################################################
def extract_first_json_dict(input_str):
try:
# Replace curly quotes with standard double quotes
input_str = (input_str.replace("“", "\"")
.replace("”", "\"")
.replace("‘", "'")
.replace("’", "'"))
# Find the first occurrence of '{' in the input_str
start_index = input_str.index('{')
# Initialize a count to keep track of open and close braces
count = 1
end_index = start_index + 1
# Loop to find the closing '}' for the first JSON dictionary
while count > 0 and end_index < len(input_str):
if input_str[end_index] == '{':
count += 1
elif input_str[end_index] == '}':
count -= 1
end_index += 1
# Extract the JSON substring
json_str = input_str[start_index:end_index]
# Parse the JSON string into a Python dictionary
json_dict = json.loads(json_str)
return json_dict
except ValueError:
# Handle the case where the JSON parsing fails
return None
##############################################################################
### GENERATIVE AGENT CLASS ###
##############################################################################
def run_gpt_generate_batch_importance(name, records,
test_input=None, verbose=False):
def create_prompt_input(name, records, test_input=None):
record_count = str(len(records))
records_str = ""
for count, r in enumerate(records):
records_str += f"Item {str(count+1)}:\n"
records_str += f"{r}\n"
prompt_input = [record_count, records_str, name]
return prompt_input
def __chat_func_clean_up(gpt_response, prompt=""):
gpt_response = json.loads(gpt_response)
return gpt_response
def __chat_func_validate(gpt_response, prompt=""):
try:
response = __chat_func_clean_up(gpt_response)
return True
except:
return False
def get_fail_safe():
return None
prompt_template = f"{prompt_dir}/batch_importance_v1.txt"
prompt_input = create_prompt_input(name, records)
prompt = generate_prompt(prompt_input, prompt_template)
fail_safe = get_fail_safe()
output = chat_safe_generate(prompt, "GPT4", 5, fail_safe,
__chat_func_validate, __chat_func_clean_up, verbose)
if DEBUG or verbose:
print_run_prompts(prompt_template, prompt_input, prompt, output)
return output, [output, prompt, prompt_input, fail_safe]
def run_gpt_generate_profiler_reflections_v2(name, observation_str,
test_input=None, verbose=False):
def create_prompt_input(name, observation_str, test_input=None):
prompt_input = [observation_str, name, name, name, name, name]
return prompt_input
def __chat_func_clean_up(gpt_response, prompt=""):
gpt_response = list(extract_first_json_dict(gpt_response).values())
return gpt_response
def __chat_func_validate(gpt_response, prompt=""):
try:
response = __chat_func_clean_up(gpt_response)
return True
except:
return False
def get_fail_safe():
return None
prompt_template = f"{prompt_dir}/profiler_reflection_v2.txt"
prompt_input = create_prompt_input(name, observation_str)
prompt = generate_prompt(prompt_input, prompt_template)
fail_safe = get_fail_safe()
output = chat_safe_generate(prompt, "ChatGPT", 5, fail_safe,
__chat_func_validate, __chat_func_clean_up, verbose)
if DEBUG or verbose:
print_run_prompts(prompt_template, prompt_input, prompt, output)
return output, [output, prompt, prompt_input, fail_safe]
def run_gpt_generate_reflections(observation_str,
test_input=None, verbose=False):
def create_prompt_input(observation_str, test_input=None):
prompt_input = [observation_str]
return prompt_input
def __chat_func_clean_up(gpt_response, prompt=""):
gpt_response = list(extract_first_json_dict(gpt_response)["output"])
return gpt_response
def __chat_func_validate(gpt_response, prompt=""):
try:
response = __chat_func_clean_up(gpt_response)
for i in response:
if type(i) != type("a"):
return False
return True
except:
return False
def get_fail_safe():
return None
prompt_template = f"{prompt_dir}/reflection_v1.txt"
prompt_input = create_prompt_input(observation_str)
prompt = generate_prompt(prompt_input, prompt_template)
fail_safe = get_fail_safe()
output = chat_safe_generate(prompt, "ChatGPT", 5, fail_safe,
__chat_func_validate, __chat_func_clean_up, verbose)
if DEBUG or verbose:
print_run_prompts(prompt_template, prompt_input, prompt, output)
return output, [output, prompt, prompt_input, fail_safe]
##############################################################################
### AGENT INTERFACE CLASS ###
##############################################################################
def run_gpt_generate_corr_questions(inquiry, test_input=None, verbose=False):
def create_prompt_input(inquiry, test_input=None):
prompt_input = [inquiry]
return prompt_input
def __chat_func_clean_up(gpt_response, prompt=""):
gpt_response = extract_first_json_dict(
gpt_response)["correlated questions"]
return gpt_response
def __chat_func_validate(gpt_response, prompt=""):
try:
response = __chat_func_clean_up(gpt_response)
except:
return False
def get_fail_safe():
return None
prompt_template = f"{prompt_dir}/corr_inquiry_v1.txt"
prompt_input = create_prompt_input(inquiry)
prompt = generate_prompt(prompt_input, prompt_template)
fail_safe = get_fail_safe()
output = chat_safe_generate(prompt, "ChatGPT", 5, fail_safe,
__chat_func_validate, __chat_func_clean_up, verbose)
if DEBUG or verbose:
print_run_prompts(prompt_template, prompt_input, prompt, output)
return output, [output, prompt, prompt_input, fail_safe]
def run_gpt_generate_inquiry_response_hybrid(name, retrieved_obs_str,
retrieved_ref_str, inquiry, test_input=None, verbose=False):
def create_reason_prompt_input(name, retrieved_obs_str, retrieved_ref_str,
inquiry, test_input=None):
prompt_input = [name, name, name, name,
retrieved_obs_str,
name, name,
retrieved_ref_str,
name, name,
inquiry,
name, name, name, name, name, name,
inquiry]
return prompt_input
def create_prompt_input(name, inquiry, reason_output, test_input=None):
prompt_input = [name, reason_output, inquiry, name]
return prompt_input
def __chat_func_clean_up_reason(gpt_response, prompt=""):
gpt_response = json.loads(gpt_response)
return gpt_response
def __chat_func_clean_up(gpt_response, prompt=""):
return gpt_response
def __chat_func_validate_reason(gpt_response, prompt=""):
try:
fields = ["question 0"]
response = json.loads(gpt_response)
for field in fields:
if field not in response:
return False
return True
except:
return False
def __chat_func_validate(gpt_response, prompt=""):
if gpt_response != None:
return True
else:
return False
def get_fail_safe():
return None
prompt_template_reason = f"{prompt_dir}/inquiry_reason_v1.txt"
prompt_input_reason = create_reason_prompt_input(name, retrieved_obs_str, retrieved_ref_str, inquiry)
prompt_reason = generate_prompt(prompt_input_reason, prompt_template_reason)
fail_safe = get_fail_safe()
reason_output = chat_safe_generate(prompt_reason, "ChatGPT", 5, fail_safe,
__chat_func_validate_reason, __chat_func_clean_up_reason, verbose)["question 0"]
prompt_template = f"{prompt_dir}/inquiry_response_hybrid_v1.txt"
prompt_input = create_prompt_input(name, inquiry, reason_output)
prompt = generate_prompt(prompt_input, prompt_template)
fail_safe = get_fail_safe()
output = nonchat_safe_generate(prompt, 100, "gpt-3.5-turbo-instruct", 5, fail_safe,
__chat_func_validate, __chat_func_clean_up, verbose)
if DEBUG or verbose:
print_run_prompts(prompt_template, prompt_input, prompt, output)
return output, [output, prompt, prompt_input, fail_safe]
def run_gpt_generate_corr_topics(inquiry, test_input=None, verbose=False):
def create_prompt_input(inquiry, test_input=None):
prompt_input = [inquiry]
return prompt_input
def __chat_func_clean_up(gpt_response, prompt=""):
gpt_response = extract_first_json_dict(gpt_response)
gpt_response = list(gpt_response.values())
return gpt_response
def __chat_func_validate(gpt_response, prompt=""):
if verbose:
print (gpt_response)
try:
fields = ["point 1", "point 2", "point 3"]
gpt_response = extract_first_json_dict(gpt_response)
for field in fields:
if field not in gpt_response:
return False
return True
except:
return False
def get_fail_safe():
return None
prompt_template = f"{prompt_dir}/corr_topics_v1.txt"
prompt_input = create_prompt_input(inquiry)
prompt = generate_prompt(prompt_input, prompt_template)
fail_safe = get_fail_safe()
output = chat_safe_generate(prompt, "ChatGPT", 5, fail_safe,
__chat_func_validate, __chat_func_clean_up, verbose)
if DEBUG or verbose:
print_run_prompts(prompt_template, prompt_input, prompt, output)
return output, [output, prompt, prompt_input, fail_safe]
def run_gpt_generate_corr_topics_HyDE(name, inquiry, curr_ret_str, test_input=None, verbose=False):
def create_prompt_input(name, inquiry, curr_ret_str, test_input=None):
prompt_input = [name, curr_ret_str, name, inquiry, name]
return prompt_input
def __chat_func_clean_up(gpt_response, prompt=""):
gpt_response = extract_first_json_dict(gpt_response)
gpt_response = list(gpt_response.values())
return gpt_response
def __chat_func_validate(gpt_response, prompt=""):
if verbose:
print (gpt_response)
try:
fields = ["point 1", "point 2", "point 3"]
gpt_response = extract_first_json_dict(gpt_response)
for field in fields:
if field not in gpt_response:
return False
return True
except:
return False
def get_fail_safe():
return None
prompt_template = f"{prompt_dir}/corr_topics_HyDE_v1.txt"
prompt_input = create_prompt_input(name, inquiry, curr_ret_str)
prompt = generate_prompt(prompt_input, prompt_template)
fail_safe = get_fail_safe()
output = chat_safe_generate(prompt, "ChatGPT", 5, fail_safe,
__chat_func_validate, __chat_func_clean_up, verbose)
if DEBUG or verbose:
print_run_prompts(prompt_template, prompt_input, prompt, output)
return output, [output, prompt, prompt_input, fail_safe]
def run_gpt_generate_inquiry_response_rpg_new(name, retrieved_obs_str, retrieved_ref_str, inquiry, test_input=None, verbose=False):
def create_prompt_input(name, retrieved_obs_str, retrieved_ref_str, inquiry, test_input=None):
prompt_input = [name, name, name, name,
retrieved_obs_str,
name, name,
retrieved_ref_str,
name, name, name,
inquiry,
name, name, name, name, name, name, name]
return prompt_input
def __chat_func_clean_up(gpt_response, prompt=""):
gpt_response = extract_first_json_dict(gpt_response)
# print ("Thought:", gpt_response["task 0"])
return gpt_response["task 1"]
def __chat_func_validate(gpt_response, prompt=""):
if verbose:
print (gpt_response)
try:
fields = ["task 0", "task 1"]
gpt_response = extract_first_json_dict(gpt_response)
for field in fields:
if field not in gpt_response:
return False
return True
except:
return False
def get_fail_safe():
return None
prompt_template = f"{prompt_dir}/inquiry_response_rpg_v2.txt"
prompt_input = create_prompt_input(name, retrieved_obs_str, retrieved_ref_str, inquiry)
prompt = generate_prompt(prompt_input, prompt_template)
# print ("==== DEBUG")
# print (prompt)
fail_safe = get_fail_safe()
output = chat_safe_generate(prompt, "ChatGPT", 5, fail_safe,
__chat_func_validate, __chat_func_clean_up, verbose)
if DEBUG or verbose:
print_run_prompts(prompt_template, prompt_input, prompt, output)
return output, [output, prompt, prompt_input, fail_safe]
def run_gpt_generate_inquiry_classfier_response(name, retrieved_obs_str, retrieved_ref_str, inquiry, classes, test_input=None, verbose=False):
def create_prompt_input(name, retrieved_obs_str, retrieved_ref_str, inquiry, classes, test_input=None):
prompt_input = [inquiry, str(classes), name, name,
retrieved_obs_str, retrieved_ref_str, name, inquiry, str(classes), name]
return prompt_input
def __chat_func_clean_up(gpt_response, prompt=""):
print ("DEBUGGGG", gpt_response)
gpt_response = extract_first_json_dict(gpt_response)
return gpt_response["response"]
def __chat_func_validate(gpt_response, prompt=""):
if verbose:
print (gpt_response)
try:
fields = ["response"]
gpt_response = extract_first_json_dict(gpt_response)
for field in fields:
if field not in gpt_response:
return False
return True
except:
return False
def get_fail_safe():
return None
prompt_template = f"{prompt_dir}/inquiry_classification_response_v1.txt"
prompt_input = create_prompt_input(name, retrieved_obs_str, retrieved_ref_str, inquiry, classes)
prompt = generate_prompt(prompt_input, prompt_template)
# print ("==== DEBUG")
# print (prompt)
fail_safe = get_fail_safe()
output = chat_safe_generate(prompt, "ChatGPT", 5, fail_safe,
__chat_func_validate, __chat_func_clean_up, verbose)
if DEBUG or verbose:
print_run_prompts(prompt_template, prompt_input, prompt, output)
return output, [output, prompt, prompt_input, fail_safe]
def run_gpt_generate_inquiry_classfier_response_v2(name, retrieved_obs_str, retrieved_ref_str, inquiry, classes, test_input=None, verbose=False):
def create_prompt_input(name, retrieved_obs_str, retrieved_ref_str, inquiry, classes, test_input=None):
prompt_input = [name, name, retrieved_obs_str, retrieved_ref_str,
inquiry, str(classes), name, str(classes), name]
return prompt_input
def __chat_func_clean_up(gpt_response, prompt=""):
print ("DEBUGGGG", gpt_response)
gpt_response = extract_first_json_dict(gpt_response)
return gpt_response["response"]
def __chat_func_validate(gpt_response, prompt=""):
if verbose:
print (gpt_response)
try:
fields = ["response"]
gpt_response = extract_first_json_dict(gpt_response)
for field in fields:
if field not in gpt_response:
return False
return True
except:
return False
def get_fail_safe():
return None
prompt_template = f"{prompt_dir}/inquiry_classification_response_v2.txt"
prompt_input = create_prompt_input(name, retrieved_obs_str, retrieved_ref_str, inquiry, classes)
prompt = generate_prompt(prompt_input, prompt_template)
# print ("==== DEBUG")
# print (prompt)
fail_safe = get_fail_safe()
output = chat_safe_generate(prompt, "ChatGPT", 5, fail_safe,
__chat_func_validate, __chat_func_clean_up, verbose)
if DEBUG or verbose:
print_run_prompts(prompt_template, prompt_input, prompt, output)
return output, [output, prompt, prompt_input, fail_safe]
def run_gpt_generate_simple_inquire(name, profile, inquiry, test_input=None, verbose=False):
def create_prompt_input(name, profile, inquiry, test_input=None):
prompt_input = [name, name,
profile,
name, name, name,
inquiry,
name, name, name, name, name, name, name]
return prompt_input
def __chat_func_clean_up(gpt_response, prompt=""):
gpt_response = extract_first_json_dict(gpt_response)
# print ("Thought:", gpt_response["task 0"])
return gpt_response["task 1"]
def __chat_func_validate(gpt_response, prompt=""):
if verbose:
print (gpt_response)
try:
fields = ["task 0", "task 1"]
gpt_response = extract_first_json_dict(gpt_response)
for field in fields:
if field not in gpt_response:
return False
return True
except:
return False
def get_fail_safe():
return None
prompt_template = f"{prompt_dir}/inquiry_simple_v1.txt"
prompt_input = create_prompt_input(name, profile, inquiry)
prompt = generate_prompt(prompt_input, prompt_template)
# print ("==== DEBUG")
# print (prompt)
fail_safe = get_fail_safe()
output = chat_safe_generate(prompt, "ChatGPT", 5, fail_safe,
__chat_func_validate, __chat_func_clean_up, verbose)
if DEBUG or verbose:
print_run_prompts(prompt_template, prompt_input, prompt, output)
return output, [output, prompt, prompt_input, fail_safe]
def run_gpt_generate_qual_discrete_translation(question, answer, options, test_input=None, verbose=False):
def create_prompt_input(question, answer, options, test_input=None):
prompt_input = [question, answer, options]
return prompt_input
def __chat_func_clean_up(gpt_response, prompt=""):
gpt_response = extract_first_json_dict(gpt_response)["output"]
return gpt_response
def __chat_func_validate(gpt_response, prompt=""):
if verbose:
print (gpt_response)
try:
fields = ["output"]
gpt_response = extract_first_json_dict(gpt_response)
for field in fields:
if field not in gpt_response:
return False
return True
except:
return False
def get_fail_safe():
return None
prompt_template = f"{prompt_dir}/qual_discrete_translation_v1.txt"
prompt_input = create_prompt_input(question, answer, options)
prompt = generate_prompt(prompt_input, prompt_template)
# print (prompt)
fail_safe = get_fail_safe()
output = chat_safe_generate(prompt, "ChatGPT", 5, fail_safe,
__chat_func_validate, __chat_func_clean_up, verbose)
if DEBUG or verbose:
print_run_prompts(prompt_template, prompt_input, prompt, output)
return output, [output, prompt, prompt_input, fail_safe]
def run_gpt_generate_oneshot_ref(name, curr_str, ref_anchor, ref_count, test_input=None, verbose=False):
def create_prompt_input(name, curr_str, ref_anchor, ref_count, test_input=None):
prompt_input = [name, curr_str, ref_count, name, ref_anchor, name, name, name]
return prompt_input
def __chat_func_clean_up(gpt_response, prompt=""):
return extract_first_json_dict(gpt_response)["inferences"]
def __chat_func_validate(gpt_response, prompt=""):
try:
print ("hmm....")
print (extract_first_json_dict(gpt_response)["inferences"])
return True
except:
return False
def get_fail_safe():
return None
prompt_template = f"{prompt_dir}/oneshot_reflection_v1.txt"
prompt_input = create_prompt_input(name, curr_str, ref_anchor, ref_count)
prompt = generate_prompt(prompt_input, prompt_template)
# print (prompt)
fail_safe = get_fail_safe()
output = chat_safe_generate(prompt, "ChatGPT", 5, fail_safe,
__chat_func_validate, __chat_func_clean_up, verbose)
print ("???")
print (output)
if DEBUG or verbose:
print_run_prompts(prompt_template, prompt_input, prompt, output)
return output, [output, prompt, prompt_input, fail_safe]
def run_gpt_generate_rerank_ret(name, mem_stream_list, inquiry, test_input=None, verbose=False):
def create_prompt_input(name, mem_stream_list, inquiry, test_input=None):
prompt_input = [name, str(mem_stream_list), name, name, inquiry]
return prompt_input
def __chat_func_clean_up(gpt_response, prompt=""):
return extract_first_json_dict(gpt_response)
def __chat_func_validate(gpt_response, prompt=""):
__chat_func_clean_up(gpt_response)
try:
return True
except:
return False
def get_fail_safe():
return None
prompt_template = f"{prompt_dir}/rerank_retrieved_v1.txt"
prompt_input = create_prompt_input(name, mem_stream_list, inquiry)
prompt = generate_prompt(prompt_input, prompt_template)
# print (prompt)
fail_safe = get_fail_safe()
output = chat_safe_generate(prompt, "ChatGPT", 5, fail_safe,
__chat_func_validate, __chat_func_clean_up, verbose)
print ("???")
print (output)
if DEBUG or verbose:
print_run_prompts(prompt_template, prompt_input, prompt, output)
return output, [output, prompt, prompt_input, fail_safe]