Spaces:
Running
Running
Upload 8 files
Browse files- app.py +219 -48
- custom_css.py +322 -117
- prompt_refiner.py +54 -62
app.py
CHANGED
@@ -6,6 +6,10 @@ from custom_css import custom_css
|
|
6 |
class GradioInterface:
|
7 |
def __init__(self, prompt_refiner: PromptRefiner, custom_css):
|
8 |
self.prompt_refiner = prompt_refiner
|
|
|
|
|
|
|
|
|
9 |
with gr.Blocks(css=custom_css, theme=gr.themes.Default()) as self.interface:
|
10 |
# CONTAINER 1
|
11 |
with gr.Column(elem_classes=["container", "title-container"]):
|
@@ -15,13 +19,16 @@ class GradioInterface:
|
|
15 |
|
16 |
# CONTAINER 2
|
17 |
with gr.Column(elem_classes=["container", "input-container"]):
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
22 |
|
23 |
# CONTAINER 3
|
24 |
-
# with gr.Column(elem_classes=["container"]):
|
25 |
with gr.Column(elem_classes=["container","meta-container"]):
|
26 |
meta_prompt_choice = gr.Radio(
|
27 |
choices=metaprompt_list,
|
@@ -29,17 +36,16 @@ class GradioInterface:
|
|
29 |
value=metaprompt_list[0],
|
30 |
elem_classes=["no-background", "radio-group"]
|
31 |
)
|
32 |
-
refine_button = gr.Button(
|
|
|
|
|
|
|
33 |
with gr.Accordion("Metaprompt Explanation", open=False, visible=True):
|
34 |
-
gr.Markdown(explanation_markdown)
|
35 |
-
gr.Examples(
|
36 |
-
examples=examples,
|
37 |
-
inputs=[prompt_text, meta_prompt_choice]
|
38 |
-
)
|
39 |
|
40 |
with gr.Column(elem_classes=["container", "analysis-container"]):
|
41 |
gr.Markdown(" ")
|
42 |
-
prompt_evaluation = gr.Markdown()
|
43 |
gr.Markdown("### Refined Prompt")
|
44 |
refined_prompt = gr.Textbox(
|
45 |
label=" ",
|
@@ -47,58 +53,192 @@ class GradioInterface:
|
|
47 |
show_label=True,
|
48 |
show_copy_button=True,
|
49 |
)
|
50 |
-
#gr.Markdown("### Explanation of Refinements")
|
51 |
explanation_of_refinements = gr.Markdown()
|
52 |
|
53 |
-
|
54 |
with gr.Column(elem_classes=["container", "model-container"]):
|
55 |
with gr.Row():
|
56 |
apply_model = gr.Dropdown(
|
57 |
choices=models,
|
58 |
-
value=
|
59 |
label="Choose the Model",
|
60 |
container=False,
|
61 |
scale=1,
|
62 |
min_width=300
|
63 |
)
|
64 |
-
apply_button = gr.Button(
|
|
|
|
|
|
|
65 |
|
66 |
gr.Markdown("### Prompts on Chosen Model")
|
67 |
-
with gr.Tabs():
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
# Button click handlers
|
77 |
automatic_metaprompt_button.click(
|
78 |
fn=self.automatic_metaprompt,
|
79 |
inputs=[prompt_text],
|
80 |
outputs=[MetaPrompt_analysis, meta_prompt_choice]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
)
|
82 |
|
83 |
refine_button.click(
|
84 |
fn=self.refine_prompt,
|
85 |
inputs=[prompt_text, meta_prompt_choice],
|
86 |
-
outputs=[
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
)
|
93 |
|
94 |
apply_button.click(
|
95 |
fn=self.apply_prompts,
|
96 |
inputs=[prompt_text, refined_prompt, apply_model],
|
97 |
-
outputs=[original_output, refined_output]
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
)
|
103 |
|
104 |
def automatic_metaprompt(self, prompt: str) -> tuple:
|
@@ -118,12 +258,7 @@ class GradioInterface:
|
|
118 |
"""Handle manual prompt refinement"""
|
119 |
try:
|
120 |
if not prompt.strip():
|
121 |
-
return (
|
122 |
-
"No prompt provided.",
|
123 |
-
"",
|
124 |
-
"",
|
125 |
-
{}
|
126 |
-
)
|
127 |
|
128 |
result = self.prompt_refiner.refine_prompt(prompt, meta_prompt_choice)
|
129 |
return (
|
@@ -139,16 +274,52 @@ class GradioInterface:
|
|
139 |
def apply_prompts(self, original_prompt: str, refined_prompt: str, model: str) -> tuple:
|
140 |
"""Apply both original and refined prompts to the selected model"""
|
141 |
try:
|
142 |
-
if not original_prompt
|
143 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
|
145 |
-
|
146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
|
148 |
-
return original_output, refined_output
|
149 |
except Exception as e:
|
150 |
-
error_message = f"Error
|
151 |
-
return error_message, error_message
|
152 |
|
153 |
def launch(self, share=False):
|
154 |
"""Launch the Gradio interface"""
|
|
|
6 |
class GradioInterface:
|
7 |
def __init__(self, prompt_refiner: PromptRefiner, custom_css):
|
8 |
self.prompt_refiner = prompt_refiner
|
9 |
+
# Set default model to second-to-last in the list
|
10 |
+
default_model = models[-1] if len(models) >= 1 else models[0] if models else None
|
11 |
+
#meta_prompt_choice=metaprompt_list[0]
|
12 |
+
|
13 |
with gr.Blocks(css=custom_css, theme=gr.themes.Default()) as self.interface:
|
14 |
# CONTAINER 1
|
15 |
with gr.Column(elem_classes=["container", "title-container"]):
|
|
|
19 |
|
20 |
# CONTAINER 2
|
21 |
with gr.Column(elem_classes=["container", "input-container"]):
|
22 |
+
prompt_text = gr.Textbox(label="Type your prompt (or leave empty to see metaprompt)",lines=5)
|
23 |
+
with gr.Accordion("Prompt Examples", open=False, visible=True):
|
24 |
+
gr.Examples(examples=examples,inputs=[prompt_text])
|
25 |
+
automatic_metaprompt_button = gr.Button(
|
26 |
+
"Automatic Choice for Refinement Method",
|
27 |
+
elem_classes=["button-highlight"]
|
28 |
+
)
|
29 |
+
MetaPrompt_analysis = gr.Markdown()
|
30 |
|
31 |
# CONTAINER 3
|
|
|
32 |
with gr.Column(elem_classes=["container","meta-container"]):
|
33 |
meta_prompt_choice = gr.Radio(
|
34 |
choices=metaprompt_list,
|
|
|
36 |
value=metaprompt_list[0],
|
37 |
elem_classes=["no-background", "radio-group"]
|
38 |
)
|
39 |
+
refine_button = gr.Button(
|
40 |
+
"Refine Prompt",
|
41 |
+
elem_classes=["button-waiting"]
|
42 |
+
)
|
43 |
with gr.Accordion("Metaprompt Explanation", open=False, visible=True):
|
44 |
+
gr.Markdown(explanation_markdown)
|
|
|
|
|
|
|
|
|
45 |
|
46 |
with gr.Column(elem_classes=["container", "analysis-container"]):
|
47 |
gr.Markdown(" ")
|
48 |
+
prompt_evaluation = gr.Markdown()
|
49 |
gr.Markdown("### Refined Prompt")
|
50 |
refined_prompt = gr.Textbox(
|
51 |
label=" ",
|
|
|
53 |
show_label=True,
|
54 |
show_copy_button=True,
|
55 |
)
|
|
|
56 |
explanation_of_refinements = gr.Markdown()
|
57 |
|
|
|
58 |
with gr.Column(elem_classes=["container", "model-container"]):
|
59 |
with gr.Row():
|
60 |
apply_model = gr.Dropdown(
|
61 |
choices=models,
|
62 |
+
value=default_model,
|
63 |
label="Choose the Model",
|
64 |
container=False,
|
65 |
scale=1,
|
66 |
min_width=300
|
67 |
)
|
68 |
+
apply_button = gr.Button(
|
69 |
+
"Apply Prompts",
|
70 |
+
elem_classes=["button-waiting"]
|
71 |
+
)
|
72 |
|
73 |
gr.Markdown("### Prompts on Chosen Model")
|
74 |
+
with gr.Tabs(elem_classes=["tabs"]):
|
75 |
+
with gr.TabItem("Prompts Output Comparison", elem_classes=["tabitem"]):
|
76 |
+
with gr.Row(elem_classes=["output-row"]):
|
77 |
+
with gr.Column(scale=1, elem_classes=["comparison-column"]):
|
78 |
+
gr.Markdown("### Original Prompt Output")
|
79 |
+
original_output1 = gr.Markdown(
|
80 |
+
# value="Output will appear here",
|
81 |
+
elem_classes=["output-content"],
|
82 |
+
visible=True
|
83 |
+
)
|
84 |
+
with gr.Column(scale=1, elem_classes=["comparison-column"]):
|
85 |
+
gr.Markdown("### Refined Prompt Output")
|
86 |
+
refined_output1 = gr.Markdown(
|
87 |
+
# value="Output will appear here",
|
88 |
+
elem_classes=["output-content"],
|
89 |
+
visible=True
|
90 |
+
)
|
91 |
+
with gr.TabItem("Original Prompt Output", elem_classes=["tabitem"]):
|
92 |
+
with gr.Row(elem_classes=["output-row"]):
|
93 |
+
with gr.Column(scale=1, elem_classes=["comparison-column"]):
|
94 |
+
gr.Markdown("### Original Prompt Output")
|
95 |
+
original_output = gr.Markdown(
|
96 |
+
# value="Output will appear here",
|
97 |
+
elem_classes=[ "output-content"],
|
98 |
+
visible=True
|
99 |
+
)
|
100 |
+
with gr.TabItem("Refined Prompt Output", elem_classes=["tabitem"]):
|
101 |
+
with gr.Row(elem_classes=["output-row"]):
|
102 |
+
with gr.Column(scale=1, elem_classes=["comparison-column"]):
|
103 |
+
gr.Markdown("### Refined Prompt Output")
|
104 |
+
refined_output = gr.Markdown(
|
105 |
+
# value="Output will appear here",
|
106 |
+
elem_classes=["output-content"],
|
107 |
+
visible=True
|
108 |
+
)
|
109 |
+
|
110 |
+
with gr.Accordion("Full Response JSON", open=False, visible=True):
|
111 |
+
full_response_json = gr.JSON()
|
112 |
+
|
113 |
# Button click handlers
|
114 |
automatic_metaprompt_button.click(
|
115 |
fn=self.automatic_metaprompt,
|
116 |
inputs=[prompt_text],
|
117 |
outputs=[MetaPrompt_analysis, meta_prompt_choice]
|
118 |
+
).then(
|
119 |
+
fn=lambda: None,
|
120 |
+
inputs=None,
|
121 |
+
outputs=None,
|
122 |
+
js="""
|
123 |
+
() => {
|
124 |
+
// Clear subsequent outputs
|
125 |
+
document.querySelectorAll('.analysis-container textarea, .analysis-container .markdown-text, .model-container .markdown-text, .comparison-output').forEach(el => {
|
126 |
+
if (el.value !== undefined) {
|
127 |
+
el.value = '';
|
128 |
+
} else {
|
129 |
+
el.textContent = '';
|
130 |
+
}
|
131 |
+
});
|
132 |
+
|
133 |
+
// Update button states
|
134 |
+
const allButtons = Array.from(document.querySelectorAll('button')).filter(btn =>
|
135 |
+
btn.textContent.includes('Automatic Choice') ||
|
136 |
+
btn.textContent.includes('Refine Prompt') ||
|
137 |
+
btn.textContent.includes('Apply Prompts')
|
138 |
+
);
|
139 |
+
allButtons.forEach(btn => btn.classList.remove('button-highlight'));
|
140 |
+
allButtons[1].classList.add('button-highlight'); // Highlight refine button
|
141 |
+
allButtons[0].classList.add('button-completed'); // Complete current button
|
142 |
+
allButtons[2].classList.add('button-waiting'); // Set apply button to waiting
|
143 |
+
}
|
144 |
+
"""
|
145 |
)
|
146 |
|
147 |
refine_button.click(
|
148 |
fn=self.refine_prompt,
|
149 |
inputs=[prompt_text, meta_prompt_choice],
|
150 |
+
outputs=[prompt_evaluation, refined_prompt, explanation_of_refinements, full_response_json]
|
151 |
+
).then(
|
152 |
+
fn=lambda: None,
|
153 |
+
inputs=None,
|
154 |
+
outputs=None,
|
155 |
+
js="""
|
156 |
+
() => {
|
157 |
+
// Clear model outputs
|
158 |
+
document.querySelectorAll('.model-container .markdown-text, .comparison-output').forEach(el => {
|
159 |
+
if (el.value !== undefined) {
|
160 |
+
el.value = '';
|
161 |
+
} else {
|
162 |
+
el.textContent = '';
|
163 |
+
}
|
164 |
+
});
|
165 |
+
|
166 |
+
// Update button states
|
167 |
+
const allButtons = Array.from(document.querySelectorAll('button')).filter(btn =>
|
168 |
+
btn.textContent.includes('Automatic Choice') ||
|
169 |
+
btn.textContent.includes('Refine Prompt') ||
|
170 |
+
btn.textContent.includes('Apply Prompts')
|
171 |
+
);
|
172 |
+
allButtons.forEach(btn => btn.classList.remove('button-highlight'));
|
173 |
+
allButtons[2].classList.add('button-highlight'); // Highlight apply button
|
174 |
+
allButtons[1].classList.add('button-completed'); // Complete current button
|
175 |
+
allButtons[2].classList.remove('button-waiting'); // Remove waiting from apply button
|
176 |
+
}
|
177 |
+
"""
|
178 |
)
|
179 |
|
180 |
apply_button.click(
|
181 |
fn=self.apply_prompts,
|
182 |
inputs=[prompt_text, refined_prompt, apply_model],
|
183 |
+
outputs=[original_output, refined_output, original_output1, refined_output1],
|
184 |
+
show_progress=True # Add this line
|
185 |
+
).then(
|
186 |
+
fn=lambda: None,
|
187 |
+
inputs=None,
|
188 |
+
outputs=None,
|
189 |
+
js="""
|
190 |
+
() => {
|
191 |
+
// Update button states
|
192 |
+
const allButtons = Array.from(document.querySelectorAll('button')).filter(btn =>
|
193 |
+
btn.textContent.includes('Automatic Choice') ||
|
194 |
+
btn.textContent.includes('Refine Prompt') ||
|
195 |
+
btn.textContent.includes('Apply Prompts')
|
196 |
+
);
|
197 |
+
allButtons.forEach(btn => btn.classList.remove('button-highlight', 'button-waiting'));
|
198 |
+
allButtons[2].classList.add('button-completed'); // Complete apply button
|
199 |
+
|
200 |
+
// Force refresh of output containers
|
201 |
+
document.querySelectorAll('.comparison-output').forEach(el => {
|
202 |
+
if (el.parentElement) {
|
203 |
+
el.parentElement.style.display = 'none';
|
204 |
+
setTimeout(() => {
|
205 |
+
el.parentElement.style.display = 'block';
|
206 |
+
}, 100);
|
207 |
+
}
|
208 |
+
});
|
209 |
+
}
|
210 |
+
"""
|
211 |
+
)
|
212 |
|
213 |
+
# Reset when input changes
|
214 |
+
prompt_text.change(
|
215 |
+
fn=lambda: None,
|
216 |
+
inputs=None,
|
217 |
+
outputs=None,
|
218 |
+
js="""
|
219 |
+
() => {
|
220 |
+
// Clear all outputs
|
221 |
+
document.querySelectorAll('.analysis-container textarea, .analysis-container .markdown-text, .model-container .markdown-text, .comparison-output').forEach(el => {
|
222 |
+
if (el.value !== undefined) {
|
223 |
+
el.value = '';
|
224 |
+
} else {
|
225 |
+
el.textContent = '';
|
226 |
+
}
|
227 |
+
});
|
228 |
+
|
229 |
+
// Reset all button states
|
230 |
+
const allButtons = Array.from(document.querySelectorAll('button')).filter(btn =>
|
231 |
+
btn.textContent.includes('Automatic Choice') ||
|
232 |
+
btn.textContent.includes('Refine Prompt') ||
|
233 |
+
btn.textContent.includes('Apply Prompts')
|
234 |
+
);
|
235 |
+
allButtons.forEach(btn => {
|
236 |
+
btn.classList.remove('button-completed', 'button-highlight', 'button-waiting');
|
237 |
+
});
|
238 |
+
allButtons[0].classList.add('button-highlight'); // Highlight first button
|
239 |
+
allButtons.slice(1).forEach(btn => btn.classList.add('button-waiting')); // Set subsequent buttons to waiting
|
240 |
+
}
|
241 |
+
"""
|
242 |
)
|
243 |
|
244 |
def automatic_metaprompt(self, prompt: str) -> tuple:
|
|
|
258 |
"""Handle manual prompt refinement"""
|
259 |
try:
|
260 |
if not prompt.strip():
|
261 |
+
return ("No prompt provided.", "", "", {})
|
|
|
|
|
|
|
|
|
|
|
262 |
|
263 |
result = self.prompt_refiner.refine_prompt(prompt, meta_prompt_choice)
|
264 |
return (
|
|
|
274 |
def apply_prompts(self, original_prompt: str, refined_prompt: str, model: str) -> tuple:
|
275 |
"""Apply both original and refined prompts to the selected model"""
|
276 |
try:
|
277 |
+
if not original_prompt or not refined_prompt:
|
278 |
+
return ("Please provide both original and refined prompts.",
|
279 |
+
"Please provide both original and refined prompts.",
|
280 |
+
"Please provide both original and refined prompts.",
|
281 |
+
"Please provide both original and refined prompts.")
|
282 |
+
|
283 |
+
if not model:
|
284 |
+
return ("Please select a model.",
|
285 |
+
"Please select a model.",
|
286 |
+
"Please select a model.",
|
287 |
+
"Please select a model.")
|
288 |
+
|
289 |
+
# Apply prompts and get outputs
|
290 |
+
try:
|
291 |
+
# print(original_prompt)
|
292 |
+
# print(refined_prompt)
|
293 |
+
#print(model)
|
294 |
+
|
295 |
+
original_output = self.prompt_refiner.apply_prompt(original_prompt, model)
|
296 |
+
#print(original_output)
|
297 |
+
refined_output = self.prompt_refiner.apply_prompt(refined_prompt, model)
|
298 |
+
except Exception as e:
|
299 |
+
return (f"Error applying prompts: {str(e)}",
|
300 |
+
f"Error applying prompts: {str(e)}",
|
301 |
+
f"Error applying prompts: {str(e)}",
|
302 |
+
f"Error applying prompts: {str(e)}")
|
303 |
|
304 |
+
# Ensure we have string outputs
|
305 |
+
original_output = str(original_output) if original_output is not None else "No output generated"
|
306 |
+
refined_output = str(refined_output) if refined_output is not None else "No output generated"
|
307 |
+
print('-'*100)
|
308 |
+
print(original_output)
|
309 |
+
print('-'*100)
|
310 |
+
print(refined_output)
|
311 |
+
print('-'*100)
|
312 |
+
|
313 |
+
return (
|
314 |
+
original_output, # For Original Prompt Output tab
|
315 |
+
refined_output, # For Refined Prompt Output tab
|
316 |
+
original_output, # For Comparison tab - original
|
317 |
+
refined_output # For Comparison tab - refined
|
318 |
+
)
|
319 |
|
|
|
320 |
except Exception as e:
|
321 |
+
error_message = f"Error in apply_prompts: {str(e)}"
|
322 |
+
return (error_message, error_message, error_message, error_message)
|
323 |
|
324 |
def launch(self, share=False):
|
325 |
"""Launch the Gradio interface"""
|
custom_css.py
CHANGED
@@ -1,177 +1,382 @@
|
|
|
|
1 |
custom_css = """
|
|
|
|
|
|
|
|
|
|
|
2 |
.container {
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
margin: 0 auto 20px auto !important;
|
12 |
}
|
13 |
|
|
|
14 |
.container::before {
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
}
|
24 |
|
|
|
|
|
|
|
|
|
|
|
25 |
.title-container {
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
32 |
}
|
33 |
|
|
|
34 |
.title-container * {
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
}
|
39 |
|
|
|
40 |
.title-container h1 {
|
41 |
-
|
42 |
-
|
43 |
}
|
44 |
|
|
|
45 |
.title-container h3 {
|
46 |
-
|
47 |
-
|
48 |
}
|
49 |
|
|
|
50 |
.title-container p {
|
51 |
-
|
52 |
-
|
53 |
-
}
|
54 |
-
|
55 |
-
.input-container::before {
|
56 |
-
content: 'PROMPT ANALYSIS';
|
57 |
}
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
}
|
|
|
|
|
|
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
content: 'EXAMPLES';
|
73 |
-
}
|
74 |
-
|
75 |
-
/* Resizable textbox */
|
76 |
.input-container textarea {
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
}
|
86 |
|
|
|
87 |
.input-container textarea:focus {
|
88 |
-
|
89 |
-
|
90 |
}
|
91 |
|
92 |
-
/*
|
|
|
|
|
|
|
|
|
93 |
.radio-group {
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
}
|
104 |
|
|
|
105 |
.gradio-radio {
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
}
|
111 |
|
|
|
112 |
.gradio-radio label {
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
}
|
122 |
|
|
|
123 |
.gradio-radio input[type="radio"]:checked + label {
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
}
|
129 |
|
130 |
-
/*
|
|
|
|
|
|
|
|
|
131 |
.gradio-button {
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
}
|
141 |
|
|
|
142 |
.gradio-button:hover {
|
143 |
-
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
}
|
147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
/* Accordion styling */
|
149 |
.gradio-accordion {
|
150 |
-
|
151 |
-
|
152 |
}
|
153 |
|
154 |
-
/*
|
155 |
.gradio-container {
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
}
|
163 |
|
164 |
-
/* Dropdown styling */
|
165 |
.gradio-dropdown {
|
166 |
-
|
167 |
-
|
168 |
}
|
169 |
|
170 |
-
/* JSON container */
|
171 |
.full-response-json {
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
}
|
177 |
"""
|
|
|
1 |
+
|
2 |
custom_css = """
|
3 |
+
/* ============================================
|
4 |
+
MAIN LAYOUT AND CONTAINERS
|
5 |
+
Purpose: Core layout structure with minimal spacing
|
6 |
+
============================================ */
|
7 |
+
/* Main container styling with blue border and rounded corners */
|
8 |
.container {
|
9 |
+
border: 2px solid #2196F3; /* Blue border with 2px thickness */
|
10 |
+
border-radius: 10px; /* Rounded corners */
|
11 |
+
padding: 10px !important; /* Inner spacing */
|
12 |
+
margin: 2px auto !important; /* Outer spacing and center alignment */
|
13 |
+
background: white; /* White background */
|
14 |
+
position: relative; /* For absolute positioning of children */
|
15 |
+
width: 100% !important; /* Full width */
|
16 |
+
max-width: 1200px !important; /* Maximum width constraint */
|
|
|
17 |
}
|
18 |
|
19 |
+
/* Section header label positioning and styling */
|
20 |
.container::before {
|
21 |
+
position: absolute; /* Position independently */
|
22 |
+
top: -18px; /* Negative top margin to overlap container border */
|
23 |
+
left: 20px; /* Left offset */
|
24 |
+
background: white; /* White background for text */
|
25 |
+
padding: 0 2px; /* Horizontal padding*/
|
26 |
+
color: #2196F3; /* Blue text color */
|
27 |
+
font-weight: bold; /* Bold text */
|
28 |
+
font-size: 1.2em; /* Larger text size */
|
29 |
}
|
30 |
|
31 |
+
/* ============================================
|
32 |
+
TITLE SECTION
|
33 |
+
Purpose: "Prompts on Chosen Model" header styling
|
34 |
+
============================================ */
|
35 |
+
/* Title container styling */
|
36 |
.title-container {
|
37 |
+
width: fit-content !important; /* Width based on content */
|
38 |
+
margin: 0 auto !important; /* Center alignment */
|
39 |
+
margin-bottom: 30px !important; /* Adjust the value (30px) as needed */
|
40 |
+
padding: 2px 40px !important; /* Horizontal padding */
|
41 |
+
border: 1px solid #0066cc !important; /* Blue border */
|
42 |
+
border-radius: 10px !important; /* Rounded corners */
|
43 |
+
background-color: rgba(0, 102, 204, 0.05) !important; /* Light blue background */
|
44 |
}
|
45 |
|
46 |
+
/* Center align all text in title container */
|
47 |
.title-container * {
|
48 |
+
text-align: center; /* Center text alignment */
|
49 |
+
margin: 0 !important; /* Remove margins */
|
50 |
+
line-height: 1.2 !important; /* Line height for readability */
|
51 |
}
|
52 |
|
53 |
+
/* Main title styling */
|
54 |
.title-container h1 {
|
55 |
+
font-size: 28px !important; /* Large font size */
|
56 |
+
margin-bottom: 1px !important; /* Small bottom margin */
|
57 |
}
|
58 |
|
59 |
+
/* Subtitle styling */
|
60 |
.title-container h3 {
|
61 |
+
font-size: 18px !important; /* Medium font size */
|
62 |
+
margin-bottom: 1px !important; /* Small bottom margin */
|
63 |
}
|
64 |
|
65 |
+
/* Paragraph text styling in title */
|
66 |
.title-container p {
|
67 |
+
font-size: 14px !important; /* Regular font size */
|
68 |
+
margin-bottom: 1px !important; /* Small bottom margin */
|
|
|
|
|
|
|
|
|
69 |
}
|
70 |
|
71 |
+
/* ============================================
|
72 |
+
SECTION LABELS
|
73 |
+
Purpose: Section header text content
|
74 |
+
============================================ */
|
75 |
+
/* Define text content for each section header */
|
76 |
+
.input-container::before { content: 'PROMPT ANALYSIS'; }
|
77 |
+
.analysis-container::before { content: 'PROMPT REFINEMENT'; }
|
78 |
+
.meta-container::before { content: 'REFINEMENT METHOD'; }
|
79 |
+
.model-container::before { content: 'PROMPTS APPLICATION'; }
|
80 |
+
.examples-container::before { content: 'EXAMPLES'; }
|
81 |
|
82 |
+
/* ============================================
|
83 |
+
INPUT ELEMENTS
|
84 |
+
Purpose: Textarea and input styling
|
85 |
+
============================================ */
|
86 |
+
/* Textarea styling */
|
|
|
|
|
|
|
|
|
87 |
.input-container textarea {
|
88 |
+
resize: vertical !important; /* Allow vertical resizing only */
|
89 |
+
min-height: 100px !important; /* Minimum height */
|
90 |
+
max-height: 500px !important; /* Maximum height */
|
91 |
+
width: 100% !important; /* Full width */
|
92 |
+
border: 1px solid #ddd !important; /* Light gray border */
|
93 |
+
border-radius: 4px !important; /* Rounded corners */
|
94 |
+
padding: 2px !important; /* Inner spacing */
|
95 |
+
transition: all 0.3s ease !important; /* Smooth transitions */
|
96 |
}
|
97 |
|
98 |
+
/* Textarea focus state */
|
99 |
.input-container textarea:focus {
|
100 |
+
border-color: #2196F3 !important; /* Blue border when focused */
|
101 |
+
box-shadow: 0 0 0 2px rgba(33, 150, 243, 0.1) !important; /* Subtle shadow */
|
102 |
}
|
103 |
|
104 |
+
/* ============================================
|
105 |
+
RADIO BUTTONS
|
106 |
+
Purpose: Selection options styling
|
107 |
+
============================================ */
|
108 |
+
/* Radio button group container */
|
109 |
.radio-group {
|
110 |
+
background-color: rgba(0, 102, 204, 0.05) !important; /* Light blue background */
|
111 |
+
padding: 10px !important; /* Inner spacing */
|
112 |
+
border-radius: 8px !important; /* Rounded corners */
|
113 |
+
border: 1px solid rgba(0, 102, 204, 0.1) !important; /* Light blue border */
|
114 |
+
display: flex !important; /* Flex layout */
|
115 |
+
justify-content: center !important; /* Center items */
|
116 |
+
flex-wrap: wrap !important; /* Allow wrapping */
|
117 |
+
gap: 8px !important; /* Space between items */
|
118 |
+
width: 100% !important; /* Full width */
|
119 |
}
|
120 |
|
121 |
+
/* Radio button container */
|
122 |
.gradio-radio {
|
123 |
+
display: flex !important; /* Flex layout */
|
124 |
+
justify-content: center !important; /* Center items */
|
125 |
+
flex-wrap: wrap !important; /* Allow wrapping */
|
126 |
+
gap: 8px !important; /* Space between items */
|
127 |
}
|
128 |
|
129 |
+
/* Radio button label styling */
|
130 |
.gradio-radio label {
|
131 |
+
display: flex !important; /* Flex layout */
|
132 |
+
align-items: center !important; /* Center vertically */
|
133 |
+
padding: 6px 12px !important; /* Inner spacing */
|
134 |
+
border: 1px solid #ddd !important; /* Light gray border */
|
135 |
+
border-radius: 4px !important; /* Rounded corners */
|
136 |
+
cursor: pointer !important; /* Pointer cursor */
|
137 |
+
background: white !important; /* White background */
|
138 |
+
margin: 4px !important; /* Outer spacing */
|
139 |
}
|
140 |
|
141 |
+
/* Selected radio button styling */
|
142 |
.gradio-radio input[type="radio"]:checked + label {
|
143 |
+
background: rgba(0, 102, 204, 0.1) !important; /* Light blue background */
|
144 |
+
border-color: #0066cc !important; /* Blue border */
|
145 |
+
color: #0066cc !important; /* Blue text */
|
146 |
+
font-weight: bold !important; /* Bold text */
|
147 |
}
|
148 |
|
149 |
+
/* ============================================
|
150 |
+
BUTTONS
|
151 |
+
Purpose: Interactive button styling
|
152 |
+
============================================ */
|
153 |
+
/* Base button styling */
|
154 |
.gradio-button {
|
155 |
+
background-color: white !important; /* White background */
|
156 |
+
color: #2196F3 !important; /* Blue text */
|
157 |
+
border: 2px solid #2196F3 !important; /* Blue border */
|
158 |
+
border-radius: 4px !important; /* Rounded corners */
|
159 |
+
padding: 8px 16px !important; /* Inner spacing */
|
160 |
+
margin: 10px 0 !important; /* Vertical margin */
|
161 |
+
font-weight: bold !important; /* Bold text */
|
162 |
+
transition: all 0.3s ease !important; /* Smooth transitions */
|
163 |
}
|
164 |
|
165 |
+
/* Button hover state */
|
166 |
.gradio-button:hover {
|
167 |
+
background-color: #2196F3 !important; /* Blue background on hover */
|
168 |
+
color: white !important; /* White text on hover */
|
169 |
+
box-shadow: 0 2px 5px rgba(33, 150, 243, 0.3) !important; /* Shadow effect */
|
170 |
+
}
|
171 |
+
|
172 |
+
/* Highlighted button state */
|
173 |
+
.button-highlight {
|
174 |
+
animation: pulse 2s infinite; /* Pulsing animation */
|
175 |
+
border-color: #ff9800 !important; /* Orange border */
|
176 |
+
border-width: 3px !important; /* Thicker border */
|
177 |
+
box-shadow: 0 0 10px rgba(255, 152, 0, 0.5) !important; /* Glow effect */
|
178 |
+
}
|
179 |
+
|
180 |
+
/* Pulsing animation keyframes */
|
181 |
+
@keyframes pulse {
|
182 |
+
0% { box-shadow: 0 0 0 0 rgba(255, 152, 0, 0.4); } /* Start state */
|
183 |
+
70% { box-shadow: 0 0 0 10px rgba(255, 152, 0, 0); } /* Mid state */
|
184 |
+
100% { box-shadow: 0 0 0 0 rgba(255, 152, 0, 0); } /* End state */
|
185 |
}
|
186 |
|
187 |
+
/* Waiting button state */
|
188 |
+
.button-waiting {
|
189 |
+
opacity: 0.6 !important; /* Reduced opacity */
|
190 |
+
}
|
191 |
+
|
192 |
+
/* Completed button state */
|
193 |
+
.button-completed {
|
194 |
+
border-color: #4CAF50 !important; /* Green border */
|
195 |
+
background-color: rgba(76, 175, 80, 0.1) !important; /* Light green background */
|
196 |
+
}
|
197 |
+
|
198 |
+
/* ============================================
|
199 |
+
LAYOUT COMPONENTS
|
200 |
+
Purpose: Basic layout element styling
|
201 |
+
============================================ */
|
202 |
/* Accordion styling */
|
203 |
.gradio-accordion {
|
204 |
+
margin: 10px 0 !important; /* Vertical margin */
|
205 |
+
border: none !important; /* Remove border */
|
206 |
}
|
207 |
|
208 |
+
/* Main container layout */
|
209 |
.gradio-container {
|
210 |
+
display: flex !important; /* Flex layout */
|
211 |
+
flex-direction: column !important; /* Stack vertically */
|
212 |
+
align-items: center !important; /* Center items */
|
213 |
+
width: 100% !important; /* Full width */
|
214 |
+
max-width: 1200px !important; /* Maximum width */
|
215 |
+
margin: 2px auto !important; /* Center horizontally */
|
216 |
}
|
217 |
|
218 |
+
/* Dropdown menu styling */
|
219 |
.gradio-dropdown {
|
220 |
+
width: 100% !important; /* Full width */
|
221 |
+
max-width: 300px !important; /* Maximum width */
|
222 |
}
|
223 |
|
224 |
+
/* JSON response container */
|
225 |
.full-response-json {
|
226 |
+
margin-top: 20px !important; /* Top margin */
|
227 |
+
padding: 10px !important; /* Inner spacing */
|
228 |
+
background-color: rgba(0, 102, 204, 0.05) !important; /* Light blue background */
|
229 |
+
border-radius: 8px !important; /* Rounded corners */
|
230 |
+
}
|
231 |
+
|
232 |
+
/* ============================================
|
233 |
+
COMPARISON COLUMNS
|
234 |
+
Purpose: Side-by-side output display
|
235 |
+
============================================ */
|
236 |
+
/* Column container styling */
|
237 |
+
.comparison-column {
|
238 |
+
border: 2px solid #2196F3 !important; /* Blue border */
|
239 |
+
border-radius: 8px !important; /* Rounded corners */
|
240 |
+
padding: 4px !important; /* Inner spacing */
|
241 |
+
margin: 1px !important; /* Minimal margin */
|
242 |
+
background-color: white !important; /* White background */
|
243 |
+
flex: 1 !important; /* Equal width columns */
|
244 |
+
min-width: 300px !important; /* Minimum width */
|
245 |
+
padding-right: 2px !important; /* Add this to remove right padding */
|
246 |
+
margin-right: 2px !important; /* Add this to remove right margin */
|
247 |
+
}
|
248 |
+
|
249 |
+
/* Column header styling */
|
250 |
+
.comparison-column h3 {
|
251 |
+
color: #2196F3 !important; /* Blue text */
|
252 |
+
border-bottom: 1px solid #e0e0e0 !important; /* Bottom border */
|
253 |
+
padding-bottom: 2px !important; /* Bottom padding */
|
254 |
+
margin: 0 0 4px 0 !important; /* Bottom margin */
|
255 |
+
font-size: 16px !important; /* Font size */
|
256 |
+
text-align: center !important; /* Center text */
|
257 |
+
}
|
258 |
+
|
259 |
+
/* Output area styling */
|
260 |
+
.comparison-output {
|
261 |
+
min-height: 200px !important; /* Minimum height */
|
262 |
+
padding: 6px !important; /* Inner spacing */
|
263 |
+
background-color: #f8f9fa !important; /* Light gray background */
|
264 |
+
border: 1px solid #dee2e6 !important; /* Gray border */
|
265 |
+
border-radius: 4px !important; /* Rounded corners */
|
266 |
+
margin: 4px 0 !important; /* Vertical margin */
|
267 |
+
white-space: pre-wrap !important; /* Preserve whitespace */
|
268 |
+
word-wrap: break-word !important; /* Break long words */
|
269 |
+
font-family: monospace !important; /* Monospace font */
|
270 |
+
line-height: 1.5 !important; /* Line height */
|
271 |
+
overflow-y: auto !important; /* Vertical scroll */
|
272 |
+
width: 100% !important; /* Full width */
|
273 |
+
visibility: visible !important; /* Always visible */
|
274 |
+
opacity: 1 !important; /* Full opacity */
|
275 |
+
padding: 10px 3px !important; /* 20px top/bottom, 30px left/right */
|
276 |
+
}
|
277 |
+
|
278 |
+
/* ============================================
|
279 |
+
OUTPUT ROW
|
280 |
+
Purpose: Layout for output display
|
281 |
+
============================================ */
|
282 |
+
/* Output row container */
|
283 |
+
.output-row {
|
284 |
+
display: flex !important; /* Flex layout */
|
285 |
+
gap: 1mm !important; /* Small gap between items */
|
286 |
+
padding: 2mm !important; /* Inner spacing */
|
287 |
+
width: 100% !important; /* Full width */
|
288 |
+
flex-wrap: wrap !important; /* Allow wrapping */
|
289 |
+
}
|
290 |
+
|
291 |
+
/* ============================================
|
292 |
+
TABS
|
293 |
+
Purpose: Tab navigation styling
|
294 |
+
============================================ */
|
295 |
+
/* Tab container */
|
296 |
+
.tabs {
|
297 |
+
border: none !important; /* Remove border */
|
298 |
+
margin-top: 4px !important; /* Top margin */
|
299 |
+
width: 100% !important; /* Full width */
|
300 |
+
}
|
301 |
+
|
302 |
+
/* Individual tab item */
|
303 |
+
.tabitem {
|
304 |
+
padding: 4px !important; /* Inner spacing */
|
305 |
+
width: 100% !important; /* Full width */
|
306 |
+
}
|
307 |
+
|
308 |
+
/* ============================================
|
309 |
+
TEXT CONTENT
|
310 |
+
Purpose: Text display formatting
|
311 |
+
============================================ */
|
312 |
+
/* Markdown text styling */
|
313 |
+
.markdown-text {
|
314 |
+
color: #333333 !important; /* Dark gray text */
|
315 |
+
line-height: 1.6 !important; /* Line height */
|
316 |
+
font-size: 14px !important; /* Font size */
|
317 |
+
margin: 4px 4px !important; /* Vertical margin */
|
318 |
+
opacity: 1 !important; /* Full opacity */
|
319 |
+
visibility: visible !important; /* Always visible */
|
320 |
+
padding: 15px !important;
|
321 |
+
}
|
322 |
+
|
323 |
+
/* Placeholder text for empty output */
|
324 |
+
.comparison-output:empty::before {
|
325 |
+
content: 'Output will appear here' !important; /* Placeholder text */
|
326 |
+
color: #666666 !important; /* Gray text */
|
327 |
+
font-style: italic !important; /* Italic style */
|
328 |
+
}
|
329 |
+
|
330 |
+
/* ============================================
|
331 |
+
BUTTON STATES
|
332 |
+
Purpose: Button interaction styling
|
333 |
+
============================================ */
|
334 |
+
/* Default button state */
|
335 |
+
button {
|
336 |
+
opacity: 1 !important; /* Full opacity */
|
337 |
+
pointer-events: auto !important; /* Enable interactions */
|
338 |
+
}
|
339 |
+
|
340 |
+
/* Disabled button state */
|
341 |
+
button:disabled {
|
342 |
+
opacity: 0.6 !important; /* Reduced opacity */
|
343 |
+
pointer-events: none !important; /* Disable interactions */
|
344 |
+
}
|
345 |
+
|
346 |
+
/* ============================================
|
347 |
+
VISIBILITY
|
348 |
+
Purpose: Element display control
|
349 |
+
============================================ */
|
350 |
+
/* Output content visibility */
|
351 |
+
.output-content {
|
352 |
+
opacity: 1 !important; /* Full opacity */
|
353 |
+
visibility: visible !important; /* Always visible */
|
354 |
+
display: block !important; /* Block display */
|
355 |
+
}
|
356 |
+
|
357 |
+
/* Output container visibility */
|
358 |
+
.output-container {
|
359 |
+
display: block !important; /* Block display */
|
360 |
+
visibility: visible !important; /* Always visible */
|
361 |
+
opacity: 1 !important; /* Full opacity */
|
362 |
+
}
|
363 |
+
|
364 |
+
/* ============================================
|
365 |
+
CODE BLOCKS
|
366 |
+
Purpose: Code and pre-formatted text styling
|
367 |
+
============================================ */
|
368 |
+
/* Code block styling */
|
369 |
+
pre, code {
|
370 |
+
background-color: #f8f9fa !important; /* Light gray background */
|
371 |
+
border: 1px solid #dee2e6 !important; /* Gray border */
|
372 |
+
border-radius: 4px !important; /* Rounded corners */
|
373 |
+
padding: 10px !important; /* Inner spacing */
|
374 |
+
margin: 5px 0 !important; /* Vertical margin */
|
375 |
+
white-space: pre-wrap !important; /* Preserve whitespace */
|
376 |
+
word-wrap: break-word !important; /* Break long words */
|
377 |
+
font-family: monospace !important; /* Monospace font */
|
378 |
+
line-height: 1.5 !important; /* Line height */
|
379 |
+
display: block !important; /* Block display */
|
380 |
+
width: 100% !important; /* Full width */
|
381 |
}
|
382 |
"""
|
prompt_refiner.py
CHANGED
@@ -55,13 +55,13 @@ class PromptRefiner:
|
|
55 |
if isinstance(parsed_json, str):
|
56 |
parsed_json = json.loads(parsed_json)
|
57 |
prompt_analysis = f"""
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
explanation_of_refinements=f"""
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
return {
|
66 |
"initial_prompt_evaluation": prompt_analysis,
|
67 |
"refined_prompt": parsed_json.get("refined_prompt", ""),
|
@@ -124,40 +124,39 @@ class PromptRefiner:
|
|
124 |
"content": metaprompt_router.replace("[Insert initial prompt here]", prompt)
|
125 |
}
|
126 |
]
|
127 |
-
|
128 |
router_response = self.client.chat_completion(
|
129 |
model=prompt_refiner_model,
|
130 |
messages=router_messages,
|
131 |
max_tokens=3000,
|
132 |
temperature=0.2
|
133 |
)
|
134 |
-
|
135 |
router_content = router_response.choices[0].message.content.strip()
|
136 |
json_match = re.search(r'<json>(.*?)</json>', router_content, re.DOTALL)
|
137 |
|
138 |
if not json_match:
|
139 |
raise ValueError("No JSON found in router response")
|
140 |
-
|
141 |
router_result = json.loads(json_match.group(1))
|
142 |
recommended_key = router_result["recommended_metaprompt"]["key"]
|
143 |
metaprompt_analysis = f"""
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
"""
|
155 |
|
156 |
return metaprompt_analysis, recommended_key
|
157 |
-
|
158 |
except Exception as e:
|
159 |
return f"Error in automatic metaprompt: {str(e)}", ""
|
160 |
-
|
161 |
|
162 |
def refine_prompt(self, prompt: str, meta_prompt_choice: str) -> Tuple[str, str, str, dict]:
|
163 |
"""Refine the given prompt using the selected meta prompt."""
|
@@ -175,14 +174,14 @@ class PromptRefiner:
|
|
175 |
"content": selected_meta_prompt.replace("[Insert initial prompt here]", prompt)
|
176 |
}
|
177 |
]
|
178 |
-
|
179 |
response = self.client.chat_completion(
|
180 |
model=prompt_refiner_model,
|
181 |
messages=messages,
|
182 |
max_tokens=3000,
|
183 |
temperature=0.8
|
184 |
)
|
185 |
-
|
186 |
result = self._parse_response(response.choices[0].message.content.strip())
|
187 |
llm_response = LLMResponse(**result)
|
188 |
llm_response_dico={}
|
@@ -196,7 +195,7 @@ class PromptRefiner:
|
|
196 |
llm_response.explanation_of_refinements,
|
197 |
llm_response_dico
|
198 |
)
|
199 |
-
|
200 |
except Exception as e:
|
201 |
return (
|
202 |
f"Error: {str(e)}",
|
@@ -205,42 +204,35 @@ class PromptRefiner:
|
|
205 |
{}
|
206 |
)
|
207 |
|
208 |
-
def _create_error_response(self, error_message: str) -> Tuple[str, str, str, str, dict]:
|
209 |
-
"""Create a standardized error response tuple."""
|
210 |
-
return (
|
211 |
-
# f"Error: {error_message}",
|
212 |
-
f"Error: {error_message}",
|
213 |
-
"The selected model is currently unavailable.",
|
214 |
-
"An error occurred during processing.",
|
215 |
-
{"error": error_message}
|
216 |
-
)
|
217 |
-
|
218 |
def apply_prompt(self, prompt: str, model: str) -> str:
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
|
|
|
|
|
|
|
55 |
if isinstance(parsed_json, str):
|
56 |
parsed_json = json.loads(parsed_json)
|
57 |
prompt_analysis = f"""
|
58 |
+
#### Original prompt analysis
|
59 |
+
- {parsed_json.get("initial_prompt_evaluation", "")}
|
60 |
+
"""
|
61 |
explanation_of_refinements=f"""
|
62 |
+
#### Refinement Explanation
|
63 |
+
- {parsed_json.get("explanation_of_refinements", "")}
|
64 |
+
"""
|
65 |
return {
|
66 |
"initial_prompt_evaluation": prompt_analysis,
|
67 |
"refined_prompt": parsed_json.get("refined_prompt", ""),
|
|
|
124 |
"content": metaprompt_router.replace("[Insert initial prompt here]", prompt)
|
125 |
}
|
126 |
]
|
127 |
+
|
128 |
router_response = self.client.chat_completion(
|
129 |
model=prompt_refiner_model,
|
130 |
messages=router_messages,
|
131 |
max_tokens=3000,
|
132 |
temperature=0.2
|
133 |
)
|
134 |
+
|
135 |
router_content = router_response.choices[0].message.content.strip()
|
136 |
json_match = re.search(r'<json>(.*?)</json>', router_content, re.DOTALL)
|
137 |
|
138 |
if not json_match:
|
139 |
raise ValueError("No JSON found in router response")
|
140 |
+
|
141 |
router_result = json.loads(json_match.group(1))
|
142 |
recommended_key = router_result["recommended_metaprompt"]["key"]
|
143 |
metaprompt_analysis = f"""
|
144 |
+
#### Selected MetaPrompt
|
145 |
+
- **Primary Choice**: {router_result["recommended_metaprompt"]["name"]}
|
146 |
+
- *Description*: {router_result["recommended_metaprompt"]["description"]}
|
147 |
+
- *Why This Choice*: {router_result["recommended_metaprompt"]["explanation"]}
|
148 |
+
- *Similar Sample*: {router_result["recommended_metaprompt"]["similar_sample"]}
|
149 |
+
- *Customized Sample*: {router_result["recommended_metaprompt"]["customized_sample"]}
|
150 |
+
|
151 |
+
#### Alternative Option
|
152 |
+
- **Secondary Choice**: {router_result["alternative_recommendation"]["name"]}
|
153 |
+
- *Why Consider This*: {router_result["alternative_recommendation"]["explanation"]}
|
154 |
"""
|
155 |
|
156 |
return metaprompt_analysis, recommended_key
|
157 |
+
|
158 |
except Exception as e:
|
159 |
return f"Error in automatic metaprompt: {str(e)}", ""
|
|
|
160 |
|
161 |
def refine_prompt(self, prompt: str, meta_prompt_choice: str) -> Tuple[str, str, str, dict]:
|
162 |
"""Refine the given prompt using the selected meta prompt."""
|
|
|
174 |
"content": selected_meta_prompt.replace("[Insert initial prompt here]", prompt)
|
175 |
}
|
176 |
]
|
177 |
+
|
178 |
response = self.client.chat_completion(
|
179 |
model=prompt_refiner_model,
|
180 |
messages=messages,
|
181 |
max_tokens=3000,
|
182 |
temperature=0.8
|
183 |
)
|
184 |
+
|
185 |
result = self._parse_response(response.choices[0].message.content.strip())
|
186 |
llm_response = LLMResponse(**result)
|
187 |
llm_response_dico={}
|
|
|
195 |
llm_response.explanation_of_refinements,
|
196 |
llm_response_dico
|
197 |
)
|
198 |
+
|
199 |
except Exception as e:
|
200 |
return (
|
201 |
f"Error: {str(e)}",
|
|
|
204 |
{}
|
205 |
)
|
206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
207 |
def apply_prompt(self, prompt: str, model: str) -> str:
|
208 |
+
"""Apply formatting to the prompt using the specified model."""
|
209 |
+
try:
|
210 |
+
if not prompt or not model:
|
211 |
+
return "Error: Prompt and model are required"
|
212 |
+
|
213 |
+
messages = [
|
214 |
+
{
|
215 |
+
"role": "system",
|
216 |
+
"content": "You are a markdown formatting expert."
|
217 |
+
},
|
218 |
+
{
|
219 |
+
"role": "user",
|
220 |
+
"content": prompt
|
221 |
+
}
|
222 |
+
]
|
223 |
+
|
224 |
+
response = self.client.chat_completion(
|
225 |
+
model=model,
|
226 |
+
messages=messages,
|
227 |
+
max_tokens=3000,
|
228 |
+
temperature=0.8,
|
229 |
+
stream=False # Mode non-stream
|
230 |
+
)
|
231 |
+
|
232 |
+
# Accès direct à la réponse puisque stream=False
|
233 |
+
result = response.choices[0].message.content.strip()
|
234 |
+
return f"""{result}"""
|
235 |
+
|
236 |
+
|
237 |
+
except Exception as e:
|
238 |
+
return f"Error: {str(e)}"
|