😂 Joke Generator
GPT OSS 120B via OpenAI API
Concept
This demo asks an LLM to tell a joke about a topic you supply. It uses the
OpenAI API as the inference provider to hit an open-source model endpoint.
Note: the python code uses historical variable names like get_groq_client.
The request uses a high temperature (1.3) so the model leans toward creative,
varied output instead of repeating the same few jokes it defaults to at lower temperatures.
Theory & Concepts
Unleashing Creativity with Temperature and Persona
This Joke Generator demo relies on two key concepts to produce varied and funny responses:
- High Temperature for Variety: Language models predict the next word based on probabilities. By default, they pick the most likely word. In this demo, we intentionally set the
temperatureto a high value of 1.3 in the API call. This forces the model to take creative risks and pick less obvious words, preventing it from repeating the exact same jokes when you ask for a joke about "dogs" multiple times. - Role-Based Prompting: We use the
systemrole to define the model's persona, telling it: "You are a comedian with a huge repertoire of jokes. Never repeat the same joke twice." This anchors its behavior. Then, we pass your specific topic in theuserprompt: "Tell me a joke about {subject}. Just the joke, no commentary." This combination ensures you get exactly what you asked for—a raw joke without unnecessary introductory text.
Request flow
🧑 Browser
Type a topic, click “Tell me a joke”
→
POST /joke
Flask route
→
get_joke()
Builds the prompt
→
OpenAI API
GPT OSS 120B generates the joke
→
🧑 Browser
Joke rendered in the result box
Code flow
flowchart TD
A[Browser
topic] -->|POST /joke| B[app.py
joke route] B -->|topic| C[joke.py
get_joke] C -->|prompt + temperature| D[OpenAI API
GPT OSS 120B] D -->|joke text| C C -->|joke text| B B -->|JSON result| A
topic] -->|POST /joke| B[app.py
joke route] B -->|topic| C[joke.py
get_joke] C -->|prompt + temperature| D[OpenAI API
GPT OSS 120B] D -->|joke text| C C -->|joke text| B B -->|JSON result| A
Backend
Generates the joke via OpenAI API's chat completions API.from config import get_groq_client
# GPT OSS 120B via OpenAI API: fast inference.
JOKE_MODEL = "openai/gpt-oss-120b"
def get_joke(topic: str = "") -> str:
# Fall back to "random" instead of sending an empty string to the model.
subject = topic.strip() if topic.strip() else "random"
client = get_groq_client()
response = client.chat.completions.create(
model=JOKE_MODEL,
temperature=1.3, # high temperature = more creative, varied jokes
messages=[
{
"role": "system",
# Persona + "never repeat" nudge the model toward variety.
"content": "You are a comedian with a huge repertoire of jokes. Never repeat the same joke twice.",
},
{
"role": "user",
# Ask for just the joke so the UI can display it directly.
"content": f"Tell me a joke about {subject}. Just the joke, no commentary.",
},
],
)
# choices[0] is the first (and only) completion; .message.content is the
# assistant's reply text.
return response.choices[0].message.content
API route
Exposes the joke generator over HTTP asPOST /joke.
@bp.route("/joke", methods=["POST"])
def joke():
data = request.get_json(force=True)
# topic is optional; an empty string makes get_joke() pick randomly.
topic = (data.get("topic") or "").strip()
try:
text = get_joke(topic)
response = jsonify({"result": text})
# Every click should fetch a fresh joke — never serve a cached one.
response.headers["Cache-Control"] = "no-store"
return response
except Exception as e:
return jsonify({"error": str(e)}), 500