File size: 9,432 Bytes
339626d ceaab81 339626d ceaab81 64f801f 339626d 85645eb 339626d 85645eb 339626d 85645eb 339626d 4fc4367 4975f8c 85645eb a3947f4 4fc4367 a3947f4 339626d 075635c 339626d 64f801f 99e3b5d aff3b52 99e3b5d aff3b52 99e3b5d aff3b52 99e3b5d 4fc4367 aff3b52 a3947f4 aff3b52 4fc4367 99e3b5d 64f801f 99e3b5d aff3b52 99e3b5d aff3b52 99e3b5d aff3b52 99e3b5d 4fc4367 aff3b52 a3947f4 aff3b52 4fc4367 99e3b5d 339626d 64f801f 339626d 3f2a66a 64f801f 339626d 99e3b5d 64f801f 99e3b5d 64f801f 99e3b5d 64f801f 99e3b5d 64f801f 99e3b5d 339626d 99e3b5d 64f801f 99e3b5d 64f801f 99e3b5d 339626d ceaab81 99e3b5d | 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 | import os
import requests
import json
import gradio as gr
from textblob import TextBlob
from groq import Groq
# Tool 1: One Shot Optimization
def one_shot_optimization(user_input: str) -> str:
"""
Receives an initial prompt idea to be optimized. A prompt writing tone may be provided.
Args:
user_input (str): The initial prompt idea to be optimized. May optionally include a writing tone specification (e.g., 'Standard writing tone', 'Technical writing tone') if provided.
Returns:
A single string containing the optimized prompt.
"""
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
resp = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[
{
"role": "system",
"content": (
"You are a prompt-engineering expert."
"The user will give you a simple prompt, and you must rewrite and optimize it for clarity, "
"specificity, and better results. "
"If the user specifies a writing tone (e.g., 'Standard writing tone', "
"'Technical writing tone'), incorporate that tone into the optimized prompt. "
"If no tone is specified, use a clear, professional tone. "
"Always make the final optimized prompt more specific, actionable, and likely to produce "
"high-quality responses. "
"IMPORTANT: Your final response must contain ONLY the final optimized prompt "
"with no additional commentary or explanation. "
"If the user writes to you in English, your final response must be written in English; If the user writes to you in Brazilian Portuguese, your final response must be written in Brazilian Portuguese."
)
},
{"role": "user", "content": user_input},
],
)
return resp.choices[0].message.content
# Tool 2: Five Questions Analysis and Follow-Up Generation
def five_questions_analysis_and_followup_generation(user_input: str) -> str:
"""
Analyzes 5 question-answer pairs about a user's prompt requirements and generates 3 strategic follow-up questions to gather additional context for future better prompt creation.
Args:
user_input (str): A string containing exactly 5 questions and their corresponding answers related to the user's prompt requirements. Format should be clear Questioin and Answer pairs. Must not be empty.
Returns:
Three numbered follow-up questions (e.g.: 1. Foo...?, 2. Foo...?, 3. Foo...?) designed to elicit additional context that will improve future prompt creation. No additional commentary included.
"""
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
resp = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[
{
"role": "system",
"content": (
"You are a prompt-engineering expert specializing in context analysis."
"The user will provide you with 5 questions and answers about a subject "
"they want to create a prompt for. "
"Your task is to analyze these question-answer pairs to understand the user's needs, "
"then generate exactly 3 strategic follow-up questions that will gather "
"the most valuable additional context for creating an optimal prompt. "
"Focus on identifying gaps in information about scope, constraints, audience, "
"desired outcomes, or specific requirements that weren't covered in the original 5 questions. "
"Format your response as a numbered list (e.g.: 1. Foo...?, 2. Foo...?, 3. Foo...?). "
"Make questions specific, actionable, and directly relevant a future to prompt creation. "
"If the user answers the 5 initial questions in Brazilian Portuguese, your final response must be written in Brazilian Portuguese. "
"If the user answers the 5 initial questions in English, your final response must be written in English. "
"IMPORTANT: Your final response must contain ONLY the 3 numbered follow-up questions "
"with no additional commentary, analysis, or explanation. "
"Example format: "
"1. What specific ...? "
"2. Are there any ...? "
"3. What does ...? "
)
},
{"role": "user", "content": user_input},
],
)
return resp.choices[0].message.content
# Tool 3: Eight Questions Analysis and Prompt Generation
def eight_questions_analysis_and_prompt_generation(user_input: str) -> str:
"""
Analyzes 8 question-answer pairs to understand user requirements and generates a comprehensive, ready-to-use prompt for commercial LLM platforms.
Args:
user_input (str): A string containing exactly 8 questions and their corresponding answers that define the user's requirements, context, and desired outcomes. Format should be clear Questioin and Answer pairs. Must not be empty.
Returns:
A complete, optimized prompt that synthesizes all provided information into a clear, actionable request ready for copy-paste use with commercial LLMs (ChatGPT, Claude, etc.). No additional commentary included.
"""
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
resp = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[
{
"role": "system",
"content": (
"You are a prompt-engineering expert specialized in comprehensive prompt creation."
"The user will provide you with 8 questions and answers that define their specific "
"requirements, context, constraints, and desired outcomes. "
"Your task is to analyze these Questioin and Answer pairs thoroughly to understand the user's "
"complete request, then craft a detailed, well-structured prompt that captures "
"all essential information. "
"The final prompt should be optimized for commercial web-based LLMs (like ChatGPT, "
"Claude, Gemini, etc.) and include: clear instructions, relevant context, "
"specific requirements, and any constraints mentioned. "
"Make the prompt self-contained so the user can copy and paste it directly "
"without needing to provide additional context. "
"Structure the prompt logically with clear sections if needed, and ensure "
"it's specific enough to generate high-quality, targeted responses. "
"If the user writes to you in English, your final response must be written in English; If the user writes answers in Brazilian Portuguese, your final response must be written in Brazilian Portuguese."
"IMPORTANT: Your final response must contain ONLY the final optimized prompt "
"with no introduction, explanation, or additional commentary. The prompt "
"should be ready for immediate use. "
)
},
{"role": "user", "content": user_input},
],
)
return resp.choices[0].message.content
# --- Gradio Interface Setup ---
# Prompt Optimization interface
one_shot_optimization_interface = gr.Interface(
fn=one_shot_optimization,
inputs=gr.Textbox(lines=5, placeholder="Enter prompt to optimize..."),
outputs=gr.Textbox(),
api_name="one_shot_optimization",
title="Prompt Optimization Tool",
description="Rewrites user prompts to improve results from commercial LLMs."
)
# Five Questions Analysis and Follow‑Up interface
five_questions_analysis_and_followup_generation_interface = gr.Interface(
fn=five_questions_analysis_and_followup_generation,
inputs=gr.Textbox(lines=5, placeholder="Enter 5 questions and answers, one per line..."),
outputs=gr.Textbox(),
api_name="five_questions_analysis_and_followup_generation",
title="Five Questions Analysis and Follow‑Up Tool",
description="Analyzes 5 questions and answers and generates 3 follow‑up questions."
)
# Final Prompt Analysis interface
eight_questions_analysis_and_prompt_generation_interface = gr.Interface(
fn=eight_questions_analysis_and_prompt_generation,
inputs=gr.Textbox(lines=8, placeholder="Enter 8 questions and answers, one per line..."),
outputs=gr.Textbox(),
api_name="eight_questions_analysis_and_prompt_generation",
title="Final Prompt Analysis Tool",
description="Generates an optimized prompt based on 8 questions and answers."
)
# Combine into a tabbed demo
demo = gr.TabbedInterface(
[
one_shot_optimization_interface,
five_questions_analysis_and_followup_generation_interface,
eight_questions_analysis_and_prompt_generation_interface
],
[
"One Shot Optimization",
"Five Questions Analysis and Follow-Up Generation",
"Eight Questions Analysis and Prompt Generation"
]
)
if __name__ == "__main__":
demo.launch(mcp_server=True)
|