Context, personality & temperature
Here's something surprising: AI models are stateless. They don't remember anything between calls. If a customer says "I'm allergic to nuts" and then asks "What can I order?", the AI won't remember the allergy unless you send the full conversation history every time.
Stateless AI vs conversation Context
Without context, each call is independent. With context, AI can reference previous messages.
# Build conversation history manually
conversation_history = [
"Customer: Hi, I'm looking for healthy lunch options.",
"Bot: We have salads, grain bowls, and smoothies!",
"Customer: I'd like something with protein."
]
# Send the FULL context with each request
full_context = "\n".join(conversation_history) + "\n"
response = generate_text(full_context)
# The AI now knows the customer wants
# healthy + protein-rich options!The trick is simple: concatenate the entire conversation history and send it with every request. This is called the "context window".
Every model has a context window limit (e.g., 128K tokens for GPT-4o). When conversations get very long, you need strategies like summarizing older messages or using RAG (Retrieval-Augmented Generation) to handle it. We'll explore these in more advanced workshops!
question = "What is a Python decorator?"
# Personality 1: Friendly tutor
friendly = "You are a friendly coding tutor. Explain concepts simply."
print(generate_text(question, system_message=friendly))
# Output: "Hey there! A decorator is like gift-wrapping
# a function that adds extra behavior..."
# Personality 2: Senior engineer
professional = "You are a senior software engineer. Be concise."
print(generate_text(question, system_message=professional))
# Output: "A decorator is a callable that takes a function
# as input, wraps it, and returns a modified function..."System instructions define the AI's personality and constraints. Same question, completely different answers.
Temperature & creativity control
Temperature controls randomness in AI responses: - Low (0.0 to 0.3): Consistent, focused, deterministic. Great for customer support, code generation, data extraction. - Medium (0.4 to 0.7): Balanced. Good for general conversation. - High (0.8 to 1.0): Creative, diverse, unpredictable. Great for brainstorming, marketing copy, creative writing.
prompt = "Create a unique name for a plant-based burger"
# Low temperature: similar results every time
for i in range(3):
print(generate_text(prompt, temperature=0.2))
# "Bloom Burger", "Bloom Burger", "Bloom Burger"
# High temperature: different creative results
for i in range(3):
print(generate_text(prompt, temperature=0.9))
# "Terra Firma Feast", "Sprout Smash", "Earth & Ember"Low temperature gives consistent results. High temperature gives creative variety. Choose based on your use case.
AI prompt: Try it with AI
Loading practice…
Timed quiz: Quick review
Loading practice…
Matching exercise: Match use cases to temperature
Loading practice…
Flashcards: Flashcards
Loading practice…
Checkpoint: Text generation fundamentals
Loading practice…
You now control the three key levers of text generation: context for memory, system instructions for personality, and temperature for creativity. Next, we will put it all together in hands-on challenges.