File size: 2,679 Bytes
b470e8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ff3d12
b470e8d
 
2b894e4
 
 
 
b470e8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ff3d12
b470e8d
 
 
 
6ff3d12
b470e8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import copy

import mesop as me

import components as mex
import handlers
import llm
from constants import DIALOG_INPUT_WIDTH
from helpers import parse_variables, find_prompt
from state import State


@me.component
def add_row():
  state = me.state(State)

  with mex.dialog(state.dialog_show_add_row):
    me.text("Add row", type="headline-6")
    if not state.prompt_variables:
      me.text("No variables defined in prompt.", style=me.Style(width=DIALOG_INPUT_WIDTH))
    else:
      with me.box(
        style=me.Style(display="flex", justify_content="end", margin=me.Margin(bottom=15))
      ):
        mex.button(
          "Generate",
          on_click=on_click_generate_variables,
          style=me.Style(
            background=me.theme_var("secondary-container"),
            color=me.theme_var("on-secondary-container"),
          ),
        )
      variable_names = set(parse_variables(state.prompt))
      with me.box(style=me.Style(display="flex", flex_direction="column")):
        for name in state.prompt_variables.keys():
          if name not in variable_names:
            continue
          me.textarea(
            label=name,
            value=state.add_row_prompt_variables.get(name, ""),
            on_blur=on_input_variable,
            style=me.Style(width=DIALOG_INPUT_WIDTH),
            key=name,
          )

    with mex.dialog_actions():
      mex.button(
        "Close",
        on_click=on_close_dialog,
        key="dialog_show_add_row",
      )
      mex.button("Add", type="flat", on_click=on_click_add_row)


def on_close_dialog(e: me.ClickEvent):
  state = me.state(State)
  state.add_row_prompt_variables = {}
  handlers.on_close_dialog(e)


def on_click_add_row(e: me.ClickEvent):
  state = me.state(State)
  prompt = find_prompt(state.prompts, state.version)
  prompt.responses.append(
    {
      "variables": copy.copy(state.add_row_prompt_variables),
      "output": "",
      "rating": 0,
    }
  )
  state.add_row_prompt_variables = {}
  state.dialog_show_add_row = False


def on_input_variable(e: me.InputBlurEvent):
  """Generic event to save input variables."""
  state = me.state(State)
  state.add_row_prompt_variables[e.key] = e.value


def on_click_generate_variables(e: me.ClickEvent):
  """Generates values for the given empty variables."""
  state = me.state(State)
  variable_names = set(parse_variables(state.prompt))
  generated_variables = llm.generate_variables(
    state.prompt, variable_names, state.model, state.model_temperature
  )

  for name in state.prompt_variables:
    if name in variable_names and name in generated_variables:
      state.add_row_prompt_variables[name] = generated_variables[name]