File size: 6,063 Bytes
3d1636d
 
f3d45a9
 
 
3d1636d
f3d45a9
 
 
3d1636d
 
 
 
 
 
 
 
 
 
 
 
f3d45a9
 
 
 
 
 
3d1636d
f3d45a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d1636d
f3d45a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d1636d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3d45a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d1636d
f3d45a9
 
 
 
 
3d1636d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3d45a9
3d1636d
 
f3d45a9
 
 
 
3d1636d
 
f3d45a9
3d1636d
f3d45a9
 
 
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
from typing import Tuple

import google.generativeai as genai


with open("prompt/base_system_instructions.txt") as f:
  SYSTEM_INSTRUCTION = f.read()


with open("prompt/base_examples.txt") as f:
  DEFAULT_EXAMPLES = f.read()


with open("prompt/chat_elements_examples.txt") as f:
  CHAT_ELEMENTS_EXAMPLES = f.read()


with open("prompt/chat_examples.txt") as f:
  CHAT_EXAMPLES = f.read()


GENERATE_APP_BASE_PROMPT = """
Your task is to write a Mesop app.

Instructions:
1. For the @me.page decorator, leave it empty like this `@me.page()`
2. Event handler functions cannot use lambdas. You must use functions.
3. Event handler functions only pass in the event type. They do not accept extra parameters.
4. For padding, make sure to use the the `me.Padding` object rather than a string or int.
5. For margin, make sure to use the the `me.Margin` object rather than a string or int.
6. For border, make sure to use the the `me.Border` and `me.BorderSide` objects rather than a string.
7. For buttons, prefer using type="flat", especially if it is the primary button.
8. Only output the python code.

Here is a description of the app I want you to write:

<APP_DESCRIPTION>

""".strip()

REVISE_APP_BASE_PROMPT = """
Your task is to modify a Mesop app given the code and a description.

Make sure to remember these rules when making modifications:
1. For the @me.page decorator, leave it empty like this `@me.page()`
2. Event handler functions cannot use lambdas. You must use functions.
3. Event handler functions only pass in the event type. They do not accept extra parameters.
4. For padding, make sure to use the the `me.Padding` object rather than a string or int.
5. For margin, make sure to use the the `me.Margin` object rather than a string or int.
6. For border, make sure to use the the `me.Border` and `me.BorderSide` objects rather than a string.
7. For buttons, prefer using type="flat", especially if it is the primary button.
8. Only output the python code.

Here is is the code for the app:

```
<APP_CODE>
```

Here is a description of the changes I want:

<APP_CHANGES>

""".strip()


GENERATE_CHAT_APP_BASE_PROMPT = """
Your task is to write a Mesop chat app.

Instructions:
1. For the @me.page decorator, leave it empty like this `@me.page()`
2. Event handler functions cannot use lambdas. You must use functions.
3. Event handler functions only pass in the event type. They do not accept extra parameters.
4. For padding, make sure to use the the `me.Padding` object rather than a string or int.
5. For margin, make sure to use the the `me.Margin` object rather than a string or int.
6. For border, make sure to use the the `me.Border` and `me.BorderSide` objects rather than a string.
7. For buttons, prefer using type="flat", especially if it is the primary button.
8. Remember that me.box is a like a div. So you can use similar CSS styles for layout.
9. Only output the python code.

Here is a description of the chat app I want you to write:

<APP_DESCRIPTION>

""".strip()

REVISE_CHAT_APP_BASE_PROMPT = """
Your task is to modify a Mesop chat app given the code and a description.

Make sure to remember these rules when making modifications:
1. For the @me.page decorator, leave it empty like this `@me.page()`
2. Event handler functions cannot use lambdas. You must use functions.
3. Event handler functions only pass in the event type. They do not accept extra parameters.
4. For padding, make sure to use the the `me.Padding` object rather than a string or int.
5. For margin, make sure to use the the `me.Margin` object rather than a string or int.
6. For border, make sure to use the the `me.Border` and `me.BorderSide` objects rather than a string.
7. For buttons, prefer using type="flat", especially if it is the primary button.
8. Remember that me.box is a like a div. So you can use similar CSS styles for layout.
9. Only output the python code.

Here is is the code for the chat app:

```
<APP_CODE>
```

Here is a description of the changes I want:

<APP_CHANGES>

""".strip()


def make_model(
  api_key: str, model_name: str, additional_instructions: str
) -> genai.GenerativeModel:
  genai.configure(api_key=api_key)

  generation_config = {
    "temperature": 1,
    "top_p": 0.95,
    "top_k": 64,
    "max_output_tokens": 32768,
  }

  safety_settings = [
    {
      "category": "HARM_CATEGORY_HARASSMENT",
      "threshold": "BLOCK_NONE",
    },
    {
      "category": "HARM_CATEGORY_HATE_SPEECH",
      "threshold": "BLOCK_NONE",
    },
    {
      "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
      "threshold": "BLOCK_NONE",
    },
    {
      "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
      "threshold": "BLOCK_NONE",
    },
  ]

  return genai.GenerativeModel(
    model_name=model_name,
    system_instruction=SYSTEM_INSTRUCTION + additional_instructions,
    safety_settings=safety_settings,
    generation_config=generation_config,
  )


def get_prompt_examples(app_type: str) -> str:
  if app_type == "chat":
    return CHAT_ELEMENTS_EXAMPLES + CHAT_EXAMPLES
  return DEFAULT_EXAMPLES


def get_generate_prompt_base(app_type: str) -> str:
  if app_type == "chat":
    return GENERATE_CHAT_APP_BASE_PROMPT
  return GENERATE_APP_BASE_PROMPT


def get_revise_prompt_base(app_type: str) -> str:
  if app_type == "chat":
    return REVISE_CHAT_APP_BASE_PROMPT
  return REVISE_APP_BASE_PROMPT


def generate_mesop_app(msg: str, model_name: str, api_key: str, app_type: str) -> str:
  model = make_model(api_key, model_name, get_prompt_examples(app_type))
  response = model.generate_content(
    get_generate_prompt_base(app_type).replace("<APP_DESCRIPTION>", msg),
    request_options={"timeout": 120},
  )
  return response.text


def adjust_mesop_app(code: str, msg: str, model_name: str, api_key: str, app_type: str) -> str:
  model = make_model(api_key, model_name, get_prompt_examples(app_type))
  response = model.generate_content(
    get_revise_prompt_base(app_type).replace("<APP_CODE>", code).replace("<APP_CHANGES>", msg),
    request_options={"timeout": 120},
  )
  return response.text