liveBook · Manning

Temperature

As explained in Build Python Web Apps with Streamlit

Temperature is a language-model sampling parameter that controls how broadly probability is spread across possible next tokens.

Need a trivia game to ask varied questions instead of repeating itself? Temperature is one setting that changes how predictably a language model chooses its next token. At each step, the model assigns probabilities to possible tokens. A higher temperature flattens that distribution, giving less likely choices a better chance; a lower temperature favors the most likely choices more strongly. OpenAI accepts values from 0 to 2. section 9.6.1

Temperature affects variability, not factual accuracy or guaranteed uniqueness. For repeated trivia questions, combine it with a prompt that requests a unique question, includes a changing seed, and lists questions already asked. Another setting, top_p, limits the pool of candidate tokens by cumulative probability. section 9.6.1 section 9.6.2

import time

from llm import Llm
...

class Game:
  def __init__(self, llm_api_key):
    ...
    self.max_questions = 5
    self.questions = []

  def ask_llm_for_question(self):
    seed = int(time.time())
    sys_msg = QUESTION_PROMPT['system']
    usr_msg = (
      QUESTION_PROMPT['user']
      .replace('{already_asked}', '\n'.join(self.questions))
      .replace('{seed}', str(seed))
    )
    return self.llm.ask(usr_msg, sys_msg, temperature=0.7, top_p=0.9)

  ...
  def obtain_question(self):
    self.curr_question = self.ask_llm_for_question()
    self.questions.append(self.curr_question)
    self.status = 'ASK_QUESTION'
    return self.curr_question

  ...
  def proceed_to_next_question(self):
    self.status = 'GET_QUESTION'

  ...

The question-generation call uses temperature=0.7 and top_p=0.9 alongside a time-based seed and a record of earlier questions. section 9.6.2

In a Python wrapper, temperature can be an optional argument forwarded to OpenAI’s chat completion call. Check model support before exposing the setting: reasoning models such as gpt-5-mini do not allow explicit temperature or top_p configuration, and gpt-5.1 disallows it when reasoning mode is on. Also watch for a small wrapper bug: the shown `if temperature` check would omit an explicitly supplied value of 0. section 9.6.2

How Build Python Web Apps with Streamlit covers it

How deep it goes
Explains next-token probabilities and temperature’s effect on their distribution, then shows API configuration without deriving the sampling mathematics.
Examples
Uses a trivia game that requests questions with temperature=0.7, top_p=0.9, a changing seed and a list of earlier questions.
What it assumes
Introduces tokens and probability distributions in plain language; uses Python methods and dictionary unpacking for the implementation.
What you can do after
Apply it via the OpenAI Python library to vary generated questions while checking whether the selected model supports explicit configuration.
How it connects
Connects temperature to tokens, probability distributions, top_p, prompt randomness and hallucinations.

Also explained in

Compare how 17 books explain temperature →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.