yaleh commited on
Commit
44ede51
·
1 Parent(s): 1084b7d

Added `Evaluate` buttons.

Browse files
Files changed (1) hide show
  1. app/gradio_meta_prompt.py +114 -33
app/gradio_meta_prompt.py CHANGED
@@ -11,6 +11,7 @@ from gradio_client import utils as client_utils
11
  from confz import BaseConfig, CLArgSource, EnvSource, FileSource
12
  from app.config import MetaPromptConfig
13
  from langchain_core.language_models import BaseLanguageModel
 
14
  from langchain_openai import ChatOpenAI
15
  from meta_prompt import *
16
  from pythonjsonlogger import jsonlogger
@@ -102,6 +103,59 @@ def chat_log_2_chatbot_list(chat_log: str):
102
  return chatbot_list
103
 
104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  def process_message(user_message, expected_output, acceptance_criteria,
106
  initial_system_message, recursion_limit: int,
107
  max_output_age: int,
@@ -229,6 +283,8 @@ with gr.Blocks(title='Meta Prompt') as demo:
229
  label="Acceptance Criteria", show_copy_button=True)
230
  initial_system_message_input = gr.Textbox(
231
  label="Initial System Message", show_copy_button=True, value="")
 
 
232
  recursion_limit_input = gr.Number(
233
  label="Recursion Limit", value=config.recursion_limit,
234
  precision=0, minimum=1, maximum=config.recursion_limit_max, step=1)
@@ -236,41 +292,45 @@ with gr.Blocks(title='Meta Prompt') as demo:
236
  label="Max Output Age", value=config.max_output_age,
237
  precision=0, minimum=1, maximum=config.max_output_age_max, step=1)
238
  with gr.Row():
239
- with gr.Tab('Simple'):
240
- model_name_input = gr.Dropdown(
241
- label="Model Name",
242
- choices=config.llms.keys(),
243
- value=list(config.llms.keys())[0],
244
- )
245
- # Connect the inputs and outputs to the function
246
- with gr.Row():
247
- submit_button = gr.Button(
248
- value="Submit", variant="primary")
249
- clear_button = gr.ClearButton(
250
- [user_message_input, expected_output_input,
251
- acceptance_criteria_input, initial_system_message_input],
252
- value='Clear All')
253
- with gr.Tab('Advanced'):
254
- optimizer_model_name_input = gr.Dropdown(
255
- label="Optimizer Model Name",
256
- choices=config.llms.keys(),
257
- value=list(config.llms.keys())[0],
258
- )
259
- executor_model_name_input = gr.Dropdown(
260
- label="Executor Model Name",
261
- choices=config.llms.keys(),
262
- value=list(config.llms.keys())[0],
263
- )
264
- # Connect the inputs and outputs to the function
265
- with gr.Row():
266
- multiple_submit_button = gr.Button(
267
- value="Submit", variant="primary")
268
- multiple_clear_button = gr.ClearButton(
269
- components=[user_message_input, expected_output_input,
270
- acceptance_criteria_input, initial_system_message_input],
271
- value='Clear All')
 
272
  with gr.Column():
273
  system_message_output = gr.Textbox(label="System Message", show_copy_button=True)
 
 
 
274
  output_output = gr.Textbox(label="Output", show_copy_button=True)
275
  analysis_output = gr.Textbox(label="Analysis", show_copy_button=True)
276
  flag_button = gr.Button(value="Flag", variant="secondary", visible=config.allow_flagging)
@@ -292,6 +352,27 @@ with gr.Blocks(title='Meta Prompt') as demo:
292
  ])
293
 
294
  # set up event handlers
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
295
  clear_button.add([system_message_output, output_output,
296
  analysis_output, logs_chatbot])
297
  multiple_clear_button.add([system_message_output, output_output,
 
11
  from confz import BaseConfig, CLArgSource, EnvSource, FileSource
12
  from app.config import MetaPromptConfig
13
  from langchain_core.language_models import BaseLanguageModel
14
+ from langchain_core.prompts import ChatPromptTemplate
15
  from langchain_openai import ChatOpenAI
16
  from meta_prompt import *
17
  from pythonjsonlogger import jsonlogger
 
103
  return chatbot_list
104
 
105
 
106
+ active_model_tab = "Simple"
107
+
108
+ def on_model_tab_select(event: gr.SelectData):
109
+ if not event.selected:
110
+ return
111
+
112
+ global active_model_tab
113
+ active_model_tab = event.value
114
+
115
+
116
+ def get_current_models(simple_model_name: str, optimizer_model_name: str, executor_model_name: str):
117
+ optimizer_model_config = config.llms[optimizer_model_name if active_model_tab ==
118
+ "Advanced" else simple_model_name]
119
+ executor_model_config = config.llms[executor_model_name if active_model_tab ==
120
+ "Advanced" else simple_model_name]
121
+ optimizer_model = LLMModelFactory().create(optimizer_model_config.type,
122
+ **optimizer_model_config.model_dump(exclude={'type'}))
123
+ executor_model = LLMModelFactory().create(executor_model_config.type,
124
+ **executor_model_config.model_dump(exclude={'type'}))
125
+
126
+ return {
127
+ NODE_PROMPT_INITIAL_DEVELOPER: optimizer_model,
128
+ NODE_PROMPT_DEVELOPER: optimizer_model,
129
+ NODE_PROMPT_EXECUTOR: executor_model,
130
+ NODE_OUTPUT_HISTORY_ANALYZER: optimizer_model,
131
+ NODE_PROMPT_ANALYZER: optimizer_model,
132
+ NODE_PROMPT_SUGGESTER: optimizer_model
133
+ }
134
+
135
+
136
+ def get_current_executor_model(simple_model_name: str, executor_model_name: str):
137
+ executor_model_config = config.llms[executor_model_name if active_model_tab ==
138
+ "Advanced" else simple_model_name]
139
+ executor_model = LLMModelFactory().create(executor_model_config.type,
140
+ **executor_model_config.model_dump(exclude={'type'}))
141
+ return executor_model
142
+
143
+
144
+ def evaluate_system_message(system_message, user_message, simple_model, executor_model):
145
+ llm = get_current_executor_model(simple_model, executor_model)
146
+ template = ChatPromptTemplate.from_messages([
147
+ ("system", "{system_message}"),
148
+ ("human", "{user_message}")
149
+ ])
150
+ messages = template.format_messages(system_message=system_message, user_message=user_message)
151
+ output = llm.invoke(messages)
152
+
153
+ if hasattr(output, 'content'):
154
+ return output.content
155
+ else:
156
+ return ""
157
+
158
+
159
  def process_message(user_message, expected_output, acceptance_criteria,
160
  initial_system_message, recursion_limit: int,
161
  max_output_age: int,
 
283
  label="Acceptance Criteria", show_copy_button=True)
284
  initial_system_message_input = gr.Textbox(
285
  label="Initial System Message", show_copy_button=True, value="")
286
+ evaluate_initial_system_message_button = gr.Button(
287
+ value="Evaluate", variant="secondary")
288
  recursion_limit_input = gr.Number(
289
  label="Recursion Limit", value=config.recursion_limit,
290
  precision=0, minimum=1, maximum=config.recursion_limit_max, step=1)
 
292
  label="Max Output Age", value=config.max_output_age,
293
  precision=0, minimum=1, maximum=config.max_output_age_max, step=1)
294
  with gr.Row():
295
+ with gr.Tabs():
296
+ with gr.Tab('Simple') as simple_llm_tab:
297
+ model_name_input = gr.Dropdown(
298
+ label="Model Name",
299
+ choices=config.llms.keys(),
300
+ value=list(config.llms.keys())[0],
301
+ )
302
+ # Connect the inputs and outputs to the function
303
+ with gr.Row():
304
+ submit_button = gr.Button(
305
+ value="Submit", variant="primary")
306
+ clear_button = gr.ClearButton(
307
+ [user_message_input, expected_output_input,
308
+ acceptance_criteria_input, initial_system_message_input],
309
+ value='Clear All')
310
+ with gr.Tab('Advanced') as advanced_llm_tab:
311
+ optimizer_model_name_input = gr.Dropdown(
312
+ label="Optimizer Model Name",
313
+ choices=config.llms.keys(),
314
+ value=list(config.llms.keys())[0],
315
+ )
316
+ executor_model_name_input = gr.Dropdown(
317
+ label="Executor Model Name",
318
+ choices=config.llms.keys(),
319
+ value=list(config.llms.keys())[0],
320
+ )
321
+ # Connect the inputs and outputs to the function
322
+ with gr.Row():
323
+ multiple_submit_button = gr.Button(
324
+ value="Submit", variant="primary")
325
+ multiple_clear_button = gr.ClearButton(
326
+ components=[user_message_input, expected_output_input,
327
+ acceptance_criteria_input, initial_system_message_input],
328
+ value='Clear All')
329
  with gr.Column():
330
  system_message_output = gr.Textbox(label="System Message", show_copy_button=True)
331
+ with gr.Row():
332
+ evaluate_system_message_button = gr.Button(value="Evaluate", variant="secondary")
333
+ copy_to_initial_system_message_button = gr.Button(value="Copy to Initial System Message", variant="secondary")
334
  output_output = gr.Textbox(label="Output", show_copy_button=True)
335
  analysis_output = gr.Textbox(label="Analysis", show_copy_button=True)
336
  flag_button = gr.Button(value="Flag", variant="secondary", visible=config.allow_flagging)
 
352
  ])
353
 
354
  # set up event handlers
355
+ simple_llm_tab.select(on_model_tab_select)
356
+ advanced_llm_tab.select(on_model_tab_select)
357
+
358
+ evaluate_initial_system_message_button.click(
359
+ evaluate_system_message,
360
+ inputs=[initial_system_message_input, user_message_input,
361
+ model_name_input, executor_model_name_input],
362
+ outputs=[output_output]
363
+ )
364
+ evaluate_system_message_button.click(
365
+ evaluate_system_message,
366
+ inputs=[system_message_output, user_message_input,
367
+ model_name_input, executor_model_name_input],
368
+ outputs=[output_output]
369
+ )
370
+ copy_to_initial_system_message_button.click(
371
+ lambda x: x,
372
+ inputs=[system_message_output],
373
+ outputs=[initial_system_message_input]
374
+ )
375
+
376
  clear_button.add([system_message_output, output_output,
377
  analysis_output, logs_chatbot])
378
  multiple_clear_button.add([system_message_output, output_output,