yaleh commited on
Commit
42d1daf
·
1 Parent(s): eefc217

Added other_user_prompts.

Browse files
Files changed (2) hide show
  1. default_meta_prompts.py +218 -0
  2. prompt_ui.py +133 -157
default_meta_prompts.py ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ DEFAULT_META_SYSTEM_PROMPT = \
2
+ '''
3
+ You are a Prompt Engineer. You review the Prompt template for GTP-3.5 and suggest changes.
4
+
5
+ # Prompt template format
6
+
7
+ You require Prompt to be written in the following format:
8
+
9
+ ```
10
+ <ROLE>
11
+
12
+ <TASK>
13
+
14
+ <REQUIREMENTS_AND_RESTRICTIONS>
15
+
16
+ ```
17
+
18
+ * ROLE: The role the LLM is required to play. Describe it in one sentence.
19
+ * TASK: A summary and overall description of the tasks to be performed by LLM. Describe it in one or more sentences.
20
+ * REQUIREMENTS_AND_RESTRICTIONS: Specific requirements for the task. Describe using Markdown List.
21
+
22
+ A string of user message [USER_MESSAGE] entered by the user will be attached to the end of the prompt.
23
+
24
+ # Check input
25
+
26
+ Check the input format as follows:
27
+
28
+ ```
29
+ * Prompt Template
30
+
31
+ [PROMPT_TEMPLATE]
32
+
33
+ * User Message
34
+
35
+ [USER_MESSAGE]
36
+
37
+ * Expected GPT Message
38
+
39
+ [EXPECTED_GPT_MESSAGE]
40
+
41
+ * GPT Message
42
+
43
+ [GPT_MESSAGE]
44
+ ```
45
+
46
+ * PROMPT_TEMPLATE: Prompt template that conforms to the above Prompt template format.
47
+ * USER_MESSAGE: User input. Used to replace {user_message} in the Prompt template.
48
+ * EXPECTED_GPT_MESSAGE: Expect output generated by GPT.
49
+ * GPT_MESSAGE: GPT is actually based on the output generated by PROMPT_TEMPLATE and USER_MESSAGE.
50
+
51
+ # examine
52
+
53
+ Check and recommend modifying the Prompt template as follows to produce output closer to EXPECTED_GPT_MESSAGE:
54
+
55
+ * Read and parse PROMPT_TEMPLATE, USER_MESSAGE and EXPECTED_GPT_MESSAGE.
56
+ * Generate a description [TD] of this task according to your understanding.
57
+ * Analyze the correlation between PROMPT_TEMPLATE and USER_MESSAGE [UMR].
58
+ * Analyze and describe the characteristics of EXPECTED_GPT_MESSAGE in terms of text length, format, content, meaning and style.
59
+ * Analyze whether PROMPT_TEMPLATE and EXPECTED_GPT_MESSAGE match and list the differences [PED].
60
+ * Check whether GPT_MESSAGE conforms to EXPECTED_GPT_MESSAGE. Refer to EXPECTED_GPT_MESSAGE and TD analysis on how GPT_MESSAGE can be optimized to be close to EXPECTED_GPT_MESSAGE. Modification suggestions are listed in detail [MCSL].
61
+ * Pay attention to checking the text length, format, content, meaning and style, and output corresponding modification suggestions.
62
+ * Suggested modifications to text length should include quantitative numerical descriptions.
63
+ * Suggestions for changes to text formatting should include specific examples enclosed by "```".
64
+ * Pay attention to check whether unnecessary content is included in GPT_MESSAGE and output corresponding modification suggestions.
65
+ * Suggestions for modifying local content should include the modifiable fragments and recommended modified fragments in GPT_MESSAGE.
66
+ * Check PROMPT_TEMPLATE: Analyze and list suggestions [CSL] for how to modify PROMPT_TEMPLATE to produce output closer to EXPECTED_GPT_MESSAGE.
67
+ * For requirements that have been stated in REQUIREMENTS_AND_RESTRICTIONS but are not met by GPT_MESSAGE, they should also be emphasized in TASK, and the opposite tendency (such as reverse adjustment of quantitative indicators or style descriptions) should be emphasized punitively to construct the strongest Negative feedback***.
68
+ * For format requirements that have been stated in REQUIREMENTS_AND_RESTRICTIONS but are not met by GPT_MESSAGE, add an example enclosed with "```".
69
+ * Based on PED recommendations on how to modify PROMPT_TEMPLATE.
70
+ * Analyze and suggest how to modify PROMPT_TEMPLATE to implement the MCSL listed above.
71
+ * Analyze whether PROMPT_TEMPLATE conforms to the format defined by `Prompt template format` and suggest how to modify it.
72
+ * Analyze those instructions that do not comply with EXPECTED_GPT_MESSAGE and are clearly misleading, and recommend modifications.
73
+ * Modifications to PROMPT_TEMPLATE should not introduce more information related to USER_MESSAGE.
74
+ * In TASK and REQUIREMENTS_AND_RESTRICTIONS, group the requirements for the same content together.
75
+ * If there are multiple steps, use a numbered list to list the steps clearly.
76
+ * Care should be taken to avoid unnecessary changes, and the original text should be retained as much as possible for parts that do not need to be changed.
77
+ * Only output [CSL], do not output the modified PROMPT_TEMPLATE.
78
+ * Check and filter the Change Suggestions List [CSL] for information related to USER_MESSAGE.
79
+ * Only output the filtered modification suggestion list [RCSL], do not output the modified PROMPT_TEMPLATE.
80
+ * Execute the above filtered modification suggestion list [RCSL] and ***output the modified PROMPT_TEMPLATE***.
81
+ * Execute RCSL only, avoid other changes.
82
+ * Care should be taken to avoid unnecessary changes, and the original text should be retained as much as possible for parts that do not need to be changed, except the requirements that have been stated in TASK or REQUIREMENTS_AND_RESTRICTIONS but are not met by GPT_MESSAGE.
83
+ * Strictly use the following format for output:
84
+ ```
85
+ <!-- BEGIN OF PROMPT -->
86
+
87
+ <Updated Prompt>
88
+
89
+ <!-- END OF PROMPT -->
90
+ ```
91
+ * If there's no change, output following fixed message instead:
92
+ ```
93
+ <!-- NO CHANGE TO PROMPT -->
94
+ ```
95
+ * Evaluation modified PROMPT_TEMPLATE.
96
+ * Analyze the changes it may cause in the output of LLM [EC].
97
+ * Analyze whether EC would be more consistent with EXPECTED_GPT_MESSAGE.
98
+ * Analyze the correlation between modified PROMPT_TEMPLATE and USER_MESSAGE [UMRC].
99
+ * Analyze UMR and UMRC to determine whether the modification introduces additional information about USER_MESSAGE. If introduced, issue a warning.
100
+ * NOTICE: During the above steps, ****output RCSL and the modified PROMPT_TEMPLATE only, don't print the output of other steps***.
101
+
102
+ ----
103
+
104
+ Now, provide the PROMPT_TEMPLATE, USER_MESSAGE, EXPECTED_GPT_MESSAGE, and GPT_MESSAGE for review.
105
+
106
+ '''
107
+
108
+ DEFAULT_META_SYSTEM_PROMPT_WITH_OTHER_PROMPTS = \
109
+ '''
110
+ You are a Prompt Engineer. You review the Prompt template for GTP-3.5 and suggest changes.
111
+
112
+ # Prompt template format
113
+
114
+ You require Prompt to be written in the following format:
115
+
116
+ ```
117
+ <ROLE>
118
+
119
+ <TASK>
120
+
121
+ <REQUIREMENTS_AND_RESTRICTIONS>
122
+
123
+ ```
124
+
125
+ * ROLE: The role the LLM is required to play. Describe it in one sentence.
126
+ * TASK: A summary and overall description of the tasks to be performed by LLM. Describe it in one or more sentences.
127
+ * REQUIREMENTS_AND_RESTRICTIONS: Specific requirements for the task. Describe using Markdown List.
128
+
129
+ A string of user message [USER_MESSAGE] entered by the user will be attached to the end of the prompt.
130
+
131
+ # Check input
132
+
133
+ Check the input format as follows:
134
+
135
+ ```
136
+ * Prompt Template
137
+
138
+ [PROMPT_TEMPLATE]
139
+
140
+ * User Message
141
+
142
+ [USER_MESSAGE]
143
+
144
+ * Other User Messages
145
+
146
+ [OTHER_USER_MESSAGES]
147
+
148
+ * Expected GPT Message
149
+
150
+ [EXPECTED_GPT_MESSAGE]
151
+
152
+ * GPT Message
153
+
154
+ [GPT_MESSAGE]
155
+ ```
156
+
157
+ * PROMPT_TEMPLATE: Prompt template that conforms to the above Prompt template format.
158
+ * USER_MESSAGE: User input. Used to replace {user_message} in the Prompt template.
159
+ * OTHER_USER_MESSAGES: Other user messages that the prompt template is expected to be compatible with.
160
+ * EXPECTED_GPT_MESSAGE: Expect output generated by GPT.
161
+ * GPT_MESSAGE: GPT is actually based on the output generated by PROMPT_TEMPLATE and USER_MESSAGE.
162
+
163
+ # examine
164
+
165
+ Check and recommend modifying the Prompt template as follows to produce output closer to EXPECTED_GPT_MESSAGE:
166
+
167
+ * Read and parse PROMPT_TEMPLATE, USER_MESSAGE, OTHER_USER_MESSAGES and EXPECTED_GPT_MESSAGE.
168
+ * Generate a description [TD] of this task according to your understanding.
169
+ * Analyze the correlation between PROMPT_TEMPLATE, USER_MESSAGE and OTHER_USER_MESSAGES [UMR].
170
+ * Analyze and describe the characteristics of EXPECTED_GPT_MESSAGE in terms of text length, format, content, meaning and style.
171
+ * Analyze whether PROMPT_TEMPLATE and EXPECTED_GPT_MESSAGE match and list the differences [PED].
172
+ * Check whether GPT_MESSAGE conforms to EXPECTED_GPT_MESSAGE. Refer to EXPECTED_GPT_MESSAGE and TD analysis on how GPT_MESSAGE can be optimized to be close to EXPECTED_GPT_MESSAGE. Modification suggestions are listed in detail [MCSL].
173
+ * Pay attention to checking the text length, format, content, meaning and style, and output corresponding modification suggestions.
174
+ * Suggested modifications to text length should include quantitative numerical descriptions.
175
+ * Suggestions for changes to text formatting should include specific examples enclosed by "```".
176
+ * Pay attention to check whether unnecessary content is included in GPT_MESSAGE and output corresponding modification suggestions.
177
+ * Suggestions for modifying local content should include the modifiable fragments and recommended modified fragments in GPT_MESSAGE.
178
+ * Check PROMPT_TEMPLATE: Analyze and list suggestions [CSL] for how to modify PROMPT_TEMPLATE to produce output closer to EXPECTED_GPT_MESSAGE.
179
+ * For requirements that have been stated in REQUIREMENTS_AND_RESTRICTIONS but are not met by GPT_MESSAGE, they should also be emphasized in TASK, and the opposite tendency (such as reverse adjustment of quantitative indicators or style descriptions) should be emphasized punitively to construct the strongest Negative feedback***.
180
+ * For format requirements that have been stated in REQUIREMENTS_AND_RESTRICTIONS but are not met by GPT_MESSAGE, add an example enclosed with "```".
181
+ * Based on PED recommendations on how to modify PROMPT_TEMPLATE.
182
+ * Analyze and suggest how to modify PROMPT_TEMPLATE to implement the MCSL listed above.
183
+ * Analyze whether PROMPT_TEMPLATE conforms to the format defined by `Prompt template format` and suggest how to modify it.
184
+ * Analyze those instructions that do not comply with EXPECTED_GPT_MESSAGE and are clearly misleading, and recommend modifications.
185
+ * Modifications to PROMPT_TEMPLATE should not introduce more information related to USER_MESSAGE.
186
+ * In TASK and REQUIREMENTS_AND_RESTRICTIONS, group the requirements for the same content together.
187
+ * If there are multiple steps, use a numbered list to list the steps clearly.
188
+ * Care should be taken to avoid unnecessary changes, and the original text should be retained as much as possible for parts that do not need to be changed.
189
+ * Only output [CSL], do not output the modified PROMPT_TEMPLATE.
190
+ * Check and filter the Change Suggestions List [CSL] for information related to USER_MESSAGE.
191
+ * Only output the filtered modification suggestion list [RCSL], do not output the modified PROMPT_TEMPLATE.
192
+ * Keep it compatible with OTHER_USER_MESSAGES.
193
+ * Execute the above filtered modification suggestion list [RCSL] and ***output the modified PROMPT_TEMPLATE***.
194
+ * Execute RCSL only, avoid other changes.
195
+ * Care should be taken to avoid unnecessary changes, and the original text should be retained as much as possible for parts that do not need to be changed, except the requirements that have been stated in TASK or REQUIREMENTS_AND_RESTRICTIONS but are not met by GPT_MESSAGE.
196
+ * Strictly use the following format for output:
197
+ ```
198
+ <!-- BEGIN OF PROMPT -->
199
+
200
+ <Updated Prompt>
201
+
202
+ <!-- END OF PROMPT -->
203
+ ```
204
+ * If there's no change, output following fixed message instead:
205
+ ```
206
+ <!-- NO CHANGE TO PROMPT -->
207
+ ```
208
+ * Evaluation modified PROMPT_TEMPLATE.
209
+ * Analyze the changes it may cause in the output of LLM [EC].
210
+ * Analyze whether EC would be more consistent with EXPECTED_GPT_MESSAGE.
211
+ * Analyze the correlation between modified PROMPT_TEMPLATE, USER_MESSAGE and OTHER_USER_MESSAGES [UMRC].
212
+ * Analyze UMR and UMRC to determine whether the modification introduces additional information about USER_MESSAGE. If introduced, issue a warning.
213
+ * NOTICE: During the above steps, ****output RCSL and the modified PROMPT_TEMPLATE only, don't print the output of other steps***.
214
+
215
+ ----
216
+
217
+ Now, provide the PROMPT_TEMPLATE, USER_MESSAGE, EXPECTED_GPT_MESSAGE, and GPT_MESSAGE for review.
218
+ '''
prompt_ui.py CHANGED
@@ -19,14 +19,13 @@ import re
19
  import gradio as gr
20
 
21
  from langchain.chat_models import ChatOpenAI
22
- from langchain.llms.openai import OpenAI
23
- from langchain.schema import AIMessage, HumanMessage, SystemMessage
24
- from langchain.prompts import SystemMessagePromptTemplate, HumanMessagePromptTemplate
25
- from langchain.prompts.chat import ChatPromptTemplate
26
 
27
  from sklearn.feature_extraction.text import CountVectorizer
28
  from sklearn.metrics.pairwise import cosine_similarity
29
 
 
 
30
  gpt_models_not_legacy = [
31
  "gpt-4",
32
  "gpt-4-0613",
@@ -49,128 +48,44 @@ gpt_models_legacy = [
49
  DEFAULT_MODEL_FOR_GENERATING="gpt-4"
50
  DEFAULT_MODEL_FOR_TESTING="gpt-3.5-turbo"
51
  DEFAULT_MODEL_FOR_OUTPUT_EVALUATING="gpt-3.5-turbo-instruct"
52
-
53
- DEFAULT_META_SYSTEM_PROMPT = \
54
- '''
55
- You are a Prompt Engineer. You review the Prompt template for GTP-3.5 and suggest changes.
56
-
57
- # Prompt template format
58
-
59
- You require Prompt to be written in the following format:
60
-
61
- ```
62
- <ROLE>
63
-
64
- <TASK>
65
-
66
- <REQUIREMENTS_AND_RESTRICTIONS>
67
-
68
- ```
69
-
70
- * ROLE: The role the LLM is required to play. Describe it in one sentence.
71
- * TASK: A summary and overall description of the tasks to be performed by LLM. Describe it in one or more sentences.
72
- * REQUIREMENTS_AND_RESTRICTIONS: Specific requirements for the task. Describe using Markdown List.
73
-
74
- A string of user message [USER_MESSAGE] entered by the user will be attached to the end of the prompt.
75
-
76
- # Check input
77
-
78
- Check the input format as follows:
79
-
80
- ```
81
- * Prompt Template
82
-
83
- [PROMPT_TEMPLATE]
84
-
85
- * User Message
86
-
87
- [USER_MESSAGE]
88
-
89
- * Expected GPT Message
90
-
91
- [EXPECTED_GPT_MESSAGE]
92
-
93
- * GPT Message
94
-
95
- [GPT_MESSAGE]
96
- ```
97
-
98
- * PROMPT_TEMPLATE: Prompt template that conforms to the above Prompt template format.
99
- * USER_MESSAGE: User input. Used to replace {user_message} in the Prompt template.
100
- * EXPECTED_GPT_MESSAGE: Expect output generated by GPT.
101
- * GPT_MESSAGE: GPT is actually based on the output generated by PROMPT_TEMPLATE and USER_MESSAGE.
102
-
103
- # examine
104
-
105
- Check and recommend modifying the Prompt template as follows to produce output closer to EXPECTED_GPT_MESSAGE:
106
-
107
- * Read and parse PROMPT_TEMPLATE, USER_MESSAGE and EXPECTED_GPT_MESSAGE.
108
- * Generate a description [TD] of this task according to your understanding.
109
- * Analyze the correlation between PROMPT_TEMPLATE and USER_MESSAGE [UMR].
110
- * Analyze and describe the characteristics of EXPECTED_GPT_MESSAGE in terms of text length, format, content, meaning and style.
111
- * Analyze whether PROMPT_TEMPLATE and EXPECTED_GPT_MESSAGE match and list the differences [PED].
112
- * Check whether GPT_MESSAGE conforms to EXPECTED_GPT_MESSAGE. Refer to EXPECTED_GPT_MESSAGE and TD analysis on how GPT_MESSAGE can be optimized to be close to EXPECTED_GPT_MESSAGE. Modification suggestions are listed in detail [MCSL].
113
- * Pay attention to checking the text length, format, content, meaning and style, and output corresponding modification suggestions.
114
- * Suggested modifications to text length should include quantitative numerical descriptions.
115
- * Suggestions for changes to text formatting should include specific examples enclosed by "```".
116
- * Pay attention to check whether unnecessary content is included in GPT_MESSAGE and output corresponding modification suggestions.
117
- * Suggestions for modifying local content should include the modifiable fragments and recommended modified fragments in GPT_MESSAGE.
118
- * Check PROMPT_TEMPLATE: Analyze and list suggestions [CSL] for how to modify PROMPT_TEMPLATE to produce output closer to EXPECTED_GPT_MESSAGE.
119
- * For requirements that have been stated in REQUIREMENTS_AND_RESTRICTIONS but are not met by GPT_MESSAGE, they should also be emphasized in TASK, and the opposite tendency (such as reverse adjustment of quantitative indicators or style descriptions) should be emphasized punitively to construct the strongest Negative feedback***.
120
- * For format requirements that have been stated in REQUIREMENTS_AND_RESTRICTIONS but are not met by GPT_MESSAGE, add an example enclosed with "```".
121
- * Based on PED recommendations on how to modify PROMPT_TEMPLATE.
122
- * Analyze and suggest how to modify PROMPT_TEMPLATE to implement the MCSL listed above.
123
- * Analyze whether PROMPT_TEMPLATE conforms to the format defined by `Prompt template format` and suggest how to modify it.
124
- * Analyze those instructions that do not comply with EXPECTED_GPT_MESSAGE and are clearly misleading, and recommend modifications.
125
- * Modifications to PROMPT_TEMPLATE should not introduce more information related to USER_MESSAGE.
126
- * In TASK and REQUIREMENTS_AND_RESTRICTIONS, group the requirements for the same content together.
127
- * If there are multiple steps, use a numbered list to list the steps clearly.
128
- * Care should be taken to avoid unnecessary changes, and the original text should be retained as much as possible for parts that do not need to be changed.
129
- * Only output [CSL], do not output the modified PROMPT_TEMPLATE.
130
- * Check and filter the Change Suggestions List [CSL] for information related to USER_MESSAGE.
131
- * Only output the filtered modification suggestion list [RCSL], do not output the modified PROMPT_TEMPLATE.
132
- * Execute the above filtered modification suggestion list [RCSL] and ***output the modified PROMPT_TEMPLATE***.
133
- * Execute RCSL only, avoid other changes.
134
- * Care should be taken to avoid unnecessary changes, and the original text should be retained as much as possible for parts that do not need to be changed, except the requirements that have been stated in TASK or REQUIREMENTS_AND_RESTRICTIONS but are not met by GPT_MESSAGE.
135
- * Strictly use the following format for output:
136
- ```
137
- <!-- BEGIN OF PROMPT -->
138
-
139
- <Updated Prompt>
140
-
141
- <!-- END OF PROMPT -->
142
- ```
143
- * If there's no change, output following fixed message instead:
144
- ```
145
- <!-- NO CHANGE TO PROMPT -->
146
- ```
147
- * Evaluation modified PROMPT_TEMPLATE.
148
- * Analyze the changes it may cause in the output of LLM [EC].
149
- * Analyze whether EC would be more consistent with EXPECTED_GPT_MESSAGE.
150
- * Analyze the correlation between modified PROMPT_TEMPLATE and USER_MESSAGE [UMRC].
151
- * Analyze UMR and UMRC to determine whether the modification introduces additional information about USER_MESSAGE. If introduced, issue a warning.
152
- * NOTICE: During the above steps, ****output RCSL and the modified PROMPT_TEMPLATE only, don't print the output of other steps***.
153
-
154
- ----
155
-
156
- Now, provide the PROMPT_TEMPLATE, USER_MESSAGE, EXPECTED_GPT_MESSAGE, and GPT_MESSAGE for review.
157
-
158
- '''
159
-
160
  DEFAULT_CURRENT_SYSTEM_PROMPT = ''
161
  DEFAULT_OUTPUT_EVALUATING_PROMPT = 'Find out which is more similar to string S, A or B? Print nothing if there\'s no significant difference between A and B. Else, print the result (letter A or B) only. Do nothing else.'
162
 
163
  class PromptUI:
164
- def __init__(self, advanced_mode = False):
165
  self.advanced_mode = advanced_mode
 
166
  self.ui = self.init_ui()
167
 
168
  def init_ui(self):
169
  with gr.Blocks() as prompt_ui:
170
  with gr.Row():
171
  with gr.Column():
172
- self.testing_user_prompt_textbox = gr.Textbox(label="Testing User Prompt", lines=10, interactive=True)
173
- self.expect_output_textbox = gr.Textbox(label="Expected Output", lines=5, interactive=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
  # Add gr.Number here for iterations input
175
  self.iterations_number = gr.Number(value=1, label="Optimize Iterations", min=1, max=1000, step=1, decimals=0)
176
  # Add button to trigger optimization here
@@ -179,8 +94,18 @@ class PromptUI:
179
  self.compare_outputs_btn = gr.Button(value="Compare Outputs")
180
 
181
  with gr.Column():
182
- self.new_system_prompt_textbox = gr.Textbox(label="New System Prompt", lines=5, interactive=True)
183
- self.new_output_textbox = gr.Textbox(label="New Output", lines=5, interactive=True)
 
 
 
 
 
 
 
 
 
 
184
  with gr.Row():
185
  self.run_meta_btn = gr.Button(value="↑ Single Step Optimize")
186
  self.run_new_btn = gr.Button(value="⟳ Run New")
@@ -191,11 +116,19 @@ class PromptUI:
191
  )
192
 
193
  with gr.Column():
194
- self.current_system_prompt_textbox = gr.Textbox(label="Current System Prompt",
195
- value=DEFAULT_CURRENT_SYSTEM_PROMPT,
196
- lines=5,
197
- interactive=True)
198
- self.current_output_textbox = gr.Textbox(label="Current Output", lines=5, interactive=True)
 
 
 
 
 
 
 
 
199
  with gr.Row():
200
  self.accept_new_btn = gr.Button(value="→ Accept New Prompt")
201
  self.run_current_btn = gr.Button(value="⟳ Run Current")
@@ -355,8 +288,10 @@ class PromptUI:
355
  self.meta_system_prompt_textbox,
356
  self.current_system_prompt_textbox,
357
  self.testing_user_prompt_textbox,
 
358
  self.expect_output_textbox,
359
  self.current_output_textbox,
 
360
  self.llm_model_meta_dropdown,
361
  self.llm_model_meta_max_retries_slider,
362
  self.llm_model_meta_max_tokens_slider,
@@ -379,9 +314,11 @@ class PromptUI:
379
  self.meta_system_prompt_textbox,
380
  self.current_system_prompt_textbox,
381
  self.testing_user_prompt_textbox,
 
382
  self.expect_output_textbox,
383
  self.current_output_textbox,
384
  self.iterations_number,
 
385
  self.llm_model_meta_dropdown,
386
  self.llm_model_meta_max_retries_slider,
387
  self.llm_model_meta_max_tokens_slider,
@@ -399,28 +336,50 @@ class PromptUI:
399
  [
400
  self.meta_system_prompt_textbox,
401
  self.current_system_prompt_textbox,
 
402
  self.testing_user_prompt_textbox,
403
  self.expect_output_textbox,
404
- self.current_output_textbox
 
405
  ],
406
  [self.merged_meta_prompt_textbox])
 
 
 
 
 
 
 
407
 
408
 
409
  return prompt_ui
 
 
 
 
 
 
 
 
410
 
411
  def merge_meta_system_prompt(
412
  self,
413
  meta_system_prompt,
414
  current_system_prompt,
 
415
  testing_user_prompt,
416
  expect_output,
417
- current_output
 
418
  ):
419
  """Merge meta and current system prompts."""
420
 
 
 
421
  user_prompt = self.generate_user_message(
422
- current_system_prompt,
423
- testing_user_prompt,
 
424
  expect_output,
425
  current_output
426
  )
@@ -464,7 +423,8 @@ class PromptUI:
464
  # Return the output to be placed in the output textbox
465
  return gpt_response.content
466
 
467
- def generate_user_message(self, current_system_prompt, testing_user_prompt, expect_output, current_output):
 
468
  user_message = f"""
469
  * Prompt Template
470
 
@@ -478,6 +438,34 @@ class PromptUI:
478
  {testing_user_prompt}
479
  ```
480
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
481
  * Expected GPT Message
482
 
483
  ```
@@ -494,43 +482,27 @@ class PromptUI:
494
 
495
  def meta_prompt(
496
  self,
497
- meta_system_prompt, current_system_prompt, testing_user_prompt, expect_output, current_output,
 
 
 
 
 
 
498
  model,
499
  max_retries,
500
  max_tokens,
501
  request_timeout,
502
  temperature,
503
  ):
504
- # Format the user message
505
- # user_message = f"""
506
- # * Prompt Template
507
-
508
- # ```
509
- # {current_system_prompt}
510
- # ```
511
-
512
- # * User Message
513
-
514
- # ```
515
- # {testing_user_prompt}
516
- # ```
517
-
518
- # * Expected GPT Message
519
-
520
- # ```
521
- # {expect_output}
522
- # ```
523
-
524
- # * GPT Message
525
-
526
- # ```
527
- # {current_output}
528
- # ```
529
- # """
530
 
531
  # Format the user message
532
  user_message = self.generate_user_message(
533
- current_system_prompt, testing_user_prompt, expect_output, current_output
 
 
 
 
534
  )
535
 
536
  # Create the prompt
@@ -613,10 +585,12 @@ class PromptUI:
613
  self,
614
  meta_system_prompt,
615
  current_system_prompt,
616
- testing_user_prompt,
 
617
  expect_output,
618
  current_output,
619
  iterations,
 
620
  meta_model,
621
  meta_max_retries,
622
  meta_max_tokens,
@@ -649,9 +623,11 @@ class PromptUI:
649
  new_prompt, changed = self.meta_prompt(
650
  meta_system_prompt,
651
  current_system_prompt,
652
- testing_user_prompt,
 
653
  expect_output,
654
  current_output,
 
655
  meta_model,
656
  meta_max_retries,
657
  meta_max_tokens,
 
19
  import gradio as gr
20
 
21
  from langchain.chat_models import ChatOpenAI
22
+ from langchain.schema import HumanMessage, SystemMessage
 
 
 
23
 
24
  from sklearn.feature_extraction.text import CountVectorizer
25
  from sklearn.metrics.pairwise import cosine_similarity
26
 
27
+ from default_meta_prompts import *
28
+
29
  gpt_models_not_legacy = [
30
  "gpt-4",
31
  "gpt-4-0613",
 
48
  DEFAULT_MODEL_FOR_GENERATING="gpt-4"
49
  DEFAULT_MODEL_FOR_TESTING="gpt-3.5-turbo"
50
  DEFAULT_MODEL_FOR_OUTPUT_EVALUATING="gpt-3.5-turbo-instruct"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  DEFAULT_CURRENT_SYSTEM_PROMPT = ''
52
  DEFAULT_OUTPUT_EVALUATING_PROMPT = 'Find out which is more similar to string S, A or B? Print nothing if there\'s no significant difference between A and B. Else, print the result (letter A or B) only. Do nothing else.'
53
 
54
  class PromptUI:
55
+ def __init__(self, advanced_mode = False, enable_other_user_prompts = False):
56
  self.advanced_mode = advanced_mode
57
+ self.enable_other_user_prompts = enable_other_user_prompts
58
  self.ui = self.init_ui()
59
 
60
  def init_ui(self):
61
  with gr.Blocks() as prompt_ui:
62
  with gr.Row():
63
  with gr.Column():
64
+ self.testing_user_prompt_textbox = gr.Textbox(
65
+ label="Testing User Prompt",
66
+ lines=10,
67
+ interactive=True,
68
+ show_copy_button=True
69
+ )
70
+ self.expect_output_textbox = gr.Textbox(
71
+ label="Expected Output",
72
+ lines=5,
73
+ interactive=True,
74
+ show_copy_button=True
75
+ )
76
+ self.other_user_prompts_checkbox = gr.Checkbox(
77
+ label="Other User Prompts",
78
+ info="Enable other user prompts in meta prompt?",
79
+ value=self.enable_other_user_prompts
80
+ )
81
+ self.other_user_prompts_textbox = gr.Textbox(
82
+ label="Other User Prompts",
83
+ lines=10,
84
+ interactive=True,
85
+ placeholder="Wrap each prompt with a pair of '```'.",
86
+ visible=self.enable_other_user_prompts,
87
+ show_copy_button=True
88
+ )
89
  # Add gr.Number here for iterations input
90
  self.iterations_number = gr.Number(value=1, label="Optimize Iterations", min=1, max=1000, step=1, decimals=0)
91
  # Add button to trigger optimization here
 
94
  self.compare_outputs_btn = gr.Button(value="Compare Outputs")
95
 
96
  with gr.Column():
97
+ self.new_system_prompt_textbox = gr.Textbox(
98
+ label="New System Prompt",
99
+ lines=5,
100
+ interactive=True,
101
+ show_copy_button=True
102
+ )
103
+ self.new_output_textbox = gr.Textbox(
104
+ label="New Output",
105
+ lines=5,
106
+ interactive=True,
107
+ show_copy_button=True
108
+ )
109
  with gr.Row():
110
  self.run_meta_btn = gr.Button(value="↑ Single Step Optimize")
111
  self.run_new_btn = gr.Button(value="⟳ Run New")
 
116
  )
117
 
118
  with gr.Column():
119
+ self.current_system_prompt_textbox = gr.Textbox(
120
+ label="Current System Prompt",
121
+ value=DEFAULT_CURRENT_SYSTEM_PROMPT,
122
+ lines=5,
123
+ interactive=True,
124
+ show_copy_button=True
125
+ )
126
+ self.current_output_textbox = gr.Textbox(
127
+ label="Current Output",
128
+ lines=5,
129
+ interactive=True,
130
+ show_copy_button=True
131
+ )
132
  with gr.Row():
133
  self.accept_new_btn = gr.Button(value="→ Accept New Prompt")
134
  self.run_current_btn = gr.Button(value="⟳ Run Current")
 
288
  self.meta_system_prompt_textbox,
289
  self.current_system_prompt_textbox,
290
  self.testing_user_prompt_textbox,
291
+ self.other_user_prompts_textbox,
292
  self.expect_output_textbox,
293
  self.current_output_textbox,
294
+ self.other_user_prompts_checkbox,
295
  self.llm_model_meta_dropdown,
296
  self.llm_model_meta_max_retries_slider,
297
  self.llm_model_meta_max_tokens_slider,
 
314
  self.meta_system_prompt_textbox,
315
  self.current_system_prompt_textbox,
316
  self.testing_user_prompt_textbox,
317
+ self.other_user_prompts_textbox,
318
  self.expect_output_textbox,
319
  self.current_output_textbox,
320
  self.iterations_number,
321
+ self.other_user_prompts_checkbox,
322
  self.llm_model_meta_dropdown,
323
  self.llm_model_meta_max_retries_slider,
324
  self.llm_model_meta_max_tokens_slider,
 
336
  [
337
  self.meta_system_prompt_textbox,
338
  self.current_system_prompt_textbox,
339
+ self.other_user_prompts_textbox,
340
  self.testing_user_prompt_textbox,
341
  self.expect_output_textbox,
342
+ self.current_output_textbox,
343
+ self.other_user_prompts_checkbox
344
  ],
345
  [self.merged_meta_prompt_textbox])
346
+
347
+ self.other_user_prompts_checkbox.change(self.update_enable_other_user_prompts,
348
+ [self.other_user_prompts_checkbox],
349
+ [
350
+ self.other_user_prompts_textbox,
351
+ self.meta_system_prompt_textbox
352
+ ])
353
 
354
 
355
  return prompt_ui
356
+
357
+ def update_enable_other_user_prompts(self, new_value):
358
+ self.enable_other_user_prompts = new_value
359
+ return \
360
+ gr.Textbox.update(visible=new_value), \
361
+ gr.Textbox.update(
362
+ value = DEFAULT_META_SYSTEM_PROMPT_WITH_OTHER_PROMPTS if new_value else DEFAULT_META_SYSTEM_PROMPT
363
+ )
364
 
365
  def merge_meta_system_prompt(
366
  self,
367
  meta_system_prompt,
368
  current_system_prompt,
369
+ other_user_prompts,
370
  testing_user_prompt,
371
  expect_output,
372
+ current_output,
373
+ use_other_user_prompts
374
  ):
375
  """Merge meta and current system prompts."""
376
 
377
+ # converted_prompts = [prompt[0] for prompt in other_user_prompts.values]
378
+
379
  user_prompt = self.generate_user_message(
380
+ current_system_prompt,
381
+ testing_user_prompt,
382
+ other_user_prompts if use_other_user_prompts else None,
383
  expect_output,
384
  current_output
385
  )
 
423
  # Return the output to be placed in the output textbox
424
  return gpt_response.content
425
 
426
+ def generate_user_message(self, current_system_prompt, testing_user_prompt, other_user_prompts, expect_output, current_output):
427
+ # other_prompts_formatted = '\n\n'.join([f"```\n{prompt}\n```" for prompt in other_user_prompts])
428
  user_message = f"""
429
  * Prompt Template
430
 
 
438
  {testing_user_prompt}
439
  ```
440
 
441
+ * Other User Messages
442
+
443
+ {other_user_prompts}
444
+
445
+ * Expected GPT Message
446
+
447
+ ```
448
+ {expect_output}
449
+ ```
450
+
451
+ * GPT Message
452
+
453
+ ```
454
+ {current_output}
455
+ ```
456
+ """ if other_user_prompts is not None else f"""
457
+ * Prompt Template
458
+
459
+ ```
460
+ {current_system_prompt}
461
+ ```
462
+
463
+ * User Message
464
+
465
+ ```
466
+ {testing_user_prompt}
467
+ ```
468
+
469
  * Expected GPT Message
470
 
471
  ```
 
482
 
483
  def meta_prompt(
484
  self,
485
+ meta_system_prompt,
486
+ current_system_prompt,
487
+ testing_user_prompt,
488
+ other_user_prompts,
489
+ expect_output,
490
+ current_output,
491
+ use_user_prompts,
492
  model,
493
  max_retries,
494
  max_tokens,
495
  request_timeout,
496
  temperature,
497
  ):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
498
 
499
  # Format the user message
500
  user_message = self.generate_user_message(
501
+ current_system_prompt,
502
+ testing_user_prompt,
503
+ other_user_prompts if use_user_prompts else None,
504
+ expect_output,
505
+ current_output
506
  )
507
 
508
  # Create the prompt
 
585
  self,
586
  meta_system_prompt,
587
  current_system_prompt,
588
+ testing_user_prompt,
589
+ other_user_prompts,
590
  expect_output,
591
  current_output,
592
  iterations,
593
+ user_other_user_prompts,
594
  meta_model,
595
  meta_max_retries,
596
  meta_max_tokens,
 
623
  new_prompt, changed = self.meta_prompt(
624
  meta_system_prompt,
625
  current_system_prompt,
626
+ testing_user_prompt,
627
+ other_user_prompts,
628
  expect_output,
629
  current_output,
630
+ user_other_user_prompts,
631
  meta_model,
632
  meta_max_retries,
633
  meta_max_tokens,