What is temperature and how it affects prompts
Temperature is a parameter in AI language models that controls the randomness of generated text. Higher temperature values produce more diverse and creative outputs, while lower values yield more deterministic and focused responses.Temperature is a parameter that controls randomness in AI language model outputs, affecting creativity and determinism.How it works
Temperature adjusts the probability distribution from which the AI model samples the next word or token. Think of it like a dial: at low temperatures (close to 0), the model picks the most likely next word, making responses predictable and focused. At higher temperatures (e.g., 0.8 to 1.0), the model samples from a wider range of possibilities, increasing creativity but also randomness.
Concrete example
This Python example uses the OpenAI SDK to generate text with different temperature settings:
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
prompt = "Write a creative story about a robot learning emotions."
# Low temperature for focused output
response_low = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}],
temperature=0.2
)
# High temperature for creative output
response_high = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}],
temperature=0.9
)
print("Low temperature output:\n", response_low.choices[0].message.content)
print("\nHigh temperature output:\n", response_high.choices[0].message.content) Low temperature output: The robot carefully analyzes its surroundings and begins to understand human emotions logically. High temperature output: The robot dances under neon skies, feeling sparks of joy and sadness swirl like electric storms inside its circuits.
When to use it
Use low temperature (0.0–0.3) when you want precise, factual, or consistent answers, such as in coding, summarization, or data extraction. Use higher temperature (0.7–1.0) for creative writing, brainstorming, or generating diverse ideas. Avoid very high values (>1.0) as they can produce incoherent or off-topic outputs.
Key terms
| Term | Definition |
|---|---|
| Temperature | A parameter controlling randomness in AI text generation. |
| Sampling | The process of selecting the next token based on probabilities. |
| Deterministic | Output that is predictable and consistent. |
| Creative output | Text that is varied, imaginative, and less predictable. |
Key Takeaways
- Adjust
temperatureto balance creativity and reliability in AI outputs. - Low
temperatureyields focused, repeatable responses ideal for factual tasks. - High
temperatureencourages diverse and imaginative text generation. - Use the OpenAI SDK's
temperatureparameter to control output randomness precisely.