π§ AI with Python β ποΈ Building a Prompt Playground
Posted on: October 8, 2026
Description:
Prompt Engineering is rarely a one-time process. A prompt that looks good on paper may produce a completely different response once it is sent to a Large Language Model. Small changes in instructions, context, or generation settings can influence the final output.
During development, repeatedly changing a prompt inside a Python script and running the entire program becomes inconvenient.
A Prompt Playground provides a better approach. It gives us an interactive environment where we can modify prompts, adjust model settings, generate responses, and quickly observe how the model behaves.
In this project, we'll build a simple Prompt Playground using Python, Hugging Face Transformers, and Streamlit.
What is a Prompt Playground?
A Prompt Playground is an interactive interface designed for experimenting with prompts and LLM configuration.
Instead of defining a fixed prompt such as:
prompt = "Explain Machine Learning to a beginner."
we provide a text editor where the prompt can be changed directly.
The basic experimentation cycle becomes:
Write Prompt β Configure Model β Generate β Review β Modify β Repeat
This makes it much easier to compare different Prompt Engineering approaches without constantly editing the underlying application.
Building the Interface with Streamlit
Streamlit allows us to turn a Python script into a simple web application without building a separate frontend.
We can create our prompt editor with just a few lines:
prompt = st.text_area(
"Enter your prompt",
value="Explain Machine Learning to a beginner.",
height=180
)
Now the prompt is no longer hardcoded into the application.
We could start with a basic instruction, generate a response, modify the instruction to include a specific role or audience, and immediately generate another response.
For example:
Explain neural networks.
could become:
You are a Machine Learning instructor.
Explain neural networks to a complete beginner using simple language and one real-world analogy.
The playground makes it easy to observe how those additional instructions affect the result.
Experimenting with Generation Settings
The prompt itself isn't the only factor that affects model output. Generation parameters can also change how responses are produced.
Two useful controls are maximum new tokens and temperature.
max_tokens = st.slider(
"Maximum New Tokens",
min_value=20,
max_value=300,
value=120
)
temperature = st.slider(
"Temperature",
min_value=0.1,
max_value=1.5,
value=0.7,
step=0.1
)
max_new_tokens limits how many new tokens the model can generate.
temperature affects randomness when sampling is enabled. Lower temperatures generally make the model's token choices more concentrated, while higher temperatures allow more variation.
This is useful when comparing tasks with different requirements. A classification or extraction task may benefit from more predictable generation, while brainstorming or creative tasks may benefit from greater variation.
Generating the Response
Once the prompt and settings are ready, the playground can send them to our Hugging Face model.
if st.button("Generate Response"):
response = generator(
prompt,
max_new_tokens=max_tokens,
temperature=temperature,
do_sample=True
)
st.write(response[0]["generated_text"])
Now experimentation becomes interactive.
Change the prompt, adjust a parameter, click the button, inspect the result, and repeat.
For larger models, caching the loaded model with st.cache_resource is also important because Streamlit reruns the script when widgets change. Caching prevents the model from being unnecessarily loaded again during every interaction.
Bringing Prompt Engineering Techniques Together
The playground is particularly useful because it gives us one place to experiment with the techniques we've already explored throughout this Prompt Engineering track.
We can test:
Zero-shot prompting by giving the model only an instruction.
Few-shot prompting by adding examples before the actual task.
Role-based prompting by assigning the model a specific role and audience.
Structured output prompts by requesting JSON or another predictable format.
We can also experiment with prompt templates and multi-step instructions before integrating them into larger Prompt Chaining, RAG, or AI-agent workflows.
Instead of treating these techniques as isolated examples, the playground becomes a practical environment for comparing them.
Why Prompt Playgrounds Matter
Prompt Playgrounds are especially useful during the prototyping stage of an LLM application.
Developers can quickly test different instructions, compare outputs, explore model behaviour, and identify promising prompt designs before embedding them inside application code.
They are useful for projects involving AI assistants, document processing, content generation, RAG systems, information extraction, internal AI tools, and automated LLM workflows.
However, manually testing a few prompts is not the same as evaluating them systematically.
A prompt that performs well for one example may fail on another. Production systems therefore need larger test datasets, defined evaluation criteria, and repeatable testing rather than relying entirely on visual inspection.
Final Thoughts
Building a Prompt Playground turns Prompt Engineering into a much more interactive process. With Python, Streamlit, and Hugging Face Transformers, we can create a simple environment where prompts can be edited, model parameters adjusted, and responses inspected immediately.
More importantly, the playground brings together many of the techniques we've explored in this seriesβfrom zero-shot and few-shot prompting to prompt templates, structured outputs, role-based prompts, and prompt chaining.
It is an excellent tool for experimentation and prototyping, but finding a promising prompt is only the beginning. Before an LLM workflow reaches production, we also need a reliable way to determine whether one prompt actually performs better than another.
Code Snippet:
import streamlit as st
from transformers import pipeline
# =========================================================
# Page Configuration
# =========================================================
st.set_page_config(
page_title="Prompt Playground",
page_icon="π§ ",
layout="wide"
)
# =========================================================
# Load Instruction-Following Model
# =========================================================
@st.cache_resource
def load_model():
return pipeline(
"text-generation",
model="google/gemma-2b-it"
)
generator = load_model()
# =========================================================
# Application Header
# =========================================================
st.title("π§ Prompt Playground")
st.write("Experiment with prompts and generation settings using an instruction-following language model.")
# =========================================================
# Prompt Presets
# =========================================================
presets = {
"Custom": "",
"Explain a Concept": "Explain Machine Learning to a complete beginner using simple language and one real-world example.",
"Summarize Text": "Summarize the following text in three concise bullet points:\n\n",
"Generate Ideas": "Generate five creative project ideas for learning Python and Artificial Intelligence.",
"Classify Text": "Classify the following customer message as Positive, Negative, or Neutral.\n\nMessage: "
}
preset = st.selectbox(
"Prompt Preset",
list(presets.keys())
)
# =========================================================
# Initialize Prompt
# =========================================================
if "selected_preset" not in st.session_state:
st.session_state.selected_preset = preset
if "prompt_text" not in st.session_state:
st.session_state.prompt_text = presets[preset]
if preset != st.session_state.selected_preset:
st.session_state.prompt_text = presets[preset]
st.session_state.selected_preset = preset
# =========================================================
# Prompt Editor
# =========================================================
st.text_area(
"Enter your prompt",
key="prompt_text",
height=220,
placeholder="Write your prompt here..."
)
# =========================================================
# Generation Settings
# =========================================================
st.subheader("Generation Settings")
col1, col2 = st.columns(2)
with col1:
max_tokens = st.slider(
"Maximum New Tokens",
min_value=20,
max_value=300,
value=120,
step=10
)
with col2:
temperature = st.slider(
"Temperature",
min_value=0.1,
max_value=1.5,
value=0.7,
step=0.1
)
# =========================================================
# Generate Model Response
# =========================================================
if st.button("Generate Response", type="primary"):
prompt = st.session_state.prompt_text.strip()
if not prompt:
st.warning("Please enter a prompt before generating a response.")
else:
with st.spinner("Generating response..."):
response = generator(
prompt,
max_new_tokens=max_tokens,
temperature=temperature,
do_sample=True,
return_full_text=False
)
output = response[0]["generated_text"]
st.session_state.last_prompt = prompt
st.session_state.last_output = output
st.session_state.last_temperature = temperature
st.session_state.last_max_tokens = max_tokens
# =========================================================
# Display Generated Response
# =========================================================
if "last_output" in st.session_state:
st.subheader("Generated Response")
st.write(st.session_state.last_output)
# =========================================================
# Display Current Experiment
# =========================================================
if "last_output" in st.session_state:
st.subheader("Experiment Details")
details_col1, details_col2 = st.columns(2)
with details_col1:
st.metric(
"Temperature",
st.session_state.last_temperature
)
with details_col2:
st.metric(
"Maximum New Tokens",
st.session_state.last_max_tokens
)
with st.expander("View Prompt"):
st.write(st.session_state.last_prompt)
# =========================================================
# Prompt Comparison
# =========================================================
st.divider()
st.subheader("π Compare Prompt Variations")
st.write("Enter two versions of a prompt and compare how the model responds.")
comparison_col1, comparison_col2 = st.columns(2)
with comparison_col1:
prompt_a = st.text_area(
"Prompt A",
value="Explain neural networks.",
height=160
)
with comparison_col2:
prompt_b = st.text_area(
"Prompt B",
value="You are a Machine Learning instructor. Explain neural networks to a complete beginner using simple language and one real-world analogy.",
height=160
)
# =========================================================
# Generate Comparison Responses
# =========================================================
if st.button("Compare Prompts"):
if not prompt_a.strip() or not prompt_b.strip():
st.warning("Please enter both prompts before comparing them.")
else:
with st.spinner("Generating comparison responses..."):
response_a = generator(
prompt_a,
max_new_tokens=max_tokens,
temperature=temperature,
do_sample=True,
return_full_text=False
)
response_b = generator(
prompt_b,
max_new_tokens=max_tokens,
temperature=temperature,
do_sample=True,
return_full_text=False
)
st.session_state.comparison_a = response_a[0]["generated_text"]
st.session_state.comparison_b = response_b[0]["generated_text"]
# =========================================================
# Display Comparison
# =========================================================
if "comparison_a" in st.session_state and "comparison_b" in st.session_state:
result_col1, result_col2 = st.columns(2)
with result_col1:
st.markdown("### Response A")
st.write(st.session_state.comparison_a)
with result_col2:
st.markdown("### Response B")
st.write(st.session_state.comparison_b)
# =========================================================
# Playground Information
# =========================================================
st.divider()
st.subheader("Prompt Engineering Techniques")
st.write("""
Use the playground to experiment with:
- Zero-shot prompting
- Few-shot prompting
- Prompt templates
- Structured output generation
- Role-based prompting
- Prompt chaining
- Different generation settings
""")
# =========================================================
# Application Footer
# =========================================================
st.divider()
st.caption("AI with Python β Prompt Engineering | python_scripts")
No comments yet. Be the first to comment!