-
Notifications
You must be signed in to change notification settings - Fork 24
/
prompt_library.py
203 lines (166 loc) · 5.64 KB
/
prompt_library.py
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
import marimo
__generated_with = "0.8.18"
app = marimo.App(width="medium")
@app.cell
def __():
import marimo as mo
from src.marimo_notebook.modules import prompt_library_module, llm_module
import re # For regex to extract placeholders
return llm_module, mo, prompt_library_module, re
@app.cell
def __(prompt_library_module):
map_prompt_library: dict = prompt_library_module.pull_in_prompt_library()
return (map_prompt_library,)
@app.cell
def __(llm_module):
llm_o1_mini, llm_o1_preview = llm_module.build_o1_series()
llm_gpt_4o_latest, llm_gpt_4o_mini = llm_module.build_openai_latest_and_fastest()
# llm_sonnet = llm_module.build_sonnet_3_5()
# gemini_1_5_pro, gemini_1_5_flash = llm_module.build_gemini_duo()
models = {
"o1-mini": llm_o1_mini,
"o1-preview": llm_o1_preview,
"gpt-4o-latest": llm_gpt_4o_latest,
"gpt-4o-mini": llm_gpt_4o_mini,
# "sonnet-3.5": llm_sonnet,
# "gemini-1-5-pro": gemini_1_5_pro,
# "gemini-1-5-flash": gemini_1_5_flash,
}
return (
llm_gpt_4o_latest,
llm_gpt_4o_mini,
llm_o1_mini,
llm_o1_preview,
models,
)
@app.cell
def __():
prompt_styles = {"background": "#eee", "padding": "10px", "border-radius": "10px"}
return (prompt_styles,)
@app.cell
def __(map_prompt_library, mo, models):
prompt_keys = list(map_prompt_library.keys())
prompt_dropdown = mo.ui.dropdown(
options=prompt_keys,
label="Select a Prompt",
)
model_dropdown = mo.ui.dropdown(
options=models,
label="Select an LLM Model",
value="gpt-4o-mini",
)
form = (
mo.md(
r"""
# Prompt Library
{prompt_dropdown}
{model_dropdown}
"""
)
.batch(
prompt_dropdown=prompt_dropdown,
model_dropdown=model_dropdown,
)
.form()
)
form
return form, model_dropdown, prompt_dropdown, prompt_keys
@app.cell
def __(form, map_prompt_library, mo, prompt_styles):
selected_prompt_name = None
selected_prompt = None
mo.stop(not form.value or not len(form.value), "")
selected_prompt_name = form.value["prompt_dropdown"]
selected_prompt = map_prompt_library[selected_prompt_name]
mo.vstack(
[
mo.md("# Selected Prompt"),
mo.accordion(
{
"### Click to show": mo.md(f"```xml\n{selected_prompt}\n```").style(
prompt_styles
)
}
),
]
)
return selected_prompt, selected_prompt_name
@app.cell
def __(mo, re, selected_prompt, selected_prompt_name):
mo.stop(not selected_prompt_name or not selected_prompt, "")
# Extract placeholders from the prompt
placeholders = re.findall(r"\{\{(.*?)\}\}", selected_prompt)
placeholders = list(set(placeholders)) # Remove duplicates
# Create text areas for placeholders, using the placeholder text as the label
placeholder_inputs = [
mo.ui.text_area(label=ph, placeholder=f"Enter {ph}", full_width=True)
for ph in placeholders
]
# Create an array of placeholder inputs
placeholder_array = mo.ui.array(
placeholder_inputs,
label="Fill in the Placeholders",
)
# Create a 'Proceed' button
proceed_button = mo.ui.run_button(label="Prompt")
# Display the placeholders and the 'Proceed' button in a vertical stack
vstack = mo.vstack([mo.md("# Prompt Variables"), placeholder_array, proceed_button])
vstack
return (
placeholder_array,
placeholder_inputs,
placeholders,
proceed_button,
vstack,
)
@app.cell
def __(mo, placeholder_array, placeholders, proceed_button):
mo.stop(not placeholder_array.value or not len(placeholder_array.value), "")
# Check if any values are missing
if any(not value.strip() for value in placeholder_array.value):
mo.stop(True, mo.md("**Please fill in all placeholders.**"))
# Ensure the 'Proceed' button has been pressed
mo.stop(
not proceed_button.value,
mo.md("**Please press the 'Proceed' button to continue.**"),
)
# Map the placeholder names to the values
filled_values = dict(zip(placeholders, placeholder_array.value))
return (filled_values,)
@app.cell
def __(filled_values, selected_prompt):
# Replace placeholders in the prompt
final_prompt = selected_prompt
for key, value in filled_values.items():
final_prompt = final_prompt.replace(f"{{{{{key}}}}}", value)
# Create context_filled_prompt
context_filled_prompt = final_prompt
return context_filled_prompt, final_prompt, key, value
@app.cell
def __(context_filled_prompt, mo, prompt_styles):
mo.vstack(
[
mo.md("# Context Filled Prompt"),
mo.accordion(
{
"### Click to Show Context Filled Prompt": mo.md(
f"```xml\n{context_filled_prompt}\n```"
).style(prompt_styles)
}
),
]
)
return
@app.cell
def __(context_filled_prompt, form, llm_module, mo):
# Get the selected model
model = form.value["model_dropdown"]
# Run the prompt through the model using context_filled_prompt
with mo.status.spinner(title="Running prompt..."):
prompt_response = llm_module.prompt(model, context_filled_prompt)
mo.md(f"# Prompt Output\n\n{prompt_response}").style(
{"background": "#eee", "padding": "10px", "border-radius": "10px"}
)
return model, prompt_response
if __name__ == "__main__":
app.run()