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)