Understanding AI · lesson 43 of 60 · about 6 minutes
Picking a Word
Why does a chatbot give a different answer when you ask the same thing twice? It's how the next word is picked.
What you'll learn
- Explain the difference between always picking the top word and picking by chance
- Pick a word from chances using a random number
- Explain what the temperature setting changes
Top word or a roll of the dice?
Always picking the most likely word sounds sensible, but it makes writing dull and repetitive. So chatbots usually pick by chance: cats 75% of the time, dogs 25%.
That's why the same question can get a different answer each time.
Picking a word with a random number between 0 and 1 (here 0.9, as if the dice landed there). The running total marks out each word's share.
chances = {"cats": 0.75, "dogs": 0.25}
roll = 0.9
running = 0
for word, chance in chances.items():
running = running + chance
if roll < running:
print(word)
breakOutput
dogsTemperature
Many chatbots have a setting called temperature. Low temperature makes the likeliest words even likelier: steady, predictable answers. High temperature evens out the chances: more surprising, more creative, and more likely to go off track.
Tip
Rolls from 0 to 0.75 land on cats; from 0.75 to 1 on dogs. break stops the loop once a word is picked.
Now try it yourself
This lesson is free in Codiqo, with 6 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.