File size: 11,080 Bytes
7c0cd5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import gradio as gr
import os
import requests
import json
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

# Get API key from environment variable
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY", "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")

# OpenRouter API endpoint
API_URL = "https://openrouter.ai/api/v1/chat/completions"

# Headers for OpenRouter API
headers = {
    "Authorization": f"Bearer {OPENROUTER_API_KEY}",
    "Content-Type": "application/json",
    "HTTP-Referer": "http://localhost:7860", # Replace with your site URL in production
    "X-Title": "Prompt Engineering Lab" # Updated title for OpenRouter rankings
}

# Available Gemma models on OpenRouter (free tier), ordered from smallest to largest
MODELS = {
    "Google: Gemma 3 1B": "google/gemma-3-1b-it:free",
    "Google: Gemma 3 4B": "google/gemma-3-4b-it:free",
    "Google: Gemma 2 9B": "google/gemma-2-9b-it:free",
    "Google: Gemma 3 12B": "google/gemma-3-12b-it:free",
    "Google: Gemma 3 27B": "google/gemma-3-27b-it:free"
}

# Enhanced prompt templates with better descriptions
PROMPT_TEMPLATES = {
    "None": "{query}",
    "Role-based": "You are an expert in {topic}.\n\n{query}",
    "Step-by-Step": "{query}\n\nThink step by step to solve this problem.",
    "Chain of Thought": "{query}\n\nLet's think through this carefully, reasoning one step at a time.",
    "Few-Shot Learning": """Here are a few examples:

Input: What is 2+2?
Output: The answer is 4.

Input: What is the capital of France?
Output: The capital of France is Paris.

Now answer this question: 

{query}"""
}

# Descriptions of each technique for the UI
TECHNIQUE_DESCRIPTIONS = {
    "None": "Sends your query directly to the model without any prompt engineering technique applied. This serves as a baseline to compare other techniques against.",
    
    "Role-based": "Assigns an expert role to the model, which can improve responses for specific domains by framing the model's perspective. This technique leverages the model's training on expert writing styles.",
    
    "Step-by-Step": "Appends an instruction to think step-by-step at the end of your query, which often improves accuracy on complex problems by encouraging methodical thinking.",
    
    "Chain of Thought": "Appends a request to think carefully with step-by-step reasoning at the end of your query. This approach is particularly effective for complex reasoning tasks.",
    
    "Few-Shot Learning": "Provides examples of the expected format or reasoning before your query, helping the model understand the task through demonstration rather than instruction. This technique is powerful when you need specific output formats or reasoning patterns."
}

def generate_response(query, model, prompt_template, topic="", temperature=0.7, max_tokens=500):
    """Generate a response from the selected model with the selected prompt template."""
    if not OPENROUTER_API_KEY:
        return "⚠️ Please set your OPENROUTER_API_KEY environment variable."
    
    if prompt_template == "Role-based" and not topic:
        topic = "artificial intelligence"
    
    # Format the prompt based on the selected template
    formatted_prompt = PROMPT_TEMPLATES[prompt_template].format(query=query, topic=topic)
    
    # Prepare the payload for the API request
    payload = {
        "model": MODELS[model],
        "messages": [{"role": "user", "content": formatted_prompt}],
        "temperature": temperature,
        "max_tokens": max_tokens
    }
    
    try:
        # Make the API request
        response = requests.post(API_URL, headers=headers, json=payload)
        response.raise_for_status()
        
        # Parse the response
        result = response.json()
        return result["choices"][0]["message"]["content"]
    except Exception as e:
        return f"Error: {str(e)}"

def update_prompt_preview(query, prompt_template, topic=""):
    """Update the prompt preview based on the selected template."""
    if prompt_template == "Role-based" and not topic:
        topic = "artificial intelligence"
    
    return PROMPT_TEMPLATES[prompt_template].format(query=query, topic=topic)

def update_technique_description(prompt_template):
    """Update the technique description based on the selected template."""
    return TECHNIQUE_DESCRIPTIONS[prompt_template]

# Create the Gradio interface
with gr.Blocks(title="Prompt Engineering Interactive Lab") as demo:
    gr.Markdown("# 🧠 Prompt Engineering Interactive Lab")
    gr.Markdown("""
    This interactive lab demonstrates how different prompt engineering techniques can dramatically affect AI outputs.
    
    Experiment with various techniques and see how the same query produces different results based on how you frame your prompt.
    This is a hands-on companion to the blog post ["What is Prompt Engineering?"](https://slyracoon23.github.io/blog/posts/2025-03-15_what_is_prompt_engineering.html)
    """)
    
    with gr.Row():
        with gr.Column(scale=1):
            query_input = gr.Textbox(
                label="Your Query",
                placeholder="Enter your question or task here...",
                lines=3
            )
            
            with gr.Row():
                with gr.Column(scale=1):
                    model_dropdown = gr.Dropdown(
                        choices=list(MODELS.keys()),
                        label="Select Model",
                        value=list(MODELS.keys())[2]  # Default to 9B model for better results
                    )
                
                with gr.Column(scale=1):
                    template_dropdown = gr.Dropdown(
                        choices=list(PROMPT_TEMPLATES.keys()),
                        label="Prompt Technique",
                        value="None"
                    )
            
            topic_input = gr.Textbox(
                label="Topic/Expertise (for Role-based technique)",
                placeholder="e.g., mathematics, history, programming",
                visible=False
            )
            
            temperature_slider = gr.Slider(
                minimum=0.1,
                maximum=1.0,
                value=0.7,
                step=0.1,
                label="Temperature (Creativity vs Precision)",
                info="Lower values = more precise, higher values = more creative"
            )
            
            submit_button = gr.Button("Generate Response", variant="primary")
        
        with gr.Column(scale=1):
            technique_description = gr.Markdown()
            
            prompt_preview = gr.Textbox(
                label="Prompt Preview",
                lines=5,
                interactive=False
            )
            
            response_output = gr.Textbox(
                label="AI Response",
                lines=15,
                interactive=False
            )
    
    # Examples section with real-world prompting scenarios
    gr.Markdown("## Example Prompting Scenarios")
    examples = gr.Examples(
        examples=[
            ["Explain how transformers work in machine learning"],
            ["Compare and contrast renewable energy sources"],
            ["What are three strategies to improve critical thinking?"],
            ["Design a simple algorithm to find duplicate elements in an array"],
            ["What are the ethical implications of AI in healthcare?"]
        ],
        inputs=query_input
    )
    
    # Connect components with events
    template_dropdown.change(
        fn=lambda x: gr.update(visible=(x == "Role-based")),
        inputs=template_dropdown,
        outputs=topic_input
    )
    
    # Update technique description when template changes
    template_dropdown.change(
        fn=update_technique_description,
        inputs=template_dropdown,
        outputs=technique_description
    )
    
    # Update prompt preview when inputs change
    for component in [query_input, template_dropdown, topic_input]:
        component.change(
            fn=update_prompt_preview,
            inputs=[query_input, template_dropdown, topic_input],
            outputs=prompt_preview
        )
    
    # Submit button event
    submit_button.click(
        fn=generate_response,
        inputs=[query_input, model_dropdown, template_dropdown, topic_input, temperature_slider],
        outputs=response_output
    )
    
    # Add explanations of prompt engineering and its impact
    gr.Markdown("""
    ## Understanding Prompt Engineering
    
    Prompt engineering is the practice of crafting inputs to AI systems to elicit desired outputs. It's a key skill for effectively using large language models.
    
    ### Why Prompt Engineering Matters
    
    The same model can produce dramatically different results based solely on how you frame your prompt. This demo lets you experience this firsthand by comparing different techniques:
    
    - **Basic Prompting**: Direct questions yield direct answers, but may lack depth or context
    - **Role-Based Prompting**: Giving the AI a persona or expertise lens changes its perspective
    - **Step-by-Step Reasoning**: Requesting explicit reasoning steps improves accuracy for complex tasks
    - **Chain of Thought**: Extended reasoning that connects concepts leads to more comprehensive answers
    - **Few-Shot Learning**: Showing examples of desired outputs helps the model understand your expectations
    
    ### Experiment Tips
    
    - Try the same query with different techniques to see how responses vary
    - Adjust the temperature to see how it affects output creativity vs. precision
    - For complex questions, compare basic prompting with reasoning-based techniques
    - For domain-specific questions, try role-based prompting with relevant expertise
    
    This demo uses the Google Gemma model family via OpenRouter's API.
    """)
    
    # Instructions for setting up the API key
    gr.Markdown("""
    ## Setup Information
    
    This demo uses the OpenRouter API to access Gemma models. The default API key has limited quota.
    
    For unlimited use:
    1. Sign up at [OpenRouter](https://openrouter.ai/)
    2. Get your API key from the dashboard
    3. Create a `.env` file in this directory with: `OPENROUTER_API_KEY=your_api_key_here`
    """)

# Handle case when API key is not set
if not OPENROUTER_API_KEY:
    demo = gr.Blocks().queue()
    with demo:
        gr.Markdown("# Prompt Engineering Lab - Setup Required")
        gr.Markdown("""
        ## Setup Instructions
        
        To use this demo, you need an OpenRouter API key:
        
        1. Sign up at [OpenRouter](https://openrouter.ai/)
        2. Get your API key from the dashboard
        3. Create a file named `.env` in the same directory as this script with:
           ```
           OPENROUTER_API_KEY=your_api_key_here
           ```
        4. Restart this application
        """)

# Launch the Gradio app
if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)