-
Notifications
You must be signed in to change notification settings - Fork 0
/
llm_helper.py
992 lines (914 loc) · 64.4 KB
/
llm_helper.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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
import re
import openai
from openai import OpenAI
import os
if not os.getenv("OPENAI_API_KEY"):
os.environ["OPENAI_API_KEY"] = "sk-xxxxxx"
# this functions takes valid subjects, topics, and causes and formats them into a string
def format_actions(actions):
actions = list(actions)
assert len(actions) == 2 or len(actions) == 3
for i in range(len(actions)):
actions[i] = ['"' + a + '"' for a in actions[i]]
actions = ["\n".join(a) for a in actions]
if len(actions) == 3:
actions = f"""{actions[0]}
Valid Subjects: {actions[1]}
Valid Topics: {actions[2]}"""
elif len(actions) == 2:
actions = f"""{actions[0]}
Valid Causes: {actions[1]}"""
else:
raise NotImplementedError
return actions
class PromptFormat:
def format_prompt(self, input):
raise NotImplementedError
def parse_response(self, response):
raise NotImplementedError
######################################################################without summarizer
class GPTChooses_or_Recs(PromptFormat):
def __init__(self, topk=5):
self.topk = topk
def format_prompt(self, history, subject, problem, valid_subjects, valid_topics, valid_causes, choices, posttest=False,prev_exp=None,summarizer=None):
formated_history = ""
for (i, s) in enumerate(history):
prefix = "Student: " if i % 2 == 1 else "Customer: "
formated_history += (prefix + s + "\n")
formated_prev_exp = ""
if prev_exp is not None:
for (j,exp) in enumerate(prev_exp):
formated_prev_exp += f"Experience {j+1}:\n"
for (i, s) in enumerate(exp):
prefix = "Student: " if i % 2 == 1 else "Customer: "
formated_prev_exp += (prefix + s + "\n")
else:
formated_prev_exp = "No previous experience\n"
formated_choices = ""
for (i, c) in enumerate(choices[:min(self.topk,len(choices))]):
formated_choices += (f"{i + 1}. " + c + "\n")
formated_valid_topics = "[\n"
formated_valid_subjects = "[\n"
formated_valid_causes = "[\n"
for s in valid_subjects:
formated_valid_subjects += s + ",\n"
for t in valid_topics:
formated_valid_topics += t + ",\n"
for t in valid_causes:
formated_valid_causes += t + ",\n"
formated_valid_topics += "\n]"
formated_valid_subjects += "\n]"
formated_valid_causes += "\n]"
output = "1. choose(x)\n2. choose(y)" if posttest else "1. ask(x,y)\n2. answer()"
available_actions = "1.choose(cause)[The most probable reason is cause]" if posttest else " 1. ask(subject,topic)[I want to ask about subject’s topic.] 2. answer()[I want to suggest the cause behind the customer’s problem.]\n"
available_options = f"valid causes: {formated_valid_causes}" if posttest else f"valid subjects: {formated_valid_subjects}\nvalid topics: {formated_valid_topics}\n"
question = "What should the student answer?" if posttest else "What should the student choose to do?"
return [
{
"role": "system",
"content": "You are a pharmacist helping a pharmacy student go through an educational game. You will receive a description of the customer and you will help the student ask a series of questions needed to help the customer. Make sure you are keeping the discussion short with the customer and offer a diagnosis once you are sure."
},
{
"role": "user",
"content": f"Task: Find the cause behind the {subject}'s {problem}\nYour previous experience with similar tasks:\n{formated_prev_exp}Interaction History:\n{formated_history}Student top choices:\n{formated_choices} available actions:{available_actions}\n{available_options} {question} if there is a suitable action between the student choice answer with the choice number. you can first reason about your choice in less than 50 words. Don't forget to put ### after your reasoning finishes.Then write your chosen action.Remember that asking the same question again will not give you a different answer. If you do not find any suitable actions between the student's top choices recommend {5 if not posttest else 2} actions from the list to the student. Write whether you are doing choose or recommend followes by $$$.\nexample output1:\nchoose\n$$$\nreason: we need to explore the baby's symptoms more.\n###\n1\nexample output2:\nrecommend\n$$$\nreason: we need to explore the baby's symptoms more but the student have not included it in their choice.\n###\n{output}"
}
]
def parse_response(self, response):
return response.choices[0].message.content
class GPTChooses(PromptFormat):
def __init__(self, topk=5):
self.topk = topk
def format_prompt(self, history, subject, problem, choices, posttest=False,prev_exp=None,summarizer=None):
formated_history = ""
for (i, s) in enumerate(history):
prefix = "Student: " if i % 2 == 1 else "Customer: "
formated_history += (prefix + s + "\n")
formated_prev_exp = ""
if prev_exp is not None:
for (j,exp) in enumerate(prev_exp):
formated_prev_exp += f"Experience {j+1}:\n"
for (i, s) in enumerate(exp):
prefix = "Student: " if i % 2 == 1 else "Customer: "
formated_prev_exp += (prefix + s + "\n")
else:
formated_prev_exp = "No previous experience\n"
formated_choices = ""
for (i, c) in enumerate(choices[:min(self.topk,len(choices))]):
formated_choices += (f"{i + 1}. " + c + "\n")
question = "What should the student answer?" if posttest else "What should the student choose to do?"
return [
{
"role": "system",
"content": "You are a pharmacist helping a pharmacy student go through an educational game. You will receive a description of the customer and you will help the student ask a series of questions needed to help the customer. Make sure you are keeping the discussion short with the customer and offer a diagnosis once you are sure."
},
{
"role": "user",
"content": f"Task: Find the cause behind the {subject}'s {problem}\nYour previous experience with similar tasks:\n{formated_prev_exp}Interaction History:\n{formated_history}Student top choices:\n{formated_choices} {question} answer with the choice number you can first reason about your choice in less than 50 words. Don't forget to put ### after your reasoning finishes.Then write your chosen action.Remember that asking the same question again will not give you a different answer.\nexample output:\nreason: we need to explore the baby's symptoms more.\n###\n1"
}
]
def parse_response(self, response):
return response.choices[0].message.content
class GPTRecs(PromptFormat):
def __init__(self, num_of_recs=5):
self.num_of_recs = num_of_recs
def format_prompt(self, history, subject, problem, valid_subjects, valid_topics, valid_causes, posttest=False,prev_exp=None,summarizer=None):
formated_history = ""
for (i, s) in enumerate(history):
prefix = "Student: " if i % 2 == 1 else "Customer: "
formated_history += (prefix + s + "\n")
formated_prev_exp = ""
if prev_exp is not None:
for (j,exp) in enumerate(prev_exp):
formated_prev_exp += f"Experience {j+1}:\n"
for (i, s) in enumerate(exp):
prefix = "Student: " if i % 2 == 1 else "Customer: "
formated_prev_exp += (prefix + s + "\n")
else:
formated_prev_exp = "No previous experience\n"
formated_valid_topics = "[\n"
formated_valid_subjects ="[\n"
formated_valid_causes ="[\n"
for s in valid_subjects:
formated_valid_subjects += s+",\n"
for t in valid_topics:
formated_valid_topics += t + ",\n"
for t in valid_causes:
formated_valid_causes += t + ",\n"
formated_valid_topics += "\n]"
formated_valid_subjects += "\n]"
formated_valid_causes += "\n]"
available_actions ="1.choose(cause)[The most probable reason is cause]" if posttest else " 1. ask(subject,topic)[I want to ask about subject’s topic.] 2. answer()[I want to suggest the cause behind the customer’s problem.]\n"
available_options = f"valid causes: {formated_valid_causes}" if posttest else f"valid subjects: {formated_valid_subjects}\nvalid topics: {formated_valid_topics}\n"
output = "1. choose(x)\n2. choose(y)" if posttest else "1. ask(x,y)\n2. answer()"
return \
[
{
"role": "system",
"content": "You are a pharmacist helping a pharmacy student go through an educational game. You will receive a description of the customer and you will help the student ask a series of questions needed to help the customer. Make sure you are keeping the discussion short with the customer and offer a diagnosis once you are sure. At each step, you will receive the list of available actions for the student and you will recommend 5 actions for the student to do."
},
{
"role": "user",
"content": f"Task: Find the cause behind the {subject}'s {problem}\nYour previous experience with similar tasks:\n{formated_prev_exp}Interaction History:\n{formated_history}available actions:{available_actions}\n{available_options}What are your top {2 if posttest else self.num_of_recs} recommended actions from the action list? you can first reason about your recommendation in less than 50 words. Don't forget to put ### after your reasoning finishes. Then write your suggested actions. \noutput format:\nreason: r\n\n###\n{output}"
}
]
def parse_response(self, response):
return response.choices[0].message.content
class GPTPlays(PromptFormat):
def __init__(self):
super().__init__()
def format_prompt(self, history, subject, problem, valid_subjects, valid_topics, valid_causes, posttest=False,prev_exp=None,summarizer=None):
formated_history = ""
for (i, s) in enumerate(history):
prefix = "Student: " if i % 2 == 1 else "Customer: "
formated_history += (prefix + s + "\n")
formated_prev_exp = ""
if prev_exp is not None:
for (j,exp) in enumerate(prev_exp):
formated_prev_exp += f"Experience {j+1}:\n"
for (i, s) in enumerate(exp):
prefix = "Student: " if i % 2 == 1 else "Customer: "
formated_prev_exp += (prefix + s + "\n")
else:
formated_prev_exp = "No previous experience\n"
formated_valid_topics = "[\n"
formated_valid_subjects = "[\n"
formated_valid_causes = "[\n"
for s in valid_subjects:
formated_valid_subjects += s + ",\n"
for t in valid_topics:
formated_valid_topics += t + ",\n"
for t in valid_causes:
formated_valid_causes += t + ",\n"
formated_valid_topics += "\n]"
formated_valid_subjects += "\n]"
formated_valid_causes += "\n]"
available_actions = "1.choose(cause)[The most probable reason is cause]" if posttest else " 1. ask(subject,topic)[I want to ask about subject’s topic.] 2. answer()[I want to suggest the cause behind the customer’s problem.]\n"
available_options = f"valid causes: {formated_valid_causes}" if posttest else f"valid subjects: {formated_valid_subjects}\nvalid topics: {formated_valid_topics}\n"
output = "choose(x)" if posttest else "ask(x,y)"
return \
[
{
"role": "system",
"content": "You are a pharmacist helping a pharmacy student go through an educational game. You will receive a description of the customer and you will help the student ask a series of questions needed to help the customer. Make sure you are keeping the discussion short with the customer and offer a diagnosis once you are sure. At each step, you will receive the list of available actions for the student and you will choose the best action for the student to do."
},
{
"role": "user",
"content": f"Task: Find the cause behind the {subject}'s {problem}\nYour previous experience with similar tasks:\n{formated_prev_exp}Interaction History:\n{formated_history}available actions:{available_actions}\n{available_options}What action would you take next? you can first reason about your choice in less than 50 words. Don't forget to put ### after your reasoning finishes. Then write your chosen action. Remember that asking the same question again will not give you a different answer. \noutput format:\nreason: r\n\n###\n{output}"
}
]
def parse_response(self, response):
return response.choices[0].message.content
class CLINChooses(PromptFormat):
def __init__(self, topk=5):
self.topk = topk
def format_prompt(self, history, subject, problem, choices,summary, posttest=False,prev_exp=None,summarizer=None):
formated_history = ""
for (i, s) in enumerate(history):
prefix = "Student: " if i % 2 == 1 else "Customer: "
formated_history += (prefix + s + "\n")
formated_prev_exp = ""
if prev_exp is not None:
for (j,exp) in enumerate(prev_exp):
formated_prev_exp += f"Experience {j+1}:\n"
for (i, s) in enumerate(exp):
prefix = "Student: " if i % 2 == 1 else "Customer: "
formated_prev_exp += (prefix + s + "\n")
formated_choices = ""
for (i, c) in enumerate(choices[:min(self.topk,len(choices))]):
formated_choices += (f"{i + 1}. " + c + "\n")
question = "What should the student answer?" if posttest else "What should the student choose to do?"
summary_prompt = f"Here is a summary of learnings based on your previous attempts on this task." \
f"These learnings capture important pre-conditions: X MAY BE NECESSARY to Y, X SHOULD BE NECESSARY to Y, and mistakes: X MAY NOT CONTRIBUTE to Y, X DOES NOT CONTRIBUTE to Y. These can be useful for completing your task:\n"
user_em = """First, scan the (unordered) list of learnings, if provided. Decide if any of the learnings are applicable given the last observation to make progress in this task. Then only use selected learnings, if any, to construct a rationale for picking the next action. If no Learning is selected, construct the rationale based on the observation history. Format your response as follows:
Write the selected learning_ids as a comma separated list; the list can be empty if no learnings selected. Then, write $$$ followed by the rationale of at most 50 words. Finally, write ### followed by the single next action you would like to take.
"""
return [
{
"role": "system",
"content": "You are a pharmacist helping a pharmacy student go through an educational game. You will receive a description of the customer and you will help the student ask a series of questions needed to help the customer. Make sure you are keeping the discussion short with the customer and offer a diagnosis once you are sure."
},
{
"role": "user",
"content": f"Task: Find the cause behind the {subject}'s {problem}\nYour previous experience with similar tasks:\n{formated_prev_exp}{summary_prompt}\n{summary}\nObservation History:\n{formated_history}Student top choices:\n{formated_choices} {question} answer with the choice number.{user_em}\nexample output:\n1, 3\n$$$\nreason: r\n\n###\n1"
}
]
def parse_response(self, response):
return response.choices[0].message.content
class CLINRecs(PromptFormat):
def __init__(self, num_of_recs=5):
self.num_of_recs = num_of_recs
def format_prompt(self, history, subject, problem, valid_subjects, valid_topics, valid_causes,summary, posttest=False,prev_exp=None,summarizer=None):
formated_history = ""
for (i, s) in enumerate(history):
prefix = "Student: " if i % 2 == 1 else "Customer: "
formated_history += (prefix + s + "\n")
formated_prev_exp = ""
if prev_exp is not None:
for (j,exp) in enumerate(prev_exp):
formated_prev_exp += f"Experience {j+1}:\n"
for (i, s) in enumerate(exp):
prefix = "Student: " if i % 2 == 1 else "Customer: "
formated_prev_exp += (prefix + s + "\n")
formated_valid_topics = "[\n"
formated_valid_subjects ="[\n"
formated_valid_causes ="[\n"
for s in valid_subjects:
formated_valid_subjects += s+",\n"
for t in valid_topics:
formated_valid_topics += t + ",\n"
for t in valid_causes:
formated_valid_causes += t + ",\n"
formated_valid_topics += "\n]"
formated_valid_subjects += "\n]"
formated_valid_causes += "\n]"
available_actions ="1.choose(cause)[The most probable reason is cause]" if posttest else " 1. ask(subject,topic)[I want to ask about subject’s topic.] 2. answer()[I want to suggest the cause behind the customer’s problem.]\n"
available_options = f"valid causes: {formated_valid_causes}" if posttest else f"valid subjects: {formated_valid_subjects}\nvalid topics: {formated_valid_topics}\n"
summary_prompt = f"Here is a summary of learnings based on your previous attempts on this task." \
f"These learnings capture important pre-conditions: X MAY BE NECESSARY to Y, X SHOULD BE NECESSARY to Y, and mistakes: X MAY NOT CONTRIBUTE to Y, X DOES NOT CONTRIBUTE to Y. These can be useful for completing your task:\n"
user_em = """First, scan the (unordered) list of learnings, if provided. Decide if any of the learnings are applicable given the last observation to make progress in this task. Then only use selected learnings, if any, to construct a rationale for picking the next action. If no Learning is selected, construct the rationale based on the observation history. Format your response as follows:
Write the selected learning_ids as a comma separated list; the list can be empty if no learnings selected. Then, write $$$ followed by the rationale of at most 50 words. Finally, write ### followed by your list of recommended actions.
"""
output = "1. choose(x)\n2. choose(y)" if posttest else "1. ask(x,y)\n2. answer()"
return \
[
{
"role": "system",
"content": "You are a pharmacist helping a pharmacy student go through an educational game. You will receive a description of the customer and you will help the student ask a series of questions needed to help the customer. Make sure you are keeping the discussion short with the customer and offer a diagnosis once you are sure. At each step, you will receive the list of available actions for the student and you will recommend 5 actions for the student to do."
},
{
"role": "user",
"content": f"Task: Find the cause behind the {subject}'s {problem}\nYour previous experience with similar tasks:\n{formated_prev_exp}{summary_prompt}\n{summary}\nObservation History:\n{formated_history}available actions:{available_actions}\n{available_options}What are your top {2 if posttest else self.num_of_recs} recommended actions from the action list? {user_em}\noutput format:\n1, 3\n$$$\nreason: r\n\n###\n{output}"
}
]
def parse_response(self, response):
return response.choices[0].message.content
class CLINPlays(PromptFormat):
def __init__(self):
super().__init__()
def format_prompt(self, history, subject, problem, valid_subjects, valid_topics, valid_causes, summary, posttest=False,prev_exp=None,summarizer=None):
formated_history = ""
for (i, s) in enumerate(history):
prefix = "Student: " if i % 2 == 1 else "Customer: "
formated_history += (prefix + s + "\n")
formated_prev_exp = ""
if prev_exp is not None:
for (j,exp) in enumerate(prev_exp):
formated_prev_exp += f"Experience {j+1}:\n"
for (i, s) in enumerate(exp):
prefix = "Student: " if i % 2 == 1 else "Customer: "
formated_prev_exp += (prefix + s + "\n")
formated_prev_exp = ""
if prev_exp is not None:
for (j,exp) in enumerate(prev_exp):
formated_prev_exp += f"Experience {j+1}:\n"
for (i, s) in enumerate(exp):
prefix = "Student: " if i % 2 == 1 else "Customer: "
formated_prev_exp += (prefix + s + "\n")
formated_valid_topics = "[\n"
formated_valid_subjects = "[\n"
formated_valid_causes = "[\n"
for s in valid_subjects:
formated_valid_subjects += s + ",\n"
for t in valid_topics:
formated_valid_topics += t + ",\n"
for t in valid_causes:
formated_valid_causes += t + ",\n"
formated_valid_topics += "\n]"
formated_valid_subjects += "\n]"
formated_valid_causes += "\n]"
available_actions = "1.choose(cause)[The most probable reason is cause]" if posttest else " 1. ask(subject,topic)[I want to ask about subject’s topic.] 2. answer()[I want to suggest the cause behind the customer’s problem.]\n"
available_options = f"valid causes: {formated_valid_causes}" if posttest else f"valid subjects: {formated_valid_subjects}\nvalid topics: {formated_valid_topics}\n"
summary_prompt = f"Here is a summary of learnings based on your previous attempts on this task." \
f"These learnings capture important pre-conditions: X MAY BE NECESSARY to Y, X SHOULD BE NECESSARY to Y, and mistakes: X MAY NOT CONTRIBUTE to Y, X DOES NOT CONTRIBUTE to Y. These can be useful for predicting your next action:\n"
user_em = """First, scan the (unordered) list of learnings, if provided. Decide if any of the learnings are applicable given the last observation to make progress in this task. Then only use selected learnings, if any, to construct a rationale for picking the next action. If no Learning is selected, construct the rationale based on the observation history. Format your response as follows:
Write the selected learning_ids as a comma separated list; the list can be empty if no learnings selected. Then, write $$$ followed by the rationale of at most 50 words. Finally, write ### followed by the single next action you would like to take.
"""
output = "choose(x)" if posttest else "ask(x,y)"
return \
[
{
"role": "system",
"content": "You are a pharmacist helping a pharmacy student go through an educational game. You will receive a description of the customer and you will help the student ask a series of questions needed to help the customer. Make sure you are keeping the discussion short with the customer and offer a diagnosis once you are sure. At each step, you will receive the list of available actions for the student and you will choose the best action for the student to do."
},
{
"role": "user",
"content": f"Task: Find the cause behind the {subject}'s {problem}\nYour previous experience with similar tasks:\n{formated_prev_exp}{summary_prompt}\n{summary}\nObservation History:\n{formated_history}available actions:{available_actions}\n{available_options}What action would you take next? {user_em}\noutput format:\n1, 3\n$$$\nreason: r\n\n###\n{output}"
}
]
def parse_response(self, response):
return response.choices[0].message.content
######################################################################with summarizer
class GPTPlays2(PromptFormat):
def __init__(self):
super().__init__()
def format_prompt(self, history, subject, problem, valid_subjects, valid_topics, valid_causes,summarizer, posttest=False,prev_exp=None):
formated_history = summarizer.summarize(history)
if prev_exp is not None:
formated_prev_exp = ""
for (j,exp) in enumerate(prev_exp):
formated_prev_exp += f"Experience {j+1}:\n"
formated_prev_exp += summarizer.summarize(exp)
formated_prev_actions = ""
for (i, h) in enumerate(history):
if i % 2 == 1:
formated_prev_actions += (h.split("the ")[-1].split('.')[0] + ",")
formated_valid_topics = "[\n"
formated_valid_subjects = "[\n"
formated_valid_causes = "[\n"
for s in valid_subjects:
formated_valid_subjects += s + ",\n"
for t in valid_topics:
formated_valid_topics += t + ",\n"
for t in valid_causes:
formated_valid_causes += t + ",\n"
formated_valid_topics += "\n]"
formated_valid_subjects += "\n]"
formated_valid_causes += "\n]"
available_actions = "1.choose(cause)[The most probable reason is cause]" if posttest else " 1. ask(subject,topic)[I want to ask about subject’s topic.] 2. answer()[I want to suggest the cause behind the customer’s problem.]\n"
available_options = f"valid causes: {formated_valid_causes}" if posttest else f"valid subjects: {formated_valid_subjects}\nvalid topics: {formated_valid_topics}\n"
output = "choose(x)" if posttest else "ask(x,y)"
return \
[
{
"role": "system",
"content": "You are a pharmacist helping a pharmacy student go through an educational game. You will receive a description of the customer and you will help the student ask a series of questions needed to help the customer. Make sure you are keeping the discussion short with the customer and offer a diagnosis once you are sure. At each step, you will receive the list of available actions for the student and you will choose the best action for the student to do."
},
{
"role": "user",
"content": f"Task: Find the cause behind the {subject}'s {problem}\nPrevious actions(Remember repeated actions does not give you additional information):{formated_prev_actions}\nSummary of Interaction:\n{formated_history}\navailable actions:{available_actions}\n{available_options}What action would you take next? Only write your chosen action.\nexample output:{output}"
}
]
def parse_response(self, response):
return response.choices[0].message.content
class GPTRecs2(PromptFormat):
def __init__(self, num_of_recs=5):
self.num_of_recs = num_of_recs
def format_prompt(self, history, subject, problem, valid_subjects, valid_topics, valid_causes, posttest=False,prev_exp=None,summarizer=None):
formated_history = summarizer.summarize(history)
if prev_exp is not None:
formated_prev_exp = ""
for (j, exp) in enumerate(prev_exp):
formated_prev_exp += f"Experience {j + 1}:\n"
formated_prev_exp += summarizer.summarize(exp)
formated_prev_actions = ""
for (i, h) in enumerate(history):
if i % 2 == 1:
formated_prev_actions += (h.split("the ")[-1].split('.')[0] + ",")
formated_valid_topics = "[\n"
formated_valid_subjects ="[\n"
formated_valid_causes ="[\n"
for s in valid_subjects:
formated_valid_subjects += s+",\n"
for t in valid_topics:
formated_valid_topics += t + ",\n"
for t in valid_causes:
formated_valid_causes += t + ",\n"
formated_valid_topics += "\n]"
formated_valid_subjects += "\n]"
formated_valid_causes += "\n]"
available_actions ="1.choose(cause)[The most probable reason is cause]" if posttest else " 1. ask(subject,topic)[I want to ask about subject’s topic.] 2. answer()[I want to suggest the cause behind the customer’s problem.]\n"
available_options = f"valid causes: {formated_valid_causes}" if posttest else f"valid subjects: {formated_valid_subjects}\nvalid topics: {formated_valid_topics}\n"
output = "1. choose(x)\n2. choose(y)" if posttest else "1. ask(x,y)\n2. answer()"
return \
[
{
"role": "system",
"content": "You are a pharmacist helping a pharmacy student go through an educational game. You will receive a description of the customer and you will help the student ask a series of questions needed to help the customer. Make sure you are keeping the discussion short with the customer and offer a diagnosis once you are sure. At each step, you will receive the list of available actions for the student and you will recommend 5 actions for the student to do."
},
{
"role": "user",
"content": f"Task: Find the cause behind the {subject}'s {problem}\nPrevious actions(Remember repeated actions does not give you additional information):{formated_prev_actions}\nSummary of Interaction:\n{formated_history}\navailable actions:{available_actions}\n{available_options}What are your top {2 if posttest else self.num_of_recs} recommended actions from the action list? Only write your recommended actions.\noutput format:{output}"
}
]
def parse_response(self, response):
return response.choices[0].message.content
class GPTChooses2(PromptFormat):
def __init__(self, topk=5):
self.topk = topk
def format_prompt(self, history, subject, problem, choices, posttest=False,prev_exp=None,summarizer=None):
formated_history = summarizer.summarize(history)
if prev_exp is not None:
formated_prev_exp = ""
for (j, exp) in enumerate(prev_exp):
formated_prev_exp += f"Experience {j + 1}:\n"
formated_prev_exp += summarizer.summarize(exp)
formated_prev_actions = ""
for (i, h) in enumerate(history):
if i % 2 == 1:
formated_prev_actions += (h.split("the ")[-1].split('.')[0] + ",")
formated_choices = ""
for (i, c) in enumerate(choices[:min(self.topk,len(choices))]):
formated_choices += (f"{i + 1}. " + c + "\n")
question = "What should the student answer?" if posttest else "What should the student choose to do?"
return [
{
"role": "system",
"content": "You are a pharmacist helping a pharmacy student go through an educational game. You will receive a description of the customer and you will help the student ask a series of questions needed to help the customer. Make sure you are keeping the discussion short with the customer and offer a diagnosis once you are sure."
},
{
"role": "user",
"content": f"Task: Find the cause behind the {subject}'s {problem}\nPrevious actions(Remember repeated actions does not give you additional information):{formated_prev_actions}\nSummary of Interaction:\n{formated_history}\nStudent top choices:\n{formated_choices} {question} answer with the choice number.Only write your chosen action.\nexample output:1"
}
]
def parse_response(self, response):
return response.choices[0].message.content
class CLINPlays2(PromptFormat):
def __init__(self):
super().__init__()
def format_prompt(self, history, subject, problem, valid_subjects, valid_topics, valid_causes, summary, posttest=False,prev_exp=None,summarizer=None):
formated_history = summarizer.summarize(history)
if prev_exp is not None:
formated_prev_exp = ""
for (j, exp) in enumerate(prev_exp):
formated_prev_exp += f"Experience {j + 1}:\n"
formated_prev_exp += summarizer.summarize(exp)
formated_prev_actions = ""
for (i, h) in enumerate(history):
if i % 2 == 1:
formated_prev_actions += (h.split("the ")[-1].split('.')[0] + ",")
formated_valid_topics = "[\n"
formated_valid_subjects = "[\n"
formated_valid_causes = "[\n"
for s in valid_subjects:
formated_valid_subjects += s + ",\n"
for t in valid_topics:
formated_valid_topics += t + ",\n"
for t in valid_causes:
formated_valid_causes += t + ",\n"
formated_valid_topics += "\n]"
formated_valid_subjects += "\n]"
formated_valid_causes += "\n]"
available_actions = "1.choose(cause)[The most probable reason is cause]" if posttest else " 1. ask(subject,topic)[I want to ask about subject’s topic.] 2. answer()[I want to suggest the cause behind the customer’s problem.]\n"
available_options = f"valid causes: {formated_valid_causes}" if posttest else f"valid subjects: {formated_valid_subjects}\nvalid topics: {formated_valid_topics}\n"
summary_prompt = f"Here is a summary of learnings based on your previous attempts on this task." \
f"These learnings capture important pre-conditions: X MAY BE NECESSARY to Y, X SHOULD BE NECESSARY to Y, and mistakes: X MAY NOT CONTRIBUTE to Y, X DOES NOT CONTRIBUTE to Y. These can be useful for predicting your next action:\n"
user_em = """First, scan the (unordered) list of learnings, if provided. Decide if any of the learnings are applicable given the last observation to make progress in this task. Then only use selected learnings, if any, to construct a rationale for picking the next action. If no Learning is selected, construct the rationale based on the observation history. Format your response as follows:
Write the selected learning_ids as a comma separated list; the list can be empty if no learnings selected. Then, write $$$ followed by the single next action you would like to take.
"""
output = "choose(x)" if posttest else "ask(x,y)"
return \
[
{
"role": "system",
"content": "You are a pharmacist helping a pharmacy student go through an educational game. You will receive a description of the customer and you will help the student ask a series of questions needed to help the customer. Make sure you are keeping the discussion short with the customer and offer a diagnosis once you are sure. At each step, you will receive the list of available actions for the student and you will choose the best action for the student to do."
},
{
"role": "user",
"content": f"Task: Find the cause behind the {subject}'s {problem}\n{summary_prompt}\n{summary}\nPrevious actions(Remember repeated actions does not give you additional information):{formated_prev_actions}\nSummary of Interaction:\n{formated_history}\navailable actions:{available_actions}\n{available_options}What action would you take next? {user_em}\noutput format:\n1, 3\n$$$\n{output}"
}
]
def parse_response(self, response):
return response.choices[0].message.content
class CLINRecs2(PromptFormat):
def __init__(self, num_of_recs=5):
self.num_of_recs = num_of_recs
def format_prompt(self, history, subject, problem, valid_subjects, valid_topics, valid_causes, summary,
posttest=False, prev_exp=None, summarizer=None):
formated_history = summarizer.summarize(history)
if prev_exp is not None:
formated_prev_exp = ""
for (j, exp) in enumerate(prev_exp):
formated_prev_exp += f"Experience {j + 1}:\n"
formated_prev_exp += summarizer.summarize(exp)
formated_prev_actions = ""
for (i, h) in enumerate(history):
if i % 2 == 1:
formated_prev_actions += (h.split("the ")[-1].split('.')[0] + ",")
formated_valid_topics = "[\n"
formated_valid_subjects = "[\n"
formated_valid_causes = "[\n"
for s in valid_subjects:
formated_valid_subjects += s + ",\n"
for t in valid_topics:
formated_valid_topics += t + ",\n"
for t in valid_causes:
formated_valid_causes += t + ",\n"
formated_valid_topics += "\n]"
formated_valid_subjects += "\n]"
formated_valid_causes += "\n]"
available_actions = "1.choose(cause)[The most probable reason is cause]" if posttest else " 1. ask(subject,topic)[I want to ask about subject’s topic.] 2. answer()[I want to suggest the cause behind the customer’s problem.]\n"
available_options = f"valid causes: {formated_valid_causes}" if posttest else f"valid subjects: {formated_valid_subjects}\nvalid topics: {formated_valid_topics}\n"
summary_prompt = f"Here is a summary of learnings based on your previous attempts on this task." \
f"These learnings capture important pre-conditions: X MAY BE NECESSARY to Y, X SHOULD BE NECESSARY to Y, and mistakes: X MAY NOT CONTRIBUTE to Y, X DOES NOT CONTRIBUTE to Y. These can be useful for completing your task:\n"
user_em = """First, scan the (unordered) list of learnings, if provided. Decide if any of the learnings are applicable given the last observation to make progress in this task. Then only use selected learnings, if any, to construct a rationale for picking the next action. If no Learning is selected, construct the rationale based on the observation history. Format your response as follows:
Write the selected learning_ids as a comma separated list; the list can be empty if no learnings selected. Then, write $$$ followed by your list of recommended actions.
"""
output = "1. choose(x)\n2. choose(y)" if posttest else "1. ask(x,y)\n2. answer()"
return \
[
{
"role": "system",
"content": "You are a pharmacist helping a pharmacy student go through an educational game. You will receive a description of the customer and you will help the student ask a series of questions needed to help the customer. Make sure you are keeping the discussion short with the customer and offer a diagnosis once you are sure. At each step, you will receive the list of available actions for the student and you will recommend 5 actions for the student to do."
},
{
"role": "user",
"content": f"Task: Find the cause behind the {subject}'s {problem}\n{summary_prompt}\n{summary}\nPrevious actions(Remember repeated actions does not give you additional information):{formated_prev_actions}\nSummary of Interaction:\n{formated_history}\navailable actions:{available_actions}\n{available_options}What are your top {2 if posttest else self.num_of_recs} recommended actions from the action list? {user_em}\noutput format:\n1, 3\n$$$\n{output}"
}
]
def parse_response(self, response):
return response.choices[0].message.content
class CLINChooses2(PromptFormat):
def __init__(self, topk=5):
self.topk = topk
def format_prompt(self, history, subject, problem, choices,summary, posttest=False,prev_exp=None,summarizer=None):
formated_history = summarizer.summarize(history)
if prev_exp is not None:
formated_prev_exp = ""
for (j, exp) in enumerate(prev_exp):
formated_prev_exp += f"Experience {j + 1}:\n"
formated_prev_exp += summarizer.summarize(exp)
formated_prev_actions = ""
for (i, h) in enumerate(history):
if i % 2 == 1:
formated_prev_actions += (h.split("the ")[-1].split('.')[0] + ",")
formated_choices = ""
for (i, c) in enumerate(choices[:min(self.topk,len(choices))]):
formated_choices += (f"{i + 1}. " + c + "\n")
question = "What should the student answer?" if posttest else "What should the student choose to do?"
summary_prompt = f"Here is a summary of learnings based on your previous attempts on this task." \
f"These learnings capture important pre-conditions: X MAY BE NECESSARY to Y, X SHOULD BE NECESSARY to Y, and mistakes: X MAY NOT CONTRIBUTE to Y, X DOES NOT CONTRIBUTE to Y. These can be useful for completing your task:\n"
user_em = """First, scan the (unordered) list of learnings, if provided. Decide if any of the learnings are applicable given the last observation to make progress in this task. Then only use selected learnings, if any, to construct a rationale for picking the next action. If no Learning is selected, construct the rationale based on the observation history. Format your response as follows:
Write the selected learning_ids as a comma separated list; the list can be empty if no learnings selected. Then, write $$$ followed by the single next action you would like to take.
"""
return [
{
"role": "system",
"content": "You are a pharmacist helping a pharmacy student go through an educational game. You will receive a description of the customer and you will help the student ask a series of questions needed to help the customer. Make sure you are keeping the discussion short with the customer and offer a diagnosis once you are sure."
},
{
"role": "user",
"content": f"Task: Find the cause behind the {subject}'s {problem}\n{summary_prompt}\n{summary}\n\nPrevious actions(Remember repeated actions does not give you additional information):{formated_prev_actions}\nSummary of Interaction:\n{formated_history}\nStudent top choices:\n{formated_choices} {question} answer with the choice number.{user_em}\nexample output:\n1, 3\n$$$\n1"
}
]
def parse_response(self, response):
return response.choices[0].message.content
class Chooser_or_Recommender():
def __init__(self, prompt_format, model="gpt-3.5-turbo", temperature=0,
max_tokens=200) -> None:
self.prompt_format = prompt_format
self.model = model
self.temperature = temperature
self.max_tokens = max_tokens
def cor(self, history, subject, problem, valid_subjects, valid_topics, valid_causes, choices,summary=None, posttest=False,prev_exp=None,summarizer=None):
client = OpenAI()
if summary is None:
prompt = self.prompt_format.format_prompt(history, subject, problem, valid_subjects, valid_topics, valid_causes, choices, posttest=posttest,prev_exp=prev_exp,summarizer=summarizer)
else:
prompt = self.prompt_format.format_prompt(history, subject, problem, valid_subjects, valid_topics, valid_causes, choices, summary, posttest=posttest,prev_exp=prev_exp,summarizer=summarizer)
response = client.chat.completions.create(
model=self.model,
messages=prompt,
temperature=self.temperature,
max_tokens=self.max_tokens,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return self.prompt_format.parse_response(response)
class Chooser():
def __init__(self, prompt_format, model="gpt-3.5-turbo", temperature=0,
max_tokens=200) -> None:
self.prompt_format = prompt_format
self.model = model
self.temperature = temperature
self.max_tokens = max_tokens
def choose(self, history, subject, problem, choices,summary=None, posttest=False,prev_exp=None,summarizer=None):
client = OpenAI()
if summary is None:
prompt = self.prompt_format.format_prompt(history, subject, problem, choices, posttest=posttest,prev_exp=prev_exp,summarizer=summarizer)
else:
prompt = self.prompt_format.format_prompt(history, subject, problem, choices, summary, posttest=posttest,prev_exp=prev_exp,summarizer=summarizer)
response = client.chat.completions.create(
model=self.model,
messages=prompt,
temperature=self.temperature,
max_tokens=self.max_tokens,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return self.prompt_format.parse_response(response)
class Recommender():
def __init__(self, prompt_format, model="gpt-3.5-turbo", temperature=0,
max_tokens=200) -> None:
self.prompt_format = prompt_format
self.model = model
self.temperature = temperature
self.max_tokens = max_tokens
def rec(self, history, subject, problem, valid_subjects, valid_topics, valid_causes, summary=None, posttest=False,prev_exp=None,summarizer=None):
client = OpenAI()
if summary is None:
prompt = self.prompt_format.format_prompt(history, subject, problem, valid_subjects, valid_topics, valid_causes, posttest=posttest,prev_exp=prev_exp,summarizer=summarizer)
else:
prompt = self.prompt_format.format_prompt(history, subject, problem, valid_subjects, valid_topics, valid_causes, summary, posttest=posttest,prev_exp=prev_exp,summarizer=summarizer)
response = client.chat.completions.create(
model=self.model,
messages=prompt,
temperature=self.temperature,
max_tokens=self.max_tokens,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return self.prompt_format.parse_response(response)
class Player():
def __init__(self, prompt_format, model="gpt-3.5-turbo", temperature=0,
max_tokens=200) -> None:
self.prompt_format = prompt_format
self.model = model
self.temperature = temperature
self.max_tokens = max_tokens
def play(self, history, subject, problem, valid_subjects, valid_topics, valid_causes,summary=None, posttest=False,prev_exp=None,summarizer=None):
if summary is None:
prompt = self.prompt_format.format_prompt(history, subject, problem, valid_subjects, valid_topics, valid_causes,summarizer=summarizer, posttest=posttest,prev_exp=prev_exp)
else:
prompt = self.prompt_format.format_prompt(history, subject, problem, valid_subjects, valid_topics, valid_causes, summary,summarizer=summarizer, posttest=posttest,prev_exp=prev_exp)
print(prompt)
client = OpenAI()
response = client.chat.completions.create(
model=self.model,
messages=prompt,
temperature=self.temperature,
max_tokens=self.max_tokens,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
response = self.prompt_format.parse_response(response)
print(response)
return response
class OraclePrompt(PromptFormat):
def __init__(self, game, oneshot=False) -> None:
super().__init__()
self.oneshot = oneshot
self.game = game
self.system = """You are a pharmacy assistant student. You are going to play a game based on scenarios of customers coming in to a simulated pharmacy for help. You will interact with the customer and whenever you are ready you will suggest a solution to the game. The solution is the diagnosis you have for the customer’s problem. Remember suggesting a diagnosis without enough interactions is not a good strategy. I'll tell you a summary of which interactions were done, and how the customer responded, and you suggest me what to do next based on the list of valid actions I give you."""
self.task = "Task: Based on the interactions so far, please suggest me which actions are more helpful to do next from the list below. Suggest 3-5 actions. Follow the template I gave you when choosing the action."
self.separator = "\n###\n"
self.ex_summary = """Summary:
- Increased urinary frequency during the day and at night.
- Customer experiences urgency to urinate but has difficulty starting the stream.
- Asked about urinary frequency specifically.
"""
self.ex_valid_actions = """List of the actions:[
"i want to suggest a solution",
"i want to know about the “subject” 's “topic”."
]
Valid Subjects: ["customer", "parents", "dog", "cutomer's child", "house"]
Valid Topics: ['Urinary Frequency', 'Urinary Sensation', 'Urine Characteristics', 'Sexual Function', 'Leakage', 'Overal Symptoms', 'Symptoms Localization', 'Symptoms Intensity', 'Duration of Symptoms', 'Medication History', 'Allergies', 'Underlying Medical Conditions', 'Current Medications', 'Surgical History', 'Pregnancy and Breastfeeding', 'Age', 'Sleep', 'Diet', 'Exercise', 'Teeth']
"""
self.ex_action = "I want to know about the customer's Sexual Function."
def format_prompt(self, summary, valid_actions):
return [{"role": "system", "content": self.system},
{"role": "user", "content": self.separator + self.ex_summary + self.separator + self.task +
self.separator + self.ex_valid_actions},
{"role": "assistant", "content": self.ex_action},
{"role": "user", "content": self.separator + "Summary:\n" + summary + self.separator +
self.task + self.separator + "List of the actions:\n" + valid_actions},
] if self.oneshot else [{"role": "system", "content": self.system},
{"role": "user",
"content": self.separator + "Summary:\n" + summary + self.separator +
self.task + self.separator + "List of the actions:\n" + valid_actions},
]
def parse_response(self, response):
response = response.choices[0].message.content
print(response)
suggestions = re.findall(
r'\d+\.\s+"([^"]*)"', response)
if len(suggestions) == 0:
suggestions = re.findall(
r'\d+\.\s+(.*)', response)
if len(suggestions) == 0:
suggestions = re.findall(
f'"([^"]*)"', response)
mapped_suggestions = []
if len(suggestions) > 0:
print(suggestions)
for suggestion in suggestions:
action = None
if "i want" in suggestion.lower():
if "diagnosis" in suggestion.lower():
action = "i want to suggest a solution."
elif "know" in suggestion.lower():
subject = None
topic = None
for s in self.game.scenario["subjects"]:
if s.lower() in suggestion.lower():
subject = s
for t in self.game.scenario["topics"]:
if t.lower() in suggestion.lower():
topic = t
if subject is not None and topic is not None:
action = f"i want to know about the {subject} 's {topic}."
elif "i think" in suggestion.lower():
for c in self.game.scenario["causes"]:
if c.lower() in suggestion.lower():
action = f"I think the most probable cause behind the problem is {c}."
break
if action is None:
action = suggestion
mapped_suggestions.append(action)
else:
mapped_suggestions = []
return mapped_suggestions
class AgentPrompt(PromptFormat):
def __init__(self, oneshot=False) -> None:
super().__init__()
self.oneshot = oneshot
self.system = """Pretend you're a pharmacy assistant student, you are going to play a game based on scenarios of customers coming in to a simulated pharmacy for help. You will interact with the customer and whenever you are ready you will suggest a solution to the game. The solution is the diagnosis you have for the customer’s problem. Remember suggesting a diagnosis without enough interactions is not a good strategy. I'll tell you a summary of which interactions were done, and how the customer responded, and you tell me what to do next based on the list of valid actions I give you."""
self.task = "Task: Please tell me which action to choose from the list below. Choose only one action. Follow the template I gave you when choosing the action."
self.separator = "\n###\n"
self.ex_summary = """Summary:
- Increased urinary frequency during the day and at night.
- Customer experiences urgency to urinate but has difficulty starting the stream.
- Asked about urinary frequency specifically.
"""
self.ex_valid_actions = """List of the actions:[
"i want to suggest a solution",
"i want to know about the “subject” 's “topic”."
]
Valid Subjects: ["customer", "parents", "dog", "cutomer's child", "house"]
Valid Topics: ['Urinary Frequency', 'Urinary Sensation', 'Urine Characteristics', 'Sexual Function', 'Leakage', 'Overal Symptoms', 'Symptoms Localization', 'Symptoms Intensity', 'Duration of Symptoms', 'Medication History', 'Allergies', 'Underlying Medical Conditions', 'Current Medications', 'Surgical History', 'Pregnancy and Breastfeeding', 'Age', 'Sleep', 'Diet', 'Exercise', 'Teeth']
"""
self.ex_action = "I want to know about the customer's Sexual Function."
def format_prompt(self, summary, valid_actions):
return [{"role": "system", "content": self.system},
{"role": "user", "content": self.separator + self.ex_summary + self.separator + self.task +
self.separator + self.ex_valid_actions},
{"role": "assistant", "content": self.ex_action},
{"role": "user", "content": self.separator + "Summary:\n" + summary + self.separator +
self.task + self.separator + "List of the actions:\n" + valid_actions},
] if self.oneshot else [{"role": "system", "content": self.system},
{"role": "user",
"content": self.separator + "Summary:\n" + summary + self.separator +
self.task + self.separator + "List of the actions:\n" + valid_actions},
]
def parse_response(self, response):
return response.choices[0].message.content
class AgentPrompt2(PromptFormat):
def __init__(self) -> None:
super().__init__()
self.system = """You are an AI agent helping diagnose the most probable cause behind a customer's problem in a pharmacy simulation with a limited number of questions to be asked from the customer available at each step."""
###################################
self.user_sm1 = """I'd like you to work your way through a simulation of a real scenario in a pharmacy to help a customer with a certain problem. Your goal is to ask enough questions from the customer to be able to diagnose the most probable cause behind their problem. Once you have asked enough questions, you must move to the next step of the simulation by saying "i want to propose the most probable diagnosis."
At each step, tell me which action you want to take, e.g., I want to ask about the baby's diet, I want to ask about the mother's symptoms, etc. and I will tell you the result. Then tell me the next action you want to do, until you complete the task."""
self.user_sm2 = "Below you can see the most recent history of your actions to help you decide your next action."
self.task = "Task: you see a customer saying"
#################################################
self.obs_history = "What action would you like to do next?"
self.action_history = "Selected action:"
#####################################################
self.obs = "Here is what you currently see:\n"
self.action = "Possible actions:\n"
self.amb = """If I say "Ambiguous request", your action might mean multiple things. In that case, respond with the number corresponding to the action you want to take in each list of possibilities."""
self.summary = f"Here is a summary of learnings based on your previous attempts on this task." \
f"These learnings capture important pre-conditions: X MAY BE NECESSARY to Y, X SHOULD BE NECESSARY to Y, and mistakes: X MAY NOT CONTRIBUTE to Y, X DOES NOT CONTRIBUTE to Y. These can be useful for predicting your next action:\n"
self.user_em = """First, scan the (unordered) list of learnings, if provided. Decide if any of the learnings are applicable given the last observation to make progress in this task. Then only use selected learnings, if any, to construct a rationale for picking the next action. If no Learning is selected, construct the rationale based on the last observation. Format your response as follows:
Write 'I used learning id(s):' as a comma separated list; the list can be empty if no learnings selected. Then, write $$$ followed by the rationale. Finally, write ### followed by the single next action you would like to take.
If you think you have asked enough questions, please write "i want to propose the most probable diagnosis." as the next action. Remember that asking the same question again will not give you a different answer."""
def format_prompt(self, obs_history, action_history, current_obs, valid_actions, summary=""):
history = ""
if len(action_history) > 0:
history += "History of your actions:\n"
for (o, a) in zip(obs_history, action_history):
history += "The customer said:\n" + o + "\n" + self.obs_history + "\n\n" + \
self.action_history + a
initial_obs = current_obs if len(
action_history) == 0 else obs_history[0]
return [{"role": "system", "content": self.system},
{"role": "user",
"content": "\n\n".join(
[self.user_sm1, self.task + f"\"{initial_obs}\"", self.user_sm2,
self.summary + summary if len(summary) > 0 else ""])},
{"role": "user",
"content": "\n\n".join(
[history, self.obs + current_obs + self.action + valid_actions, self.amb,
self.obs_history, self.user_em])}, ]
def parse_response(self, response):
return response.choices[0].message.content
class SummarizerPrompt(PromptFormat):
def __init__(self, oneshot=False) -> None:
super().__init__()
self.oneshot = oneshot
self.system = """You are a pharmacy assistant trying to interact with a customer. You need to memorize the keypoints of your discussion with the customer so that you know what you did till now so you can decide what to do next after reading them."""
self.task = "Task: Write concise and understandable notes based on the customer responses given to you. Write your note based on the text given to you. Your notes should only include the information you received. Do not include any speculations or thoughts on the customer's diagnosis in them. Use sentences and phrases in your notes. Include clues about what you already asked. Try to not exceed 100 words in your notes."
self.separator = "\n###\n"
self.ex_text = """Text:
Customer: I have urinary problems. can you help?
I: I want to know about the customer's overall symptoms.
Customer: I've been having trouble starting to pee and my stream is pretty weak. I also find myself needing to go a lot, even at night. Sometimes, I can't hold it in and I end up wetting myself.
I: I want to know about the customer's urinary frequency.
Customer: I've been going to the bathroom a lot more than usual, both during the day and at night. Sometimes, I feel like I need to go urgently, but then I have trouble starting the stream."""
self.ex_summary = """* Customer experiencing difficulty initiating urination and weak urine stream.
* Increased urinary frequency and urgency, both day and night.
* Instances of incontinence (wetting themselves).
* Asked about overall symptoms and urinary frequency specifically."""
def format_prompt(self, pa, customer):
assert len(pa) + 1 == len(customer)
customer = ["Customer: " + c + "\n" for c in customer]
pa = ["Pharmacy Assistant: " + p + "\n" for p in pa]
input = ""
input += customer[0]
customer = customer[1:]
if len(pa) > 0:
for (p, c) in zip(pa, customer):
input += (p + c)
return [{"role": "system", "content": self.system},
{"role": "user", "content": self.task +
self.separator + self.ex_text},
{"role": "assistant", "content": self.ex_summary},
{"role": "user", "content": self.task +
self.separator + "Text:\n" + input},
] if self.oneshot else [{"role": "system", "content": self.system},
{"role": "user", "content": self.task +
self.separator + "Text:\n" + input},
]
def parse_response(self, response):
return response.choices[0].message.content
class SummarizerPrompt2(PromptFormat):
def __init__(self, oneshot=False) -> None:
super().__init__()
self.oneshot = oneshot
self.system = """You are a pharmacy assistant trying to interact with a customer. You need to memorize the keypoints of your discussion with the customer so that you know what you did till now so you can decide what to do next after reading them."""
self.task = "Task: Write concise and understandable notes based on the customer responses given to you. Write your note based on the text given to you. Your notes should only include the information you received. Do not include any speculations or thoughts on the customer's diagnosis in them. Use sentences and phrases in your notes. Include clues about what you already asked. Try to not exceed 100 words in your notes."
self.separator = "\n###\n"
def format_prompt(self, history):
formated_history = ""
for (i, s) in enumerate(history):
prefix = "Assistant: " if i % 2 == 1 else "Customer: "
formated_history += (prefix + s + "\n")
return [{"role": "system", "content": self.system},
{"role": "user", "content": self.task +
self.separator + "Interaction History:\n" + formated_history}]
def parse_response(self, response):
return response.choices[0].message.content
class Summarizer():
def __init__(self, prompt_format, model="gpt-3.5-turbo", temperature=0,
max_tokens=100) -> None:
super().__init__()
self.prompt_format = prompt_format
self.model = model
self.temperature = temperature
self.max_tokens = max_tokens
def summarize(self, history):
prompt = self.prompt_format.format_prompt(history)
client = OpenAI()
response = client.chat.completions.create(
model=self.model,
messages=prompt,
temperature=self.temperature,
max_tokens=self.max_tokens,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return self.prompt_format.parse_response(response)
def main():
pa = ["I want to know about the customer's urinary frequency."]
customer = ["I have urinary problems. can you help?",
"I've been going to the bathroom a lot more than usual, both during the day and at night. Sometimes, I feel like I need to go urgently, but then I have trouble starting the stream."]
prompt_format = SummarizerPrompt()
summarizer = Summarizer(prompt_format)
print(summarizer.summarize(pa, customer))
if __name__ == "__main__":
main()