Slyracoon23 commited on
Commit
7c0cd5f
·
1 Parent(s): a473d96

add git submodule gradio app

Browse files
Files changed (3) hide show
  1. README.md +63 -1
  2. app.py +272 -0
  3. requirements.txt +3 -0
README.md CHANGED
@@ -9,4 +9,66 @@ app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  pinned: false
10
  ---
11
 
12
+ # 🧠 Prompt Engineering Interactive Lab
13
+
14
+ An interactive demo that showcases how different prompt engineering techniques affect AI outputs when working with large language models.
15
+
16
+ ## About
17
+
18
+ This application demonstrates various prompt engineering techniques using Google's Gemma models via OpenRouter API. Users can experiment with the same query across different prompt techniques to understand how the framing of a prompt dramatically impacts the quality and nature of AI responses.
19
+
20
+ ## Features
21
+
22
+ - Compare multiple prompt engineering techniques side-by-side
23
+ - Choose from various Gemma model sizes (1B to 27B parameters)
24
+ - Adjust temperature to control creativity vs precision
25
+ - Real-time prompt preview
26
+ - Detailed explanations of each technique
27
+
28
+ ## Prompt Techniques Included
29
+
30
+ - **Basic Prompting**: Direct questions without additional context
31
+ - **Role-Based Prompting**: Assigning expertise personas to the model
32
+ - **Step-by-Step Reasoning**: Requesting methodical thinking
33
+ - **Chain of Thought**: Encouraging careful, sequential reasoning
34
+ - **Few-Shot Learning**: Demonstrating examples before the actual prompt
35
+
36
+ ## Setup
37
+
38
+ 1. Install the required dependencies:
39
+ ```
40
+ pip install -r requirements.txt
41
+ ```
42
+
43
+ 2. (Optional) Set up your own OpenRouter API key:
44
+ - Sign up at [OpenRouter](https://openrouter.ai/)
45
+ - Create a `.env` file with your API key:
46
+ ```
47
+ OPENROUTER_API_KEY=your_api_key_here
48
+ ```
49
+ - A default API key is provided but has limited quota
50
+
51
+ 3. Run the application:
52
+ ```
53
+ python prompt_engineering_demo.py
54
+ ```
55
+
56
+ 4. Open your browser at `http://localhost:7860`
57
+
58
+ ## Usage
59
+
60
+ 1. Enter your query in the text box
61
+ 2. Select a Gemma model size
62
+ 3. Choose a prompt technique
63
+ 4. Adjust the temperature setting if desired
64
+ 5. Click "Generate Response"
65
+ 6. View the formatted prompt and resulting AI response
66
+ 7. Try the same query with different techniques to compare outcomes
67
+
68
+ ## Related Resources
69
+
70
+ This demo is a companion to the blog post ["What is Prompt Engineering?"](https://slyracoon23.github.io/blog/posts/2025-03-15_what_is_prompt_engineering.html)
71
+
72
+ ## License
73
+
74
+ [Add your license information here]
app.py ADDED
@@ -0,0 +1,272 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import requests
4
+ import json
5
+ from dotenv import load_dotenv
6
+
7
+ # Load environment variables from .env file
8
+ load_dotenv()
9
+
10
+ # Get API key from environment variable
11
+ OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY", "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")
12
+
13
+ # OpenRouter API endpoint
14
+ API_URL = "https://openrouter.ai/api/v1/chat/completions"
15
+
16
+ # Headers for OpenRouter API
17
+ headers = {
18
+ "Authorization": f"Bearer {OPENROUTER_API_KEY}",
19
+ "Content-Type": "application/json",
20
+ "HTTP-Referer": "http://localhost:7860", # Replace with your site URL in production
21
+ "X-Title": "Prompt Engineering Lab" # Updated title for OpenRouter rankings
22
+ }
23
+
24
+ # Available Gemma models on OpenRouter (free tier), ordered from smallest to largest
25
+ MODELS = {
26
+ "Google: Gemma 3 1B": "google/gemma-3-1b-it:free",
27
+ "Google: Gemma 3 4B": "google/gemma-3-4b-it:free",
28
+ "Google: Gemma 2 9B": "google/gemma-2-9b-it:free",
29
+ "Google: Gemma 3 12B": "google/gemma-3-12b-it:free",
30
+ "Google: Gemma 3 27B": "google/gemma-3-27b-it:free"
31
+ }
32
+
33
+ # Enhanced prompt templates with better descriptions
34
+ PROMPT_TEMPLATES = {
35
+ "None": "{query}",
36
+ "Role-based": "You are an expert in {topic}.\n\n{query}",
37
+ "Step-by-Step": "{query}\n\nThink step by step to solve this problem.",
38
+ "Chain of Thought": "{query}\n\nLet's think through this carefully, reasoning one step at a time.",
39
+ "Few-Shot Learning": """Here are a few examples:
40
+
41
+ Input: What is 2+2?
42
+ Output: The answer is 4.
43
+
44
+ Input: What is the capital of France?
45
+ Output: The capital of France is Paris.
46
+
47
+ Now answer this question:
48
+
49
+ {query}"""
50
+ }
51
+
52
+ # Descriptions of each technique for the UI
53
+ TECHNIQUE_DESCRIPTIONS = {
54
+ "None": "Sends your query directly to the model without any prompt engineering technique applied. This serves as a baseline to compare other techniques against.",
55
+
56
+ "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.",
57
+
58
+ "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.",
59
+
60
+ "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.",
61
+
62
+ "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."
63
+ }
64
+
65
+ def generate_response(query, model, prompt_template, topic="", temperature=0.7, max_tokens=500):
66
+ """Generate a response from the selected model with the selected prompt template."""
67
+ if not OPENROUTER_API_KEY:
68
+ return "⚠️ Please set your OPENROUTER_API_KEY environment variable."
69
+
70
+ if prompt_template == "Role-based" and not topic:
71
+ topic = "artificial intelligence"
72
+
73
+ # Format the prompt based on the selected template
74
+ formatted_prompt = PROMPT_TEMPLATES[prompt_template].format(query=query, topic=topic)
75
+
76
+ # Prepare the payload for the API request
77
+ payload = {
78
+ "model": MODELS[model],
79
+ "messages": [{"role": "user", "content": formatted_prompt}],
80
+ "temperature": temperature,
81
+ "max_tokens": max_tokens
82
+ }
83
+
84
+ try:
85
+ # Make the API request
86
+ response = requests.post(API_URL, headers=headers, json=payload)
87
+ response.raise_for_status()
88
+
89
+ # Parse the response
90
+ result = response.json()
91
+ return result["choices"][0]["message"]["content"]
92
+ except Exception as e:
93
+ return f"Error: {str(e)}"
94
+
95
+ def update_prompt_preview(query, prompt_template, topic=""):
96
+ """Update the prompt preview based on the selected template."""
97
+ if prompt_template == "Role-based" and not topic:
98
+ topic = "artificial intelligence"
99
+
100
+ return PROMPT_TEMPLATES[prompt_template].format(query=query, topic=topic)
101
+
102
+ def update_technique_description(prompt_template):
103
+ """Update the technique description based on the selected template."""
104
+ return TECHNIQUE_DESCRIPTIONS[prompt_template]
105
+
106
+ # Create the Gradio interface
107
+ with gr.Blocks(title="Prompt Engineering Interactive Lab") as demo:
108
+ gr.Markdown("# 🧠 Prompt Engineering Interactive Lab")
109
+ gr.Markdown("""
110
+ This interactive lab demonstrates how different prompt engineering techniques can dramatically affect AI outputs.
111
+
112
+ Experiment with various techniques and see how the same query produces different results based on how you frame your prompt.
113
+ 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)
114
+ """)
115
+
116
+ with gr.Row():
117
+ with gr.Column(scale=1):
118
+ query_input = gr.Textbox(
119
+ label="Your Query",
120
+ placeholder="Enter your question or task here...",
121
+ lines=3
122
+ )
123
+
124
+ with gr.Row():
125
+ with gr.Column(scale=1):
126
+ model_dropdown = gr.Dropdown(
127
+ choices=list(MODELS.keys()),
128
+ label="Select Model",
129
+ value=list(MODELS.keys())[2] # Default to 9B model for better results
130
+ )
131
+
132
+ with gr.Column(scale=1):
133
+ template_dropdown = gr.Dropdown(
134
+ choices=list(PROMPT_TEMPLATES.keys()),
135
+ label="Prompt Technique",
136
+ value="None"
137
+ )
138
+
139
+ topic_input = gr.Textbox(
140
+ label="Topic/Expertise (for Role-based technique)",
141
+ placeholder="e.g., mathematics, history, programming",
142
+ visible=False
143
+ )
144
+
145
+ temperature_slider = gr.Slider(
146
+ minimum=0.1,
147
+ maximum=1.0,
148
+ value=0.7,
149
+ step=0.1,
150
+ label="Temperature (Creativity vs Precision)",
151
+ info="Lower values = more precise, higher values = more creative"
152
+ )
153
+
154
+ submit_button = gr.Button("Generate Response", variant="primary")
155
+
156
+ with gr.Column(scale=1):
157
+ technique_description = gr.Markdown()
158
+
159
+ prompt_preview = gr.Textbox(
160
+ label="Prompt Preview",
161
+ lines=5,
162
+ interactive=False
163
+ )
164
+
165
+ response_output = gr.Textbox(
166
+ label="AI Response",
167
+ lines=15,
168
+ interactive=False
169
+ )
170
+
171
+ # Examples section with real-world prompting scenarios
172
+ gr.Markdown("## Example Prompting Scenarios")
173
+ examples = gr.Examples(
174
+ examples=[
175
+ ["Explain how transformers work in machine learning"],
176
+ ["Compare and contrast renewable energy sources"],
177
+ ["What are three strategies to improve critical thinking?"],
178
+ ["Design a simple algorithm to find duplicate elements in an array"],
179
+ ["What are the ethical implications of AI in healthcare?"]
180
+ ],
181
+ inputs=query_input
182
+ )
183
+
184
+ # Connect components with events
185
+ template_dropdown.change(
186
+ fn=lambda x: gr.update(visible=(x == "Role-based")),
187
+ inputs=template_dropdown,
188
+ outputs=topic_input
189
+ )
190
+
191
+ # Update technique description when template changes
192
+ template_dropdown.change(
193
+ fn=update_technique_description,
194
+ inputs=template_dropdown,
195
+ outputs=technique_description
196
+ )
197
+
198
+ # Update prompt preview when inputs change
199
+ for component in [query_input, template_dropdown, topic_input]:
200
+ component.change(
201
+ fn=update_prompt_preview,
202
+ inputs=[query_input, template_dropdown, topic_input],
203
+ outputs=prompt_preview
204
+ )
205
+
206
+ # Submit button event
207
+ submit_button.click(
208
+ fn=generate_response,
209
+ inputs=[query_input, model_dropdown, template_dropdown, topic_input, temperature_slider],
210
+ outputs=response_output
211
+ )
212
+
213
+ # Add explanations of prompt engineering and its impact
214
+ gr.Markdown("""
215
+ ## Understanding Prompt Engineering
216
+
217
+ 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.
218
+
219
+ ### Why Prompt Engineering Matters
220
+
221
+ 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:
222
+
223
+ - **Basic Prompting**: Direct questions yield direct answers, but may lack depth or context
224
+ - **Role-Based Prompting**: Giving the AI a persona or expertise lens changes its perspective
225
+ - **Step-by-Step Reasoning**: Requesting explicit reasoning steps improves accuracy for complex tasks
226
+ - **Chain of Thought**: Extended reasoning that connects concepts leads to more comprehensive answers
227
+ - **Few-Shot Learning**: Showing examples of desired outputs helps the model understand your expectations
228
+
229
+ ### Experiment Tips
230
+
231
+ - Try the same query with different techniques to see how responses vary
232
+ - Adjust the temperature to see how it affects output creativity vs. precision
233
+ - For complex questions, compare basic prompting with reasoning-based techniques
234
+ - For domain-specific questions, try role-based prompting with relevant expertise
235
+
236
+ This demo uses the Google Gemma model family via OpenRouter's API.
237
+ """)
238
+
239
+ # Instructions for setting up the API key
240
+ gr.Markdown("""
241
+ ## Setup Information
242
+
243
+ This demo uses the OpenRouter API to access Gemma models. The default API key has limited quota.
244
+
245
+ For unlimited use:
246
+ 1. Sign up at [OpenRouter](https://openrouter.ai/)
247
+ 2. Get your API key from the dashboard
248
+ 3. Create a `.env` file in this directory with: `OPENROUTER_API_KEY=your_api_key_here`
249
+ """)
250
+
251
+ # Handle case when API key is not set
252
+ if not OPENROUTER_API_KEY:
253
+ demo = gr.Blocks().queue()
254
+ with demo:
255
+ gr.Markdown("# Prompt Engineering Lab - Setup Required")
256
+ gr.Markdown("""
257
+ ## Setup Instructions
258
+
259
+ To use this demo, you need an OpenRouter API key:
260
+
261
+ 1. Sign up at [OpenRouter](https://openrouter.ai/)
262
+ 2. Get your API key from the dashboard
263
+ 3. Create a file named `.env` in the same directory as this script with:
264
+ ```
265
+ OPENROUTER_API_KEY=your_api_key_here
266
+ ```
267
+ 4. Restart this application
268
+ """)
269
+
270
+ # Launch the Gradio app
271
+ if __name__ == "__main__":
272
+ demo.launch(server_name="0.0.0.0", server_port=7860)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ requests
3
+ python-dotenv