Controlling creativity with temperature
You’re the product lead at Luna Gear, launching the eco-friendly TerraPack backpack. The team needs A/B captions for a test: one polished and informative, one playful and hype-driven.
The anthropic
library, client
and product_info
string are preloaded.
This exercise is part of the course
Introduction to Claude Models
Exercise instructions
- Set a low
temperature
value for consistent, professional output (0.1-0.3). - Set a high
temperature
value for creative, varied output (0.7-0.9). - Print the conservative and creative response contents.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
conservative_response = client.messages.create(
# Conservative caption with low temperature
model="claude-3-7-sonnet-latest", temperature=____, max_tokens=150,
messages=[{"role": "user",
"content": f"Write a professional caption for this product: {product_info}"}])
creative_response = client.messages.create(
# Creative caption with high temperature
model="claude-3-7-sonnet-latest", temperature=____, max_tokens=150,
messages=[{"role": "user",
"content": f"Write a fun, engaging caption for this product: {product_info}"}])
# Print the Conservative and Creative Responses
print("Conservative caption:", ____)
print("\nCreative caption:", ____)