File size: 51,431 Bytes
a2d6347
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6eb94b72",
   "metadata": {},
   "source": [
    "## Following is a demo on running PromptWizard under different scenarios "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52c7ee0a",
   "metadata": {},
   "source": [
    "#### Set environment variables in [.env](.env) for LLM API calling"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3cffa5ef",
   "metadata": {},
   "source": [
    "#### Import Dependencies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11efa138",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.insert(0, \"../../\")\n",
    "import promptwizard\n",
    "from promptwizard.glue.promptopt.instantiate import GluePromptOpt\n",
    "from promptwizard.glue.promptopt.techniques.common_logic import DatasetSpecificProcessing\n",
    "from promptwizard.glue.common.utils.file import save_jsonlist\n",
    "from typing import Any\n",
    "from tqdm import tqdm\n",
    "from re import compile, findall\n",
    "import os\n",
    "from datasets import load_dataset\n",
    "import yaml\n",
    "from dotenv import load_dotenv\n",
    "load_dotenv(override = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9be22d5d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def update_yaml_file(file_path,config_dict):\n",
    "\n",
    "    with open(file_path, 'r') as file:\n",
    "        data = yaml.safe_load(file)\n",
    "\n",
    "\n",
    "    for field,value in config_dict.items():\n",
    "        data[field] = value\n",
    "\n",
    "    with open(file_path, 'w') as file:\n",
    "        yaml.dump(data, file, default_flow_style=False)\n",
    "\n",
    "    print(\"YAML file updated successfully!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78abb34a",
   "metadata": {},
   "source": [
    "Set the paths"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "14399d47",
   "metadata": {},
   "outputs": [],
   "source": [
    "path_to_config = \"configs\"\n",
    "promptopt_config_path = os.path.join(path_to_config, \"promptopt_config.yaml\")\n",
    "setup_config_path = os.path.join(path_to_config, \"setup_config.yaml\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f274af9",
   "metadata": {},
   "source": [
    "### Now let us consider the three scenarios with respect to availability of training data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5aaed236",
   "metadata": {},
   "source": [
    "#### Scenario 1 : We have no training data , but we also don't want in-context examples in final prompt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c34423d",
   "metadata": {},
   "source": [
    "Set the configurations to generate mutations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec4e7607",
   "metadata": {},
   "outputs": [],
   "source": [
    "file_path = 'configs/promptopt_config.yaml' \n",
    "# Set the following based on the use case\n",
    "config_dict = {\n",
    "                \"task_description\": \"You are a mathematics expert. You will be given a mathematics problem which you need to solve\",\n",
    "                \"base_instruction\": \"Lets think step by step.\",\n",
    "                \"mutation_rounds\": 5\n",
    "               }\n",
    "update_yaml_file(file_path,config_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d984e84e",
   "metadata": {},
   "source": [
    "Create an object for calling prompt optimization and inference functionalities"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7aa4ccb",
   "metadata": {},
   "outputs": [],
   "source": [
    "gp = GluePromptOpt(promptopt_config_path,\n",
    "                   setup_config_path,\n",
    "                   dataset_jsonl=None,\n",
    "                   data_processor=None)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d587065",
   "metadata": {},
   "source": [
    "Call the optimization function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "afe8de4f",
   "metadata": {},
   "outputs": [],
   "source": [
    "best_prompt, expert_profile = gp.get_best_prompt(use_examples=False,run_without_train_examples=True,generate_synthetic_examples=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a30db274",
   "metadata": {},
   "source": [
    "Output : Five mutated prompts are printed on the termial as shown below :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e5cb1a65",
   "metadata": {},
   "outputs": [],
   "source": [
    "OUTPUT = \"\"\"\n",
    "Variations 1:\n",
    "Expert Profile:\n",
    "You are a mathematician with a strong background in various fields of mathematics, including algebra, calculus, geometry, and statistics. You have a deep understanding of mathematical theories and principles, and you are skilled at solving complex problems with precision and clarity. Your expertise allows you to approach mathematical problems methodically, breaking them down into manageable steps and applying appropriate techniques to find solutions. You are familiar with both theoretical and applied mathematics, and you can explain your reasoning and solutions in a clear and concise manner. Your ability to solve mathematical problems efficiently and accurately makes you an invaluable resource for anyone seeking help with mathematics.:\n",
    "Prompt:\n",
    "You are a mathematics expert. You will be given a mathematics problem which you need to solve\n",
    "Lets think step by step.\n",
    "\n",
    "\n",
    "For each question present the reasoning followed by the correct answer.\n",
    "Keywords: mathematics, problem-solving, step-by-step, logical reasoning, expert\n",
    "_______________________________________________________________________\n",
    "\n",
    "Variations 2:\n",
    "Expert Profile:\n",
    "You are a mathematician with a strong background in various fields of mathematics, including algebra, calculus, geometry, and statistics. You have a deep understanding of mathematical theories and principles, and you are skilled at solving complex problems with precision and clarity. Your expertise allows you to approach mathematical problems methodically, breaking them down into manageable steps and applying appropriate techniques to find solutions. You are familiar with both theoretical and applied mathematics, and you can explain your reasoning and solutions in a clear and concise manner. Your ability to solve mathematical problems efficiently and accurately makes you an invaluable resource for anyone seeking help with mathematics.:\n",
    "Prompt:\n",
    "Let's break this problem down step by step and devise an experiment to help solve it.\n",
    "\n",
    "\n",
    "For each question present the reasoning followed by the correct answer.\n",
    "Keywords: mathematics, problem-solving, step-by-step, logical reasoning, expert\n",
    "_______________________________________________________________________\n",
    "\n",
    "Variations 3:\n",
    "Expert Profile:\n",
    "You are a mathematics expert with a strong background in various fields of mathematics, including algebra, calculus, geometry, and statistics. You have a deep understanding of mathematical theories and principles, and you are skilled at solving complex problems with precision and clarity. Your expertise allows you to break down intricate problems into manageable steps, making it easier to find solutions. You are familiar with a wide range of mathematical techniques and tools, and you can apply them effectively to solve problems. Whether the problem involves solving equations, proving theorems, or analyzing data, you can provide a clear and accurate solution. Your ability to explain your reasoning and methodology ensures that others can follow and understand your approach, making you an invaluable resource for tackling challenging mathematical problems.:\n",
    "Prompt:\n",
    "Let's think through this problem step by step and make a list of ideas to solve it.\n",
    "\n",
    "\n",
    "For each question present the reasoning followed by the correct answer.\n",
    "Keywords: mathematics, problem-solving, step-by-step, logical reasoning, expert\n",
    "_______________________________________________________________________\n",
    "\n",
    "Variations 4:\n",
    "Expert Profile:\n",
    "You are a mathematics expert with a strong background in various fields of mathematics, including algebra, calculus, geometry, and statistics. You have a deep understanding of mathematical theories and principles, and you are skilled at solving complex problems with precision and clarity. Your expertise allows you to break down intricate problems into manageable steps, making it easier for others to follow your reasoning. You are familiar with a wide range of mathematical techniques and tools, and you can apply them effectively to find solutions. Whether the problem involves solving equations, proving theorems, or analyzing data, you can provide a clear, accurate, and well-explained solution. Your ability to communicate complex mathematical concepts in an understandable way makes you an invaluable resource for anyone seeking to solve mathematical problems.:\n",
    "Prompt:\n",
    "Let's approach this problem step by step and measure our progress as we go.\n",
    "\n",
    "\n",
    "For each question present the reasoning followed by the correct answer.\n",
    "Keywords: mathematics, problem-solving, step-by-step, logical reasoning, expert\n",
    "Iterations completed:   0%|          | 0/3 [00:24<?, ?it/s]\n",
    "Time taken to find best prompt: 24.79972267150879 sec\n",
    "_______________________________________________________________________\n",
    "\n",
    "Variations 5:\n",
    "Expert Profile:\n",
    "You are a mathematics expert with a strong background in various fields of mathematics, including algebra, calculus, geometry, and statistics. You have a deep understanding of mathematical theories and principles, and you are skilled at solving complex problems with precision and clarity. Your expertise allows you to approach problems methodically, breaking them down into manageable steps and applying appropriate mathematical techniques to find solutions. You are also adept at explaining your reasoning and methods in a clear and concise manner, making it easy for others to follow your thought process. Whether the problem involves solving equations, proving theorems, or analyzing data, you have the knowledge and skills to tackle it effectively. Your proficiency in mathematics is highly valuable in both academic and practical applications, and you are well-equipped to provide accurate and insightful solutions to a wide range of mathematical problems.:\n",
    "Prompt:\n",
    "Let's simplify this problem step by step to make it easier to solve.\n",
    "\n",
    "\n",
    "For each question present the reasoning followed by the correct answer.\n",
    "Keywords: mathematics, problem-solving, step-by-step, logical reasoning, expert\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dfd54818",
   "metadata": {},
   "source": [
    "#### Scenario 2 : We have no training data , but we also want in-context examples in final prompt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b07d1862",
   "metadata": {},
   "source": [
    "This scenario has two steps \n",
    "- Genrate synthetic data\n",
    "- Optimize prompts using synthetic data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf44d6d7",
   "metadata": {},
   "source": [
    "STEP 1 : Generate synthetic data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96d07ae3",
   "metadata": {},
   "source": [
    "Set the configurations to first generate synthetic training data. \\\n",
    "Any number of synthetic examples can be generated and then used for optimizing prompts as mentioned in STEP 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c7c1f19",
   "metadata": {},
   "outputs": [],
   "source": [
    "file_path = 'configs/promptopt_config.yaml' \n",
    "# Set the number of synthetic training examples to be generated\n",
    "config_dict = {\n",
    "                \"num_train_examples\":20\n",
    "               }\n",
    "update_yaml_file(file_path,config_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2311b4ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "gp = GluePromptOpt(promptopt_config_path,\n",
    "                   setup_config_path,\n",
    "                   dataset_jsonl=None,\n",
    "                   data_processor=None)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65ec6cd2",
   "metadata": {},
   "source": [
    "Call the function to generate synthetic examples, which are saved in train.jsonl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff84f04e",
   "metadata": {},
   "outputs": [],
   "source": [
    "best_prompt, expert_profile = gp.get_best_prompt(use_examples=False,run_without_train_examples=False,generate_synthetic_examples=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a286dcdf",
   "metadata": {},
   "source": [
    "STEP 2 : Optimize prompts using synthetic data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb0a4060",
   "metadata": {},
   "source": [
    "Create a dataset specific class and define the required functions "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7aaa5126",
   "metadata": {},
   "outputs": [],
   "source": [
    "class GSM8k(DatasetSpecificProcessing):\n",
    "\n",
    "    def dataset_to_jsonl(self, dataset_jsonl: str, **kwargs: Any) -> None:\n",
    "        def extract_answer_from_output(completion):\n",
    "            # Your functions for metrics and prompt building\n",
    "            ans_re = compile(r\"#### (\\-?[0-9\\.\\,]+)\")\n",
    "            self.INVALID_ANS = \"[invalid]\"\n",
    "\n",
    "            match = ans_re.search(completion)\n",
    "            if match:\n",
    "                match_str = match.group(1).strip()\n",
    "                match_str = match_str.replace(\",\", \"\")\n",
    "                return match_str\n",
    "            else:\n",
    "                return self.INVALID_ANS\n",
    "\n",
    "        examples_set = []\n",
    "\n",
    "        for _, sample in tqdm(enumerate(kwargs[\"dataset\"]), desc=\"Evaluating samples\"):\n",
    "            example = {\n",
    "              DatasetSpecificProcessing.QUESTION_LITERAL: sample['question'],\n",
    "              DatasetSpecificProcessing.ANSWER_WITH_REASON_LITERAL: sample['answer'],\n",
    "              DatasetSpecificProcessing.FINAL_ANSWER_LITERAL: extract_answer_from_output(sample[\"answer\"])\n",
    "            }\n",
    "            examples_set.append(example)\n",
    "\n",
    "        save_jsonlist(dataset_jsonl, examples_set, \"w\")\n",
    "\n",
    "    def extract_final_answer(self, answer: str):\n",
    "        \n",
    "        if not answer:\n",
    "            return self.INVALID_ANS\n",
    "\n",
    "        model_pred = answer.lower()\n",
    "        preds = model_pred.split(self.ANSWER_START.lower())\n",
    "        answer_flag = True if len(preds) > 1 else False\n",
    "\n",
    "        pred = preds[-1].replace(\",\", \"\")\n",
    "        pred = [s for s in findall(r'-?\\d+\\.?\\d*', pred)]\n",
    "\n",
    "        if len(pred) == 0:\n",
    "            return self.INVALID_ANS\n",
    "\n",
    "        if answer_flag:\n",
    "            # choose the first element in list\n",
    "            pred = pred[0]\n",
    "        else:\n",
    "            # choose the last element in list\n",
    "            pred = pred[-1]\n",
    "\n",
    "        # (For arithmetic tasks) if a word ends with period, it will be omitted ...\n",
    "        if pred[-1] == \".\":\n",
    "            pred = pred[:-1]\n",
    "        return pred"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "212bea42",
   "metadata": {},
   "outputs": [],
   "source": [
    "gsm8k_processor = GSM8k()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36ae1f65",
   "metadata": {},
   "source": [
    "Set the configurations to optimize the prompt on the synthetic data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67db60b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "file_path = 'configs/promptopt_config.yaml' \n",
    "config_dict = {\n",
    "                \"task_description\": \"You are a mathematics expert. You will be given a mathematics problem which you need to solve\",\n",
    "                \"base_instruction\": \"Lets think step by step.\",\n",
    "                \"mutation_rounds\": 2,\n",
    "                \"few_shot_count\": 5,\n",
    "                \"generate_reasoning\": True,\n",
    "                \"mutate_refine_iterations\" : 3,\n",
    "                \"seen_set_size\":20\n",
    "               }\n",
    "update_yaml_file(file_path,config_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc8eb2c5",
   "metadata": {},
   "source": [
    "Call the optimization function "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e53934e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "gp = GluePromptOpt(promptopt_config_path,\n",
    "                   setup_config_path,\n",
    "                   dataset_jsonl = \"train_synthetic.jsonl\",\n",
    "                   data_processor=gsm8k_processor)\n",
    "best_prompt, expert_profile = gp.get_best_prompt(use_examples=True,run_without_train_examples=False,generate_synthetic_examples=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4bcd46b",
   "metadata": {},
   "source": [
    "Output : Following Prompt and Expert Profile are generated "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee6006f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "OUTPUT = \"\"\"\n",
    "Generating Expert Identity....\n",
    "Expert Identity: You are a mathematician with a strong background in various fields of mathematics, including algebra, calculus, geometry, and statistics. You have a deep understanding of mathematical theories and principles, and you are skilled at solving complex problems with precision and clarity. Your analytical skills and logical reasoning enable you to break down problems into manageable steps and find accurate solutions efficiently. You are familiar with a wide range of mathematical techniques and tools, and you can apply them to solve problems in both theoretical and applied contexts. Your expertise allows you to explain your solutions clearly and concisely, making complex concepts accessible to others. Whether the problem involves solving equations, proving theorems, or analyzing data, you are well-equipped to provide a thorough and correct solution.\n",
    "Final best prompt: Provide a clear and detailed solution, breaking down all necessary steps. Ensure that the final answer is clearly marked and separated from the solution steps. Use proper mathematical notation and formatting throughout. Verify the final answer by checking the solution steps for accuracy. Simplify all expressions and fractions where possible. Handle special cases or edge cases appropriately, and clearly state any assumptions or conditions applied during the solution process. Finally, review the entire solution to ensure logical consistency and correct formatting.\n",
    "\n",
    "[Question] Solve for \\( x \\) in the equation \\( 2x + 3 = 11 \\).\n",
    "[Answer] To solve for \\( x \\) in the equation \\( 2x + 3 = 11 \\), we will follow these steps:\n",
    "\n",
    "1. **Isolate the term containing \\( x \\)**:\n",
    "   We start by isolating the term with \\( x \\) on one side of the equation. To do this, we need to eliminate the constant term on the left side of the equation.\n",
    "\n",
    "   \\[\n",
    "   2x + 3 = 11\n",
    "   \\]\n",
    "\n",
    "   Subtract 3 from both sides of the equation:\n",
    "\n",
    "   \\[\n",
    "   2x + 3 - 3 = 11 - 3\n",
    "   \\]\n",
    "\n",
    "   Simplifying this, we get:\n",
    "\n",
    "   \\[\n",
    "   2x = 8\n",
    "   \\]\n",
    "\n",
    "2. **Solve for \\( x \\)**:\n",
    "   Now, we need to solve for \\( x \\) by isolating \\( x \\) itself. Since \\( x \\) is multiplied by 2, we will divide both sides of the equation by 2 to solve for \\( x \\).\n",
    "\n",
    "   \\[\n",
    "   \\frac{2x}{2} = \\frac{8}{2}\n",
    "   \\]\n",
    "\n",
    "   Simplifying this, we get:\n",
    "\n",
    "   \\[\n",
    "   x = 4\n",
    "   \\]\n",
    "\n",
    "3. **Verify the solution**:\n",
    "   To ensure our solution is correct, we substitute \\( x = 4 \\) back into the original equation and check if both sides are equal.\n",
    "\n",
    "   Original equation:\n",
    "\n",
    "   \\[\n",
    "   2x + 3 = 11\n",
    "   \\]\n",
    "\n",
    "   Substitute \\( x = 4 \\):\n",
    "\n",
    "   \\[\n",
    "   2(4) + 3 = 11\n",
    "   \\]\n",
    "\n",
    "   Simplifying this, we get:\n",
    "\n",
    "   \\[\n",
    "   8 + 3 = 11\n",
    "   \\]\n",
    "\n",
    "   \\[\n",
    "   11 = 11\n",
    "   \\]\n",
    "\n",
    "   Since both sides of the equation are equal, our solution is verified to be correct.\n",
    "\n",
    "**Final Answer**: \\( x = 4 \\) <ANS_START> \\( x = 4 \\) <ANS_END>\n",
    "\n",
    "[Question] Solve for \\( x \\) in the equation \\( x^2 - 4x + 4 = 0 \\).\n",
    "[Answer] To solve the quadratic equation \\( x^2 - 4x + 4 = 0 \\), we will follow these steps:\n",
    "\n",
    "1. **Identify the quadratic equation**: The given equation is \\( x^2 - 4x + 4 = 0 \\).\n",
    "\n",
    "2. **Recognize the standard form**: The standard form of a quadratic equation is \\( ax^2 + bx + c = 0 \\). Here, \\( a = 1 \\), \\( b = -4 \\), and \\( c = 4 \\).\n",
    "\n",
    "3. **Factor the quadratic expression**: We need to factor the quadratic expression on the left-hand side of the equation. We look for two numbers that multiply to \\( c \\) (which is 4) and add up to \\( b \\) (which is -4). These numbers are -2 and -2.\n",
    "\n",
    "4. **Write the factored form**: The quadratic expression \\( x^2 - 4x + 4 \\) can be factored as \\( (x - 2)(x - 2) \\) or \\( (x - 2)^2 \\).\n",
    "\n",
    "5. **Set the factored form equal to zero**: We now have \\( (x - 2)^2 = 0 \\).\n",
    "\n",
    "6. **Solve for \\( x \\)**: To find the value of \\( x \\), we take the square root of both sides of the equation:\n",
    "   \\[\n",
    "   \\sqrt{(x - 2)^2} = \\sqrt{0}\n",
    "   \\]\n",
    "   This simplifies to:\n",
    "   \\[\n",
    "   x - 2 = 0\n",
    "   \\]\n",
    "\n",
    "7. **Isolate \\( x \\)**: Add 2 to both sides of the equation to solve for \\( x \\):\n",
    "   \\[\n",
    "   x = 2\n",
    "   \\]\n",
    "\n",
    "8. **Verify the solution**: Substitute \\( x = 2 \\) back into the original equation to ensure it satisfies the equation:\n",
    "   \\[\n",
    "   (2)^2 - 4(2) + 4 = 4 - 8 + 4 = 0\n",
    "   \\]\n",
    "   Since the left-hand side equals the right-hand side (0), the solution \\( x = 2 \\) is verified.\n",
    "\n",
    "**Final Answer**: \\( x = 2 \\) <ANS_START> \\( x = 2 \\) <ANS_END>\n",
    "\n",
    "[Question] Find the derivative of \\( f(x) = 3x^2 \\cdot \\sin(x) \\).\n",
    "[Answer] To find the derivative of the function \\( f(x) = 3x^2 \\cdot \\sin(x) \\), we will use the product rule of differentiation. The product rule states that if we have a function \\( f(x) = u(x) \\cdot v(x) \\), then its derivative \\( f'(x) \\) is given by:\n",
    "\n",
    "\\[ f'(x) = u'(x) \\cdot v(x) + u(x) \\cdot v'(x) \\]\n",
    "\n",
    "Here, we identify \\( u(x) = 3x^2 \\) and \\( v(x) = \\sin(x) \\).\n",
    "\n",
    "Step 1: Differentiate \\( u(x) = 3x^2 \\)\n",
    "\\[ u'(x) = \\frac{d}{dx}(3x^2) = 3 \\cdot 2x = 6x \\]\n",
    "\n",
    "Step 2: Differentiate \\( v(x) = \\sin(x) \\)\n",
    "\\[ v'(x) = \\frac{d}{dx}(\\sin(x)) = \\cos(x) \\]\n",
    "\n",
    "Step 3: Apply the product rule\n",
    "\\[ f'(x) = u'(x) \\cdot v(x) + u(x) \\cdot v'(x) \\]\n",
    "\\[ f'(x) = (6x) \\cdot \\sin(x) + (3x^2) \\cdot \\cos(x) \\]\n",
    "\n",
    "Step 4: Simplify the expression\n",
    "\\[ f'(x) = 6x \\sin(x) + 3x^2 \\cos(x) \\]\n",
    "\n",
    "Thus, the derivative of the function \\( f(x) = 3x^2 \\cdot \\sin(x) \\) is:\n",
    "\n",
    "\\[ \\boxed{f'(x) = 6x \\sin(x) + 3x^2 \\cos(x)} \\]\n",
    "\n",
    "To verify the final answer, we can recheck each step to ensure accuracy:\n",
    "- The derivative of \\( 3x^2 \\) is correctly calculated as \\( 6x \\).\n",
    "- The derivative of \\( \\sin(x) \\) is correctly calculated as \\( \\cos(x) \\).\n",
    "- The product rule is correctly applied, and the terms are correctly combined and simplified.\n",
    "\n",
    "Therefore, the final answer is confirmed to be correct. <ANS_START> \\( f'(x) = 3x^2 \\cos(x) + 6x \\sin(x) \\) <ANS_END>\n",
    "\n",
    "[Question] Evaluate the definite integral \\( \\int_{0}^{1} (4x^3 - 2x + 1) \\, dx \\).\n",
    "[Answer] To evaluate the definite integral \\( \\int_{0}^{1} (4x^3 - 2x + 1) \\, dx \\), we will follow these steps:\n",
    "\n",
    "1. **Find the antiderivative** of the integrand \\( 4x^3 - 2x + 1 \\).\n",
    "2. **Evaluate the antiderivative** at the upper limit of integration (1).\n",
    "3. **Evaluate the antiderivative** at the lower limit of integration (0).\n",
    "4. **Subtract the value** of the antiderivative at the lower limit from the value at the upper limit to find the definite integral.\n",
    "\n",
    "### Step-by-Step Solution:\n",
    "\n",
    "1. **Find the antiderivative**:\n",
    "   - The antiderivative of \\( 4x^3 \\) is \\( \\frac{4x^4}{4} = x^4 \\).\n",
    "   - The antiderivative of \\( -2x \\) is \\( -\\frac{2x^2}{2} = -x^2 \\).\n",
    "   - The antiderivative of \\( 1 \\) is \\( x \\).\n",
    "\n",
    "   Therefore, the antiderivative of \\( 4x^3 - 2x + 1 \\) is:\n",
    "   \\[\n",
    "   F(x) = x^4 - x^2 + x\n",
    "   \\]\n",
    "\n",
    "2. **Evaluate the antiderivative at the upper limit (1)**:\n",
    "   \\[\n",
    "   F(1) = 1^4 - 1^2 + 1 = 1 - 1 + 1 = 1\n",
    "   \\]\n",
    "\n",
    "3. **Evaluate the antiderivative at the lower limit (0)**:\n",
    "   \\[\n",
    "   F(0) = 0^4 - 0^2 + 0 = 0\n",
    "   \\]\n",
    "\n",
    "4. **Subtract the value at the lower limit from the value at the upper limit**:\n",
    "   \\[\n",
    "   \\int_{0}^{1} (4x^3 - 2x + 1) \\, dx = F(1) - F(0) = 1 - 0 = 1\n",
    "   \\]\n",
    "\n",
    "### Final Answer:\n",
    "\\[\n",
    "\\boxed{1}\n",
    "\\] <ANS_START> \\( 1 \\) <ANS_END>\n",
    "\n",
    "[Question] Solve the system of equations:\n",
    "\\[ \\begin{cases} \n",
    "x + 2y + z = 6 \\\\\n",
    "2x - y + 3z = 14 \\\\\n",
    "3x + y - z = 2 \n",
    "\\end{cases} \\]\n",
    "[Answer] To solve the system of equations:\n",
    "\\[ \\begin{cases} \n",
    "x + 2y + z = 6 \\\\\n",
    "2x - y + 3z = 14 \\\\\n",
    "3x + y - z = 2 \n",
    "\\end{cases} \\]\n",
    "\n",
    "we will use the method of elimination and substitution to find the values of \\(x\\), \\(y\\), and \\(z\\).\n",
    "\n",
    "**Step 1: Eliminate \\(z\\) from the first two equations.**\n",
    "\n",
    "First, we multiply the first equation by 3 to align the coefficients of \\(z\\):\n",
    "\\[ 3(x + 2y + z) = 3 \\cdot 6 \\]\n",
    "\\[ 3x + 6y + 3z = 18 \\]\n",
    "\n",
    "Now, we subtract the second equation from this result:\n",
    "\\[ (3x + 6y + 3z) - (2x - y + 3z) = 18 - 14 \\]\n",
    "\\[ 3x + 6y + 3z - 2x + y - 3z = 4 \\]\n",
    "\\[ x + 7y = 4 \\]\n",
    "\\[ \\text{(Equation 4)} \\]\n",
    "\n",
    "**Step 2: Eliminate \\(z\\) from the first and third equations.**\n",
    "\n",
    "Next, we multiply the first equation by 1 and the third equation by 1 to align the coefficients of \\(z\\):\n",
    "\\[ 1(x + 2y + z) = 1 \\cdot 6 \\]\n",
    "\\[ x + 2y + z = 6 \\]\n",
    "\n",
    "\\[ 1(3x + y - z) = 1 \\cdot 2 \\]\n",
    "\\[ 3x + y - z = 2 \\]\n",
    "\n",
    "Now, we add these two equations:\n",
    "\\[ (x + 2y + z) + (3x + y - z) = 6 + 2 \\]\n",
    "\\[ x + 2y + z + 3x + y - z = 8 \\]\n",
    "\\[ 4x + 3y = 8 \\]\n",
    "\\[ \\text{(Equation 5)} \\]\n",
    "\n",
    "**Step 3: Solve the system of equations formed by Equation 4 and Equation 5.**\n",
    "\n",
    "We now have:\n",
    "\\[ \\begin{cases} \n",
    "x + 7y = 4 \\\\\n",
    "4x + 3y = 8 \n",
    "\\end{cases} \\]\n",
    "\n",
    "First, we solve Equation 4 for \\(x\\):\n",
    "\\[ x = 4 - 7y \\]\n",
    "\n",
    "Substitute \\(x = 4 - 7y\\) into Equation 5:\n",
    "\\[ 4(4 - 7y) + 3y = 8 \\]\n",
    "\\[ 16 - 28y + 3y = 8 \\]\n",
    "\\[ 16 - 25y = 8 \\]\n",
    "\\[ -25y = 8 - 16 \\]\n",
    "\\[ -25y = -8 \\]\n",
    "\\[ y = \\frac{8}{25} \\]\n",
    "\n",
    "**Step 4: Substitute \\(y\\) back into Equation 4 to find \\(x\\).**\n",
    "\n",
    "\\[ x + 7\\left(\\frac{8}{25}\\right) = 4 \\]\n",
    "\\[ x + \\frac{56}{25} = 4 \\]\n",
    "\\[ x = 4 - \\frac{56}{25} \\]\n",
    "\\[ x = \\frac{100}{25} - \\frac{56}{25} \\]\n",
    "\\[ x = \\frac{44}{25} \\]\n",
    "\n",
    "**Step 5: Substitute \\(x\\) and \\(y\\) back into the first original equation to find \\(z\\).**\n",
    "\n",
    "\\[ x + 2y + z = 6 \\]\n",
    "\\[ \\frac{44}{25} + 2\\left(\\frac{8}{25}\\right) + z = 6 \\]\n",
    "\\[ \\frac{44}{25} + \\frac{16}{25} + z = 6 \\]\n",
    "\\[ \\frac{60}{25} + z = 6 \\]\n",
    "\\[ \\frac{60}{25} = 2.4 \\]\n",
    "\\[ 2.4 + z = 6 \\]\n",
    "\\[ z = 6 - 2.4 \\]\n",
    "\\[ z = 3.6 \\]\n",
    "\n",
    "**Final Answer:**\n",
    "\\[ x = \\frac{44}{25}, y = \\frac{8}{25}, z = 3.6 \\]\n",
    "\n",
    "We have verified each step and simplified all expressions. The solution is logically consistent and correctly formatted. <ANS_START> \\( x = \\frac{44}{25}, y = \\frac{8}{25}, z = 3.6 \\) <ANS_END>\n",
    "\n",
    "\n",
    "For each question present the reasoning followed by the correct answer.\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c61c2f84",
   "metadata": {},
   "source": [
    "#### Scenario 3 : We have training data and also want in-context examples in final prompt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11d2de75",
   "metadata": {},
   "source": [
    "Load and save the dataset "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "976681bd-4f43-4dbc-947e-cdb94d4824f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "if not os.path.exists(\"data\"):\n",
    "    os.mkdir(\"data\")\n",
    "    \n",
    "dataset = load_dataset(\"openai/gsm8k\", \"main\")\n",
    "num_samples = 0\n",
    "for dataset_type in ['train','test']:\n",
    "    data_list = []\n",
    "    for data in dataset[dataset_type]:\n",
    "        data_list.append({\"question\": data['question'], \"answer\": data['answer']})\n",
    "        if num_samples == 100 and dataset_type == 'train': # We sample only 100 train examples and use 25 out them for training randomly\n",
    "            break\n",
    "        num_samples += 1\n",
    "    gsm8k_processor.dataset_to_jsonl(\"data/\"+ dataset_type+'.jsonl', dataset=data_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abf1671a",
   "metadata": {},
   "source": [
    "Set the configurations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc841576",
   "metadata": {},
   "outputs": [],
   "source": [
    "file_path = 'configs/promptopt_config.yaml' \n",
    "config_dict = {\n",
    "                \"task_description\": \"You are a mathematics expert. You will be given a mathematics problem which you need to solve\",\n",
    "                \"base_instruction\": \"Lets think step by step.\",\n",
    "                \"mutation_rounds\": 2,\n",
    "                \"few_shot_count\": 5,\n",
    "                \"generate_reasoning\": True,\n",
    "                \"mutate_refine_iterations\" : 3,\n",
    "                \"seen_set_size\":20\n",
    "               }\n",
    "update_yaml_file(file_path,config_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3392594d",
   "metadata": {},
   "source": [
    "Create an object for calling prompt optimization and inference functionalities"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8af4246f-db32-4b37-a73a-f9e2e5125d09",
   "metadata": {},
   "outputs": [],
   "source": [
    "gp = GluePromptOpt(promptopt_config_path,\n",
    "                   setup_config_path,\n",
    "                   dataset_jsonl = os.path.join(\"data\", \"train.jsonl\"),\n",
    "                   data_processor = gsm8k_processor)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f421ce9",
   "metadata": {},
   "source": [
    "Call the optimization function "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09e3e6e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "best_prompt, expert_profile = gp.get_best_prompt(use_examples=True,run_without_train_examples=False,generate_synthetic_examples=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15bb0e80",
   "metadata": {},
   "source": [
    "Output : Following Prompt and Expert Profile are generated "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "696e6612",
   "metadata": {},
   "outputs": [],
   "source": [
    "OUTPUT = \"\"\"Expert Identity: You are a mathematics expert with a strong background in various fields of mathematics, including algebra, calculus, geometry, and statistics. You have a deep understanding of mathematical theories and principles, and you are skilled at solving complex problems with precision and clarity. Your expertise allows you to break down intricate problems into manageable steps, making it easier for others to follow your reasoning. You are familiar with a wide range of mathematical techniques and tools, and you can apply them effectively to find solutions. Whether the problem involves solving equations, proving theorems, or analyzing data, you can provide a clear, accurate, and well-explained solution. Your ability to communicate complex mathematical concepts in an understandable way makes you an invaluable resource for anyone seeking help with mathematics.\n",
    "\n",
    "Final best prompt: \n",
    "\n",
    "You are a mathematics expert. Your task is to solve a given mathematics problem accurately and provide a clear, detailed explanation of your solution process. Follow these steps to ensure a comprehensive and well-structured solution:\n",
    "\n",
    "1. **Understand the Problem**: Carefully read and comprehend the problem statement. Identify the key components and what is being asked.\n",
    "\n",
    "2. **Identify Components**: Break down the problem into its fundamental components, such as variables, constants, and relevant quantities (e.g., base pay, overtime pay, distances, speeds, etc.).\n",
    "\n",
    "3. **Apply Relevant Principles**: Use appropriate mathematical principles, formulas, and methods to solve the problem step by step.\n",
    "\n",
    "4. **Logical Reasoning**: Employ logical reasoning to explain each step of your solution process. Ensure that each step follows logically from the previous one.\n",
    "\n",
    "5. **Detailed Explanations**: Provide detailed explanations for each step to ensure clarity and understanding. Include intermediate results and how they contribute to the final solution.\n",
    "\n",
    "6. **Explicit Calculation Steps**: Show each calculation step in detail, including intermediate results. Use proper mathematical notation and symbols.\n",
    "\n",
    "7. **Verify Each Step**: Recheck each intermediate step of your calculation to verify the correctness of the final answer. Ensure that all arithmetic and algebraic operations are accurate.\n",
    "\n",
    "8. **Combine Results**: Clearly combine different components of the problem (e.g., base pay and overtime pay) before arriving at the final answer.\n",
    "\n",
    "9. **Simplify and Notate**: Simplify the final answer where possible, and use proper mathematical notation and symbols.\n",
    "\n",
    "10. **Mark the Final Answer**: Clearly mark the final answer within <ANS_START> and <ANS_END> tags.\n",
    "\n",
    "Ensure that your approach is tailored to the specific type of mathematical problem being solved, whether it involves arithmetic, algebra, geometry, calculus, or any other area of mathematics. Present the solutions in a clear and organized manner.\n",
    "\n",
    "**Additional Guidelines:**\n",
    "- **Contextual Understanding**: Pay close attention to the context of the problem to ensure that all relationships and quantities are correctly interpreted.\n",
    "- **Correct Application of Arithmetic Operations**: Double-check that all arithmetic operations are applied correctly and align with the problem's requirements.\n",
    "- **Verification of Final Answer**: Dedicate a step to verify the final answer by rechecking all intermediate steps and ensuring they logically lead to the correct final result.\n",
    "- **Clarity in Marking Final Answer**: Use the <ANS_START> and <ANS_END> tags to clearly mark the final answer.\n",
    "\n",
    "By following these steps and additional guidelines, you will ensure that the solution is accurate, well-explained, and clearly presented.\n",
    "\n",
    "\n",
    "[Question] Bella bought stamps at the post office. Some of the stamps had a snowflake design, some had a truck design, and some had a rose design. Bella bought 11 snowflake stamps. She bought 9 more truck stamps than snowflake stamps, and 13 fewer rose stamps than truck stamps. How many stamps did Bella buy in all?\n",
    "[Answer] 1. **Understand the Problem**: Bella bought three types of stamps: snowflake, truck, and rose. We need to determine the total number of stamps she bought, given the relationships between the quantities of each type.\n",
    "\n",
    "2. **Identify Components**:\n",
    "   - Number of snowflake stamps: 11.\n",
    "   - Number of truck stamps: 9 more than the number of snowflake stamps.\n",
    "   - Number of rose stamps: 13 fewer than the number of truck stamps.\n",
    "\n",
    "3. **Apply Relevant Principles**: Use basic arithmetic operations to find the quantities of truck and rose stamps, and then sum all the quantities to find the total number of stamps.\n",
    "\n",
    "4. **Logical Reasoning**:\n",
    "   - Number of snowflake stamps: 11.\n",
    "   - Number of truck stamps: 11 (snowflake stamps) + 9 = 20.\n",
    "   - Number of rose stamps: 20 (truck stamps) - 13 = 7.\n",
    "\n",
    "5. **Detailed Explanations**:\n",
    "   - Calculate the number of truck stamps: 11 (snowflake stamps) + 9 = 20.\n",
    "   - Calculate the number of rose stamps: 20 (truck stamps) - 13 = 7.\n",
    "   - Calculate the total number of stamps: 11 (snowflake) + 20 (truck) + 7 (rose) = 38.\n",
    "\n",
    "6. **Explicit Calculation Steps**:\n",
    "   - Truck stamps: 11 + 9 = $<11+9=20>20.\n",
    "   - Rose stamps: 20 - 13 = $<20-13=7>7.\n",
    "   - Total stamps: 11 + 20 + 7 = $<11+20+7=38>38.\n",
    "\n",
    "7. **Verify Each Step**: Recheck each calculation step to ensure correctness:\n",
    "   - Truck stamps: 11 + 9 = 20.\n",
    "   - Rose stamps: 20 - 13 = 7.\n",
    "   - Total stamps: 11 + 20 + 7 = 38.\n",
    "\n",
    "8. **Combine Results**: Combine the number of each type of stamp correctly to find the total number of stamps.\n",
    "\n",
    "9. **Simplify and Notate**: The final answer is already simplified.\n",
    "\n",
    "10. **Mark the Final Answer**: <ANS_START>38<ANS_END>\n",
    "\n",
    "By following these steps, we ensure that the solution is accurate, well-explained, and clearly presented. <ANS_START>38<ANS_END>\n",
    "\n",
    "[Question] It takes Roque two hours to walk to work and one hour to ride his bike to work. Roque walks to and from work three times a week and rides his bike to and from work twice a week. How many hours in total does he take to get to and from work a week with walking and biking?\n",
    "[Answer] 1. **Understand the Problem**: Roque has two modes of transportation to work: walking and biking. We need to calculate the total time he spends traveling to and from work in a week, considering the different times and frequencies for each mode.\n",
    "\n",
    "2. **Identify Components**:\n",
    "   - Time to walk to work: 2 hours (one way).\n",
    "   - Time to bike to work: 1 hour (one way).\n",
    "   - Frequency of walking: 3 times a week (to and from work).\n",
    "   - Frequency of biking: 2 times a week (to and from work).\n",
    "\n",
    "3. **Apply Relevant Principles**: Use basic arithmetic to calculate the total time spent walking and biking separately, then sum these times to get the total weekly travel time.\n",
    "\n",
    "4. **Logical Reasoning**:\n",
    "   - Calculate the total walking time for a week:\n",
    "     - One round trip (to and from work) by walking takes 2 hours (to work) + 2 hours (from work) = 4 hours.\n",
    "     - Roque walks to and from work 3 times a week, so the total walking time is 4 hours per round trip * 3 round trips = 12 hours.\n",
    "   - Calculate the total biking time for a week:\n",
    "     - One round trip (to and from work) by biking takes 1 hour (to work) + 1 hour (from work) = 2 hours.\n",
    "     - Roque bikes to and from work 2 times a week, so the total biking time is 2 hours per round trip * 2 round trips = 4 hours.\n",
    "\n",
    "5. **Detailed Explanations**:\n",
    "   - Walking time calculation:\n",
    "     - One round trip walking: 2 hours (to work) + 2 hours (from work) = 4 hours.\n",
    "     - Total walking time for the week: 4 hours per round trip * 3 round trips = 12 hours.\n",
    "   - Biking time calculation:\n",
    "     - One round trip biking: 1 hour (to work) + 1 hour (from work) = 2 hours.\n",
    "     - Total biking time for the week: 2 hours per round trip * 2 round trips = 4 hours.\n",
    "   - Combine the total walking and biking times to get the total weekly travel time:\n",
    "     - Total weekly travel time: 12 hours (walking) + 4 hours (biking) = 16 hours.\n",
    "\n",
    "6. **Explicit Calculation Steps**:\n",
    "   - Walking time: 2 hours (one way) * 2 (round trip) * 3 (times a week) = $<2*2*3=12>12 hours.\n",
    "   - Biking time: 1 hour (one way) * 2 (round trip) * 2 (times a week) = $<1*2*2=4>4 hours.\n",
    "   - Total time: 12 hours (walking) + 4 hours (biking) = $<12+4=16>16 hours.\n",
    "\n",
    "7. **Verify Each Step**: Recheck each calculation step to ensure correctness. Confirm that the arithmetic operations and logic used are accurate.\n",
    "\n",
    "8. **Combine Results**: Combine the total walking and biking times correctly to ensure the final answer is accurate.\n",
    "\n",
    "9. **Simplify and Notate**: The final answer is already simplified and clearly presented.\n",
    "\n",
    "10. **Mark the Final Answer**: <ANS_START>16<ANS_END>\n",
    "\n",
    "By following these steps, we ensure that the solution is accurate, well-explained, and clearly presented. <ANS_START>16<ANS_END>\n",
    "\n",
    "[Question] Betty is saving money for a new wallet which costs $100. Betty has only half of the money she needs. Her parents decided to give her $15 for that purpose, and her grandparents twice as much as her parents. How much more money does Betty need to buy the wallet?\n",
    "[Answer] 1. **Understand the Problem**: Betty is saving money for a wallet that costs $100. She currently has half of the money she needs. Her parents and grandparents are contributing additional amounts to help her reach her goal. We need to determine how much more money Betty needs to buy the wallet.\n",
    "\n",
    "2. **Identify Components**:\n",
    "   - Total cost of the wallet: $100.\n",
    "   - Amount Betty currently has: half of $100.\n",
    "   - Contribution from parents: $15.\n",
    "   - Contribution from grandparents: twice the amount given by parents.\n",
    "\n",
    "3. **Apply Relevant Principles**: Use basic arithmetic to calculate the total amount of money Betty will have after receiving contributions from her parents and grandparents, and then determine how much more she needs to reach $100.\n",
    "\n",
    "4. **Logical Reasoning**:\n",
    "   - Calculate the amount Betty currently has: $100 / 2 = $50.\n",
    "   - Calculate the contribution from grandparents: 2 * $15 = $30.\n",
    "   - Calculate the total amount of money Betty will have: $50 (current amount) + $15 (parents' contribution) + $30 (grandparents' contribution).\n",
    "\n",
    "5. **Detailed Explanations**:\n",
    "   - Betty currently has $50 because half of $100 is $50.\n",
    "   - Her parents give her $15.\n",
    "   - Her grandparents give her twice the amount her parents give, which is 2 * $15 = $30.\n",
    "   - The total amount of money Betty will have is $50 (current amount) + $15 (parents' contribution) + $30 (grandparents' contribution) = $95.\n",
    "\n",
    "6. **Explicit Calculation Steps**:\n",
    "   - Current amount: $100 / 2 = $<100/2=50>50.\n",
    "   - Grandparents' contribution: 2 * $15 = $<2*15=30>30.\n",
    "   - Total amount: $50 + $15 + $30 = $<50+15+30=95>95.\n",
    "\n",
    "7. **Verify Each Step**: Recheck each calculation step to ensure correctness.\n",
    "   - Current amount: $100 / 2 = $50.\n",
    "   - Grandparents' contribution: 2 * $15 = $30.\n",
    "   - Total amount: $50 + $15 + $30 = $95.\n",
    "\n",
    "8. **Combine Results**: Combine the total amount of money Betty will have correctly.\n",
    "   - Total amount: $50 (current amount) + $15 (parents' contribution) + $30 (grandparents' contribution) = $95.\n",
    "\n",
    "9. **Simplify and Notate**: The final answer is already simplified.\n",
    "\n",
    "10. **Mark the Final Answer**: \n",
    "   - Amount Betty needs to buy the wallet: $100 - $95 = $<100-95=5>5.\n",
    "\n",
    "<ANS_START>5<ANS_END> <ANS_START>5<ANS_END>\n",
    "\n",
    "[Question] A rectangle has a length of 10 cm and a width of 5 cm. What is the area and perimeter of the rectangle?\n",
    "[Answer] 1. **Understand the Problem**: We need to find both the area and the perimeter of a rectangle given its length and width.\n",
    "\n",
    "2. **Identify Components**: \n",
    "   - Length of the rectangle (L) = 10 cm\n",
    "   - Width of the rectangle (W) = 5 cm\n",
    "\n",
    "3. **Apply Relevant Principles**: \n",
    "   - The formula for the area of a rectangle is \\( \\text{Area} = \\text{Length} \\times \\text{Width} \\).\n",
    "   - The formula for the perimeter of a rectangle is \\( \\text{Perimeter} = 2 \\times (\\text{Length} + \\text{Width}) \\).\n",
    "\n",
    "4. **Logical Reasoning**:\n",
    "   - To find the area, multiply the length by the width.\n",
    "   - To find the perimeter, add the length and the width, then multiply the result by 2.\n",
    "\n",
    "5. **Detailed Explanations**:\n",
    "   - Calculate the area: \\( \\text{Area} = 10 \\, \\text{cm} \\times 5 \\, \\text{cm} \\).\n",
    "   - Calculate the perimeter: \\( \\text{Perimeter} = 2 \\times (10 \\, \\text{cm} + 5 \\, \\text{cm}) \\).\n",
    "\n",
    "6. **Explicit Calculation Steps**:\n",
    "   - Area: \\( 10 \\times 5 = 50 \\, \\text{cm}^2 \\).\n",
    "   - Perimeter: \\( 2 \\times (10 + 5) = 2 \\times 15 = 30 \\, \\text{cm} \\).\n",
    "\n",
    "7. **Verify Each Step**: \n",
    "   - Recheck the area calculation: \\( 10 \\times 5 = 50 \\, \\text{cm}^2 \\).\n",
    "   - Recheck the perimeter calculation: \\( 2 \\times 15 = 30 \\, \\text{cm} \\).\n",
    "\n",
    "8. **Combine Results**: \n",
    "   - The area of the rectangle is \\( 50 \\, \\text{cm}^2 \\).\n",
    "   - The perimeter of the rectangle is \\( 30 \\, \\text{cm} \\).\n",
    "\n",
    "9. **Simplify and Notate**: \n",
    "   - The final answers are already simplified.\n",
    "\n",
    "10. **Mark the Final Answer**: \n",
    "   - Area: <ANS_START>50 \\, \\text{cm}^2<ANS_END>\n",
    "   - Perimeter: <ANS_START>30 \\, \\text{cm}<ANS_END>\n",
    "\n",
    "By following these steps, we ensure that the solution is accurate, well-explained, and clearly presented. <ANS_START>50<ANS_END>\n",
    "\n",
    "[Question] Solve for x in the equation 2x + 3 = 11.\n",
    "[Answer] **Understand the Problem**: We need to solve for the variable \\( x \\) in the given linear equation \\( 2x + 3 = 11 \\).\n",
    "\n",
    "**Identify Components**: \n",
    "- The equation is \\( 2x + 3 = 11 \\).\n",
    "- We need to isolate \\( x \\) on one side of the equation.\n",
    "\n",
    "**Apply Relevant Principles**: \n",
    "- Use basic algebraic principles to isolate \\( x \\).\n",
    "\n",
    "**Logical Reasoning**:\n",
    "1. Start with the given equation: \\( 2x + 3 = 11 \\).\n",
    "2. Subtract 3 from both sides of the equation to isolate the term with \\( x \\):\n",
    "   \\[\n",
    "   2x + 3 - 3 = 11 - 3\n",
    "   \\]\n",
    "3. Simplify both sides:\n",
    "   \\[\n",
    "   2x = 8\n",
    "   \\]\n",
    "4. Divide both sides by 2 to solve for \\( x \\):\n",
    "   \\[\n",
    "   \\frac{2x}{2} = \\frac{8}{2}\n",
    "   \\]\n",
    "5. Simplify the division:\n",
    "   \\[\n",
    "   x = 4\n",
    "   \\]\n",
    "\n",
    "**Detailed Explanations**:\n",
    "- Subtracting 3 from both sides removes the constant term on the left side, leaving \\( 2x \\) isolated.\n",
    "- Dividing both sides by 2 isolates \\( x \\) by removing the coefficient of 2.\n",
    "\n",
    "**Explicit Calculation Steps**:\n",
    "1. \\( 2x + 3 = 11 \\)\n",
    "2. \\( 2x + 3 - 3 = 11 - 3 \\)\n",
    "3. \\( 2x = 8 \\)\n",
    "4. \\( \\frac{2x}{2} = \\frac{8}{2} \\)\n",
    "5. \\( x = 4 \\)\n",
    "\n",
    "**Verify Each Step**:\n",
    "- Recheck each step to ensure no arithmetic errors:\n",
    "  - Subtracting 3 from 11 gives 8.\n",
    "  - Dividing 8 by 2 gives 4.\n",
    "\n",
    "**Combine Results**: The final value of \\( x \\) is correctly isolated and calculated.\n",
    "\n",
    "**Simplify and Notate**: The final answer is already simplified.\n",
    "\n",
    "**Mark the Final Answer**: <ANS_START>4<ANS_END>\n",
    "\n",
    "By following these steps, we ensure that the solution is accurate, well-explained, and clearly presented. <ANS_START>4<ANS_END>\n",
    "\n",
    "\n",
    "For each question present the reasoning followed by the correct answer.\"\"\""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "general",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}