baconnier commited on
Commit
304227c
1 Parent(s): 9ac9735

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +7 -7
  2. app.py +531 -0
  3. requirements.txt +6 -0
README.md CHANGED
@@ -1,14 +1,14 @@
1
  ---
2
- title: Prompt Plus Plus
3
- emoji: 📉
4
- colorFrom: green
5
- colorTo: green
6
  sdk: gradio
7
- sdk_version: 5.1.0
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
- short_description: Refine your Prompt
12
  ---
13
 
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: PROMPT++
3
+ emoji: ✍️
4
+ colorFrom: indigo
5
+ colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 4.44.1
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
+ short_description: Refine your prompts
12
  ---
13
 
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,531 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import re
4
+ from huggingface_hub import InferenceClient
5
+ import gradio as gr
6
+ from pydantic import BaseModel, Field
7
+ from typing import Optional, Literal
8
+
9
+ # Define the metaprompts
10
+ metaprompt1 = """
11
+ As an AI Prompt Enhancement Specialist, your task is to improve the given prompt using advanced prompting techniques while emphasizing the use of previously generated context. Analyze the input prompt and apply the following steps to enhance it:
12
+
13
+ 1. Role Prompting: Assume the role of an "Expert Prompt Analyst and Optimizer" with deep knowledge in the subject matter of the input prompt.
14
+
15
+ 2. Structured Output Generation: Design a prompt structure that builds upon previous sections:
16
+ a) Introduction
17
+ b) Key Points (referencing the introduction)
18
+ c) Detailed Analysis (expanding on key points and referencing previous sections)
19
+ d) Conclusion (summarizing and synthesizing all previous content)
20
+ e) Final Review and Refinement
21
+
22
+ 3. Few-Shot Prompting: Provide 2-3 relevant examples demonstrating the desired output format, reasoning, and effective use of previous context.
23
+
24
+ 4. Chain-of-Thought (CoT) and Zero-Shot CoT: Incorporate step-by-step reasoning in your instructions, explaining the thought process for each enhancement and how to handle unfamiliar tasks.
25
+
26
+ 5. Self-Consistency: Generate multiple reasoning paths and select the most consistent one for the final improved prompt.
27
+
28
+ 6. Least-to-Most Prompting: Break down complex aspects of the prompt into smaller, manageable sub-tasks that build upon each other.
29
+
30
+ 7. Tree-of-Thought: Create a branching structure for exploring multiple improvement paths simultaneously, considering different aspects of the prompt.
31
+
32
+ 8. Prompt Paraphrasing: Rephrase key instructions in multiple ways to ensure clarity and comprehension.
33
+
34
+ 9. Self-Calibration: Include steps for the AI to assess its own confidence in the generated output and adjustments.
35
+
36
+ 10. Self-Refine: Implement an iterative process for the AI to improve its initial response to the prompt enhancement task.
37
+
38
+ 11. Self-Verification and Chain-of-Verification (COVE): Add verification steps to check the logical consistency, effectiveness, and fulfillment of all requirements in the improved prompt.
39
+
40
+ 12. Cumulative Reasoning: Build upon previous enhancements, accumulating improvements throughout the process.
41
+
42
+ 13. Style Prompting: Adjust the language style of the prompt to match the intended audience or purpose.
43
+
44
+ 14. Emotion Prompting: Incorporate language that emphasizes the importance and impact of the task described in the prompt.
45
+
46
+ 15. System 2 Attention (S2A): Guide the AI to focus on specific critical aspects of the prompt that require careful consideration.
47
+
48
+ 16. Rephrase and Respond (RaR): After each major enhancement, instruct the AI to rephrase the prompt and respond to it to test its effectiveness.
49
+
50
+ 17. Re-reading (RE2): Instruct the AI to re-read the original and enhanced prompts multiple times to catch nuances and potential improvements.
51
+
52
+ 18. Self-Ask: Encourage the AI to ask itself questions about the prompt's clarity, effectiveness, and potential weaknesses, especially in relation to using previous context.
53
+
54
+ 19. Automatic Chain-of-Thought (Auto-CoT): Generate intermediate reasoning steps automatically to support the prompt enhancement process.
55
+
56
+ 20. Program-of-Thoughts: Structure the prompt improvement process as a series of logical operations or a program-like sequence.
57
+
58
+ 21. Skeleton-of-Thought: Create a basic structure or skeleton for the enhanced prompt, then iteratively fill in the details.
59
+
60
+ 22. Mixture of Reasoning Experts (MoRE): Combine insights from multiple expert perspectives (e.g., subject matter expert, language specialist, task optimization expert) to enhance the prompt.
61
+
62
+ 23. Max Mutual Information Method: Optimize the prompt to maximize the mutual information between the input and desired output.
63
+
64
+ Now, apply these techniques to improve the following prompt:
65
+
66
+ Initial Prompt: [Insert initial prompt here]
67
+
68
+ Follow these steps to generate an enhanced version of the prompt:
69
+
70
+ 1. Analyze the original prompt, identifying its main objectives, strengths, and weaknesses.
71
+ 2. Apply the role of an Expert Prompt Analyst and Optimizer, considering the subject matter.
72
+ 3. Design a structured output format that encourages use of previous context.
73
+ 4. Use the Tree-of-Thought technique to explore multiple enhancement paths.
74
+ 5. For each path, apply Chain-of-Thought reasoning to explain the enhancements.
75
+ 6. Incorporate Few-Shot examples to illustrate the desired outcome and effective use of previous context.
76
+ 7. Break down complex aspects using Least-to-Most Prompting.
77
+ 8. Rephrase key instructions using Prompt Paraphrasing.
78
+ 9. Adjust the language style and incorporate emotional elements as appropriate.
79
+ 10. Implement Self-Verification and Chain-of-Verification steps.
80
+ 11. Use Self-Ask to critically evaluate the enhancements, especially regarding context usage.
81
+ 12. Apply the Skeleton-of-Thought technique to structure the improved prompt.
82
+ 13. Utilize the Mixture of Reasoning Experts approach to refine the prompt from multiple perspectives.
83
+ 14. Employ the Max Mutual Information Method to optimize the prompt's effectiveness.
84
+ 15. Use Self-Calibration to assess the confidence in the final enhanced prompt.
85
+ 16. Apply Self-Refine to iteratively improve the enhanced prompt.
86
+
87
+ Present the final enhanced prompt, along with a brief explanation of the key improvements and techniques used. Ensure that the enhanced prompt:
88
+ 1. Maintains the original intent
89
+ 2. Significantly improves effectiveness and clarity
90
+ 3. Effectively leverages previously generated context
91
+ 4. Includes explicit instructions for the AI to refer back to and build upon its own previous outputs
92
+ 5. Creates a cohesive and interconnected result
93
+
94
+ Your enhanced prompt should guide the AI to generate a response that not only addresses the original query but also demonstrates a clear progression of thought and utilization of previously generated information throughout the response.
95
+
96
+ Only provide the output in the following JSON format enclosed in <json> tags:
97
+ <json>
98
+ {
99
+ "initial_prompt_evaluation": "Your evaluation of the initial prompt with Strengths and Weaknesses in a string as bullet points format",
100
+ "refined_prompt": "Your refined prompt",
101
+ "explanation_of_refinements": "Explain techniques used and improvements made, including the extract of final prompt where it used. Answer in a string "
102
+ }
103
+ </json>
104
+ """
105
+
106
+
107
+ loic_metaprompt = """
108
+ Given an initial prompt:
109
+
110
+ Initial Prompt: [Insert initial prompt here]
111
+
112
+ Understand the Task: Grasp the main objective, goals, requirements, constraints, and expected output.
113
+ - Minimal Changes: If an existing prompt is provided, improve it only if it's simple. For complex prompts, enhance clarity and add missing elements without altering the original structure.
114
+ - Leverage Autoregressive Nature: Strategically order information to prime the model for better results. Generate key concepts, context, or vocabulary early in the prompt to inform later reasoning and outputs.
115
+ - Reasoning Before Conclusions: Encourage reasoning steps before any conclusions are reached. If user examples show reasoning afterward, reverse the order. Never start examples with conclusions.
116
+ - Reasoning Order: Identify reasoning and conclusion parts. Determine their order and reverse if needed. Conclusions, classifications, or results should always appear last.
117
+ - Examples: Include high-quality examples if helpful, using placeholders [in brackets] for complex elements.
118
+ - Assess example needs, quantity, and complexity for potential placeholder use.
119
+ - Clarity and Conciseness: Use clear, specific language. Avoid unnecessary instructions or bland statements.
120
+ - Formatting: Use markdown features for readability. Avoid code blocks unless requested.
121
+ - Preserve User Content: Retain extensive guidelines or examples from input. Break down vague instructions into sub-steps. Maintain user-provided details, guidelines, examples, variables, or placeholders.
122
+ - Constants: Include constants (guides, rubrics, examples) as they resist prompt injection.
123
+ - Output Format: Specify appropriate output format in detail, including length and syntax.
124
+ - For structured data, prefer JSON output (unwrapped unless requested).
125
+
126
+ The final prompt should follow this structure, with no additional commentary:
127
+ Leverage Autoregressive Nature: Strategically order information to prime the model for better results. Generate key concepts, context, or vocabulary early in the prompt to inform later reasoning and outputs.
128
+
129
+ [Concise task instruction - first line, no header]
130
+
131
+ [Additional details as needed]
132
+
133
+ [Optional sections with headings or bullet points for detailed steps]
134
+
135
+ # Context Generation
136
+
137
+ [Instructions for generating key concepts, vocabulary, or context to inform later reasoning]
138
+
139
+ # Steps [optional]
140
+
141
+ [Detailed task breakdown]
142
+
143
+ # Reasoning Process
144
+
145
+ [Specific instructions for the reasoning process, ensuring it precedes conclusions]
146
+
147
+ # Output Format
148
+
149
+ [Detailed output format specifications]
150
+
151
+ # Examples [optional]
152
+
153
+ [1-3 well-defined examples with placeholders if necessary. Mark input/output clearly. Use placeholders and indicate if real examples differ in length/complexity]
154
+
155
+ # Notes [optional]
156
+
157
+ [Edge cases, important considerations, repeated key points]
158
+
159
+ Only provide the output in the following JSON format enclosed in <json> tags:
160
+
161
+ <json>
162
+ {
163
+ "initial_prompt_evaluation": "Your evaluation of the initial prompt with Strengths and Weaknesses in a string",
164
+ "refined_prompt": "Your refined prompt into quote",
165
+ "explanation_of_refinements": "Explanation of the techniques used and improvements made, also include the extract of final prompt where it made.Answer in bullet points if accurate"
166
+ }
167
+ </json>
168
+ """
169
+
170
+ openai_metaprompt='''
171
+ Given a task description or existing prompt, produce a detailed system prompt to guide a language model in completing the task effectively.
172
+
173
+ # Guidelines
174
+
175
+ - Understand the Task: Grasp the main objective, goals, requirements, constraints, and expected output.
176
+ - Minimal Changes: If an existing prompt is provided, improve it only if it's simple. For complex prompts, enhance clarity and add missing elements without altering the original structure.
177
+ - Reasoning Before Conclusions**: Encourage reasoning steps before any conclusions are reached. ATTENTION! If the user provides examples where the reasoning happens afterward, REVERSE the order! NEVER START EXAMPLES WITH CONCLUSIONS!
178
+ - Reasoning Order: Call out reasoning portions of the prompt and conclusion parts (specific fields by name). For each, determine the ORDER in which this is done, and whether it needs to be reversed.
179
+ - Conclusion, classifications, or results should ALWAYS appear last.
180
+ - Examples: Include high-quality examples if helpful, using placeholders [in brackets] for complex elements.
181
+ - What kinds of examples may need to be included, how many, and whether they are complex enough to benefit from placeholders.
182
+ - Clarity and Conciseness: Use clear, specific language. Avoid unnecessary instructions or bland statements.
183
+ - Formatting: Use markdown features for readability. DO NOT USE ``` CODE BLOCKS UNLESS SPECIFICALLY REQUESTED.
184
+ - Preserve User Content: If the input task or prompt includes extensive guidelines or examples, preserve them entirely, or as closely as possible. If they are vague, consider breaking down into sub-steps. Keep any details, guidelines, examples, variables, or placeholders provided by the user.
185
+ - Constants: DO include constants in the prompt, as they are not susceptible to prompt injection. Such as guides, rubrics, and examples.
186
+ - Output Format: Explicitly the most appropriate output format, in detail. This should include length and syntax (e.g. short sentence, paragraph, JSON, etc.)
187
+ - For tasks outputting well-defined or structured data (classification, JSON, etc.) bias toward outputting a JSON.
188
+ - JSON should never be wrapped in code blocks (```) unless explicitly requested.
189
+
190
+ The final prompt you output should adhere to the following structure below. Do not include any additional commentary, only output the completed system prompt. SPECIFICALLY, do not include any additional messages at the start or end of the prompt. (e.g. no "---")
191
+
192
+ [Concise instruction describing the task - this should be the first line in the prompt, no section header]
193
+
194
+ [Additional details as needed.]
195
+
196
+ [Optional sections with headings or bullet points for detailed steps.]
197
+
198
+ # Steps [optional]
199
+
200
+ [optional: a detailed breakdown of the steps necessary to accomplish the task]
201
+
202
+ # Output Format
203
+
204
+ [Specifically call out how the output should be formatted, be it response length, structure e.g. JSON, markdown, etc]
205
+
206
+ # Examples [optional]
207
+
208
+ [Optional: 1-3 well-defined examples with placeholders if necessary. Clearly mark where examples start and end, and what the input and output are. User placeholders as necessary.]
209
+ [If the examples are shorter than what a realistic example is expected to be, make a reference with () explaining how real examples should be longer / shorter / different. AND USE PLACEHOLDERS! ]
210
+
211
+ # Notes [optional]
212
+
213
+ [optional: edge cases, details, and an area to call or repeat out specific important considerations]
214
+
215
+ Only provide the output in the following JSON format enclosed in <json> tags:
216
+
217
+ <json>
218
+ {
219
+ "initial_prompt_evaluation": "Your evaluation of the initial prompt with Strengths and Weaknesses in a string on markdown bullet points format",
220
+ "refined_prompt": "Your refined prompt",
221
+ "explanation_of_refinements": "Explanation of the techniques used and improvements made, also include the extract of final prompt where it made. Answer in bullet points if accurate"
222
+ }
223
+ </json>
224
+ '''
225
+ original_meta_prompt = '''Create an effective prompt from this one:
226
+
227
+ Initial Prompt: [Insert initial prompt here]
228
+
229
+ Use these simple steps:
230
+
231
+ 1. Look at the task closely, focusing on goals, needs, and limits.
232
+ 2. Make prompts better by making language clear without changing the main idea.
233
+ 3. Keep a logical flow by putting reasoning steps first.
234
+ 4. Use good examples with [placeholders] for tricky parts.
235
+ 5. Use simple words; remove unneeded instructions.
236
+ 6. Use markdown for easy reading; skip code blocks unless needed.
237
+ 7. Keep user content like guides and examples.
238
+ 8. Add constants like guides and rubrics.
239
+ 9. Clearly state output format including structure and syntax.
240
+
241
+ Only provide the output in the following JSON format enclosed in <json> tags:
242
+
243
+ <json>
244
+ {
245
+ "initial_prompt_evaluation": "Your evaluation of the initial prompt with Strengths and Weaknesses in a string as bullet points format",
246
+ "refined_prompt": "Your refined prompt",
247
+ "explanation_of_refinements": "Explanation of the techniques used and improvements made, also include the extract of final prompt where it made. Answer in bullet points if accurate"
248
+ }
249
+ </json>
250
+ '''
251
+
252
+ new_meta_prompt = '''Given an initial prompt:
253
+
254
+ Initial Prompt: [Insert initial prompt here]
255
+
256
+ follow these steps to refine the prompt:
257
+
258
+ 1. Analyze the prompt:
259
+ - Identify the main topic and key concepts
260
+ - Determine the type of task (e.g., math problem, logical reasoning, factual question)
261
+ - Assess the complexity and any specific requirements
262
+
263
+ 2. Evaluate the initial prompt:
264
+ - Determine its strengths and weaknesses in relation to the query
265
+ - Identify any missing elements or irrelevant information
266
+
267
+ 3. Refine the prompt:
268
+ - Retain relevant parts of the initial prompt
269
+ - Add query-specific context or information
270
+ - Incorporate appropriate reasoning frameworks (e.g., step-by-step, chain-of-thought)
271
+ - Ensure clear instructions for the desired output format
272
+
273
+ 4. Optimize for information flow:
274
+ - Ensure the refined prompt allows for direct use of query information
275
+ - Enable indirect information aggregation through prompt-guided reasoning
276
+
277
+ 5. Enhance versatility:
278
+ - Add elements that encourage flexible thinking and problem-solving
279
+ - Include self-evaluation or error-checking instructions if appropriate
280
+
281
+ 6. Formulate the refined prompt:
282
+ - Combine all elements into a clear, concise, and effective prompt
283
+ - Ensure the language and structure are appropriate for the task
284
+
285
+ 7. Output the refined prompt:
286
+ - Present the new prompt, highlighting key changes or additions
287
+
288
+ Only provide the output in the following JSON format enclosed in <json> tags:
289
+
290
+ <json>
291
+ {
292
+ "initial_prompt_evaluation": "Your evaluation of the initial prompt with Strengths and Weaknesses in a string as bullet points format",
293
+ "refined_prompt": "Your refined prompt",
294
+ "explanation_of_refinements": "Explanation of the techniques used and improvements made, also include the extract of final prompt where it made. Answer in bullet points if accurate"
295
+ }
296
+ </json>
297
+ '''
298
+
299
+ advanced_meta_prompt = '''
300
+ You are an expert in natural language processing and state-of-the-art prompt engineering. Your task is to refine and improve the given prompt using advanced prompt engineering techniques.
301
+
302
+ Initial Prompt: [Insert initial prompt here]
303
+
304
+ Analyze the initial prompt and generate an improved version. Apply various prompt engineering techniques to enhance clarity, specificity, and effectiveness. Consider techniques such as:
305
+
306
+ 1. Clarity and Specificity
307
+ 2. Structural Improvement
308
+ 3. Conciseness
309
+ 4. Ambiguity Resolution
310
+ 5. Instruction Refinement
311
+ 6. Query Focusing
312
+ 7. Task Decomposition
313
+ 8. Role-Playing
314
+ 9. Chain-of-Thought
315
+ 10. Few-Shot Learning
316
+ 11. Constrained Generation
317
+ 12. Metacognitive Prompting
318
+ 13. Contextual Priming
319
+ 14. Negative Prompting
320
+ 15. Output Format Control
321
+ 16. Emotional Tone Setting
322
+ 17. Iterative Refinement
323
+ 18. Analogical Reasoning
324
+ 19. Perspective Shifting
325
+
326
+ Only provide the output in the following JSON format enclosed in <json> tags:
327
+
328
+ <json>
329
+ {
330
+ "initial_prompt_evaluation": "Your evaluation of the initial prompt with Strengths and Weaknesses in a string as bullet points format",
331
+ "refined_prompt": "Your refined prompt",
332
+ "explanation_of_refinements": "Explanation of the techniques used and improvements made, also include the extract of final prompt where it made. Answer in bullet points if accurate"
333
+ }
334
+ </json>
335
+ '''
336
+
337
+ import json
338
+ import re
339
+ import json
340
+ import re
341
+ from huggingface_hub import InferenceClient
342
+ from pydantic import BaseModel, Field
343
+ from typing import Optional, Literal
344
+
345
+ class PromptInput(BaseModel):
346
+ text: str = Field(..., description="The initial prompt text")
347
+ meta_prompt_choice: Literal["physics","morphosis", "verse", "phor","bolism"] = Field(..., description="Choice of meta prompt strategy")
348
+
349
+ class RefinementOutput(BaseModel):
350
+ query_analysis: Optional[str] = None
351
+ initial_prompt_evaluation: Optional[str] = None
352
+ refined_prompt: Optional[str] = None
353
+ explanation_of_refinements: Optional[str] = None
354
+ raw_content: Optional[str] = None
355
+
356
+ class PromptRefiner:
357
+ def __init__(self, api_token: str):
358
+ self.client = InferenceClient(token=api_token)
359
+
360
+ def refine_prompt(self, prompt_input: PromptInput) -> RefinementOutput:
361
+ if prompt_input.meta_prompt_choice == "morphosis":
362
+ selected_meta_prompt = original_meta_prompt
363
+ elif prompt_input.meta_prompt_choice == "verse":
364
+ selected_meta_prompt = new_meta_prompt
365
+ elif prompt_input.meta_prompt_choice == "physics":
366
+ selected_meta_prompt = metaprompt1
367
+ elif prompt_input.meta_prompt_choice == "bolism":
368
+ selected_meta_prompt = loic_metaprompt
369
+ else:
370
+ selected_meta_prompt = advanced_meta_prompt
371
+
372
+ messages = [
373
+ {"role": "system", "content": 'You are an expert at refining and extending prompts. Given a basic prompt, provide a more detailed.'},
374
+ {"role": "user", "content": selected_meta_prompt.replace("[Insert initial prompt here]", prompt_input.text)}
375
+ ]
376
+ response = self.client.chat_completion(
377
+ model="meta-llama/Meta-Llama-3-70B-Instruct",
378
+ messages=messages,
379
+ max_tokens=3000,
380
+ temperature=0.3
381
+ )
382
+ response_content = response.choices[0].message.content.strip()
383
+ try:
384
+ # Extract JSON from between <json> tags
385
+ json_match = re.search(r'<json>\s*(.*?)\s*</json>', response_content, re.DOTALL)
386
+ if json_match:
387
+ json_str = json_match.group(1)
388
+ # Remove newlines and escape quotes within the JSON string
389
+ json_str = re.sub(r'\n\s*', ' ', json_str)
390
+ json_str = json_str.replace('"', '\\"')
391
+ # Wrap the entire string in quotes and parse it
392
+ json_output = json.loads(f'"{json_str}"')
393
+ # Ensure json_output is a dictionary
394
+ if isinstance(json_output, str):
395
+ json_output = json.loads(json_output)
396
+ # Unescape the parsed JSON
397
+ for key, value in json_output.items():
398
+ if isinstance(value, str):
399
+ json_output[key] = value.replace('\\"', '"')
400
+ return RefinementOutput(**json_output, raw_content=response_content)
401
+ else:
402
+ raise ValueError("No JSON found in the response")
403
+ except (json.JSONDecodeError, ValueError) as e:
404
+ print(f"Error parsing JSON: {e}")
405
+ print(f"Raw content: {response_content}")
406
+ # If JSON parsing fails, attempt to extract the content manually
407
+ output = {}
408
+ for key in ["initial_prompt_evaluation", "refined_prompt", "explanation_of_refinements"]:
409
+ pattern = rf'"{key}":\s*"(.*?)"(?:,|\}})'
410
+ match = re.search(pattern, response_content, re.DOTALL)
411
+ if match:
412
+ output[key] = match.group(1).replace('\\n', '\n').replace('\\"', '"')
413
+ else:
414
+ output[key] = "" # Set empty string if content not found
415
+ return RefinementOutput(**output, raw_content=response_content)
416
+
417
+ def apply_prompt(self, prompt: str) -> str:
418
+ try:
419
+ messages = [
420
+ {"role": "system", "content": "You are a helpful assistant. Answer in stylized version with latex format or markdown if relevant. Separate your answer into logical sections using level 2 headers (##) for sections and bolding (**) for subsections.Incorporate a variety of lists, headers, and text to make the answer visually appealing"},
421
+ {"role": "user", "content": prompt}
422
+ ]
423
+
424
+ response = self.client.chat_completion(
425
+ model="meta-llama/Meta-Llama-3-70B-Instruct",
426
+ messages=messages,
427
+ max_tokens=3000, # Increased token limit
428
+ temperature=0.8
429
+ )
430
+
431
+ output = response.choices[0].message.content.strip()
432
+ # Basic post-processing
433
+ output = output.replace('\n\n', '\n').strip()
434
+ return output
435
+ except Exception as e:
436
+ return f"Error: {str(e)}"
437
+
438
+ class GradioInterface:
439
+ def __init__(self, prompt_refiner: PromptRefiner):
440
+ self.prompt_refiner = prompt_refiner
441
+
442
+ with gr.Blocks() as self.interface:
443
+ gr.Markdown("# PROMPT++")
444
+ gr.Markdown("### Refine your Prompts: Learn how to generate an improved version of your prompts")
445
+ gr.Markdown("Enter a main idea for a prompt, choose a meta prompt, and the model will attempt to generate an improved version.")
446
+
447
+ with gr.Row():
448
+ prompt_text = gr.Textbox(label="Type the prompt here")
449
+ with gr.Row():
450
+ meta_prompt_choice = gr.Radio(["physics","morphosis", "verse", "phor","bolism"], label="Choose Meta Prompt", value="morphosis")
451
+ refine_button = gr.Button("Refine Prompt")
452
+ with gr.Row():
453
+ gr.Markdown("### Initial prompt analysis")
454
+ with gr.Column():
455
+ analysis_evaluation = gr.Markdown(label="Analysis and Evaluation")
456
+ gr.Markdown("### Refined Prompt")
457
+ refined_prompt = gr.Textbox(label="Refined Prompt")
458
+ gr.Markdown("### Explanation of Refinements")
459
+ explanation_of_refinements = gr.Markdown(label="Explanation of Refinements")
460
+
461
+ with gr.Accordion("Full Response JSON", open=False):
462
+ full_response_json = gr.JSON()
463
+
464
+ refine_button.click(
465
+ fn=self.refine_prompt,
466
+ inputs=[prompt_text, meta_prompt_choice],
467
+ outputs=[analysis_evaluation, refined_prompt, explanation_of_refinements, full_response_json]
468
+ )
469
+ with gr.Row():
470
+ # apply_model=gr.Dropdown(["meta-llama/Llama-3.1-70B-Instruct",'Qwen/Qwen2.5-72B-Instruct'], value="meta-llama/Llama-3.1-70B-Instruct", label="Model"),
471
+ apply_button = gr.Button("Apply Prompts")
472
+
473
+ with gr.Row():
474
+ with gr.Column():
475
+ gr.Markdown("### Original Prompt Output")
476
+ original_output = gr.Markdown(label="Original Prompt Output")
477
+ with gr.Column():
478
+ gr.Markdown("### Refined Prompt Output")
479
+ refined_output = gr.Markdown(label="Refined Prompt Output")
480
+
481
+ apply_button.click(
482
+ fn=self.apply_prompts,
483
+ inputs=[prompt_text, refined_prompt],
484
+ outputs=[original_output, refined_output]
485
+ )
486
+
487
+ gr.Examples(
488
+ examples=[
489
+ ["Tell me about that guy who invented the light bulb", "physics"],
490
+ ["Explain the universe.", "bolism"],
491
+ ["What's the population of New York City and how tall is the Empire State Building and who was the first mayor?", "morphosis"],
492
+ ["List American presidents.", "verse"],
493
+ ["Write a story.", "bolism"],
494
+ ["Explain why the experiment failed.", "morphosis"],
495
+ ["Is nuclear energy good?", "verse"],
496
+ ["How does a computer work?", "phor"],
497
+ ["How to make money fast?", "morphosis"],
498
+ ["how can you prove IT0's lemma in stochastic calculus ?", "verse"],
499
+ ],
500
+ inputs=[prompt_text, meta_prompt_choice]
501
+ )
502
+
503
+
504
+ def refine_prompt(self, prompt: str, meta_prompt_choice: str) -> tuple:
505
+ input_data = PromptInput(text=prompt, meta_prompt_choice=meta_prompt_choice)
506
+ result = self.prompt_refiner.refine_prompt(input_data)
507
+ analysis_evaluation = f"\n\n{result.initial_prompt_evaluation}"
508
+ return (
509
+ analysis_evaluation,
510
+ result.refined_prompt,
511
+ result.explanation_of_refinements,
512
+ result.dict()
513
+ )
514
+
515
+ def apply_prompts(self,original_prompt: str, refined_prompt: str):
516
+ original_output = self.prompt_refiner.apply_prompt(original_prompt)
517
+ refined_output = self.prompt_refiner.apply_prompt(refined_prompt)
518
+ return original_output, refined_output
519
+
520
+ def launch(self):
521
+ self.interface.launch()
522
+
523
+ # Main code to run the application
524
+ if __name__ == '__main__':
525
+ api_token = os.getenv('HF_API_TOKEN')
526
+ if not api_token:
527
+ raise ValueError("HF_API_TOKEN not found in environment variables")
528
+
529
+ prompt_refiner = PromptRefiner(api_token)
530
+ gradio_interface = GradioInterface(prompt_refiner)
531
+ gradio_interface.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ transformers
2
+ torch
3
+ gradio
4
+ httpx==0.24.1
5
+ accelerate>=0.26.0
6
+ tenacity