Introduction to AI: How Machines Learn · Lesson 5 of 6

Lesson 5: Chatbots and Language Models

How large language models predict text, why they sometimes make things up, and how to write better prompts.

Lesson 5: Chatbots and Language Models

Chatbots such as the AI assistants you may have used are powered by large language models (LLMs). At heart, an LLM does one thing very well: predict the next piece of text.

Predicting the next word

Given "The capital of Nepal is", the most likely next word is "Kathmandu". A language model learns such patterns from a huge amount of text. Text is split into small pieces called tokens (words or parts of words). The model repeatedly picks a likely next token, building a reply one token at a time.

A toy version

This tiny program learns which word tends to follow which, from one short text. It is far simpler than a real model, but the idea of "predict the next word from patterns in text" is the same.

text = "i like tea i like books i like tea"
words = text.split()

followers = {}
for current, nxt in zip(words, words[1:]):
    followers.setdefault(current, []).append(nxt)

def most_likely(word):
    options = followers[word]
    return max(set(options), key=options.count)

print(most_likely("i"))
print(most_likely("like"))

Output:

like
tea

Why chatbots can be wrong

  • They predict plausible text, not verified facts, so they can state wrong things confidently. This is often called a hallucination.
  • They only know what was in their training data, which may be out of date.
  • They can reflect biases in the text they learned from.

Prompting: asking well

Weak promptBetter prompt
Tell me about historyExplain the unification of Nepal in 5 simple points for a Class 10 student
Fix my codeThis Python function should return the average but returns 0. Here is the code and the error. What is wrong?
Write an essayWrite an outline for an essay on water pollution, with 4 headings, in simple English

Good prompts say who it is for, what you want, the format, and any limits. You can also ask the AI to show its steps, and then check them yourself.

Use AI as a helper, not a judge. For exams, facts, health, money or law, always confirm with a reliable source or a teacher.

Think about it

  1. Ask an AI tool a question you already know the answer to. Is it fully correct?
  2. Rewrite one of your own questions to include the audience and the format.
  3. Why is it risky to copy an AI answer straight into homework?

Final lesson: using AI responsibly, and where to go next.

Finished this lesson?Log in to save your progress and pick up where you left off.
Next lesson →

Comments

Loading comments…

MeroSir Team
A free MeroSir course. Run the examples yourself and try the exercises: you learn programming by doing. Spotted a mistake? Use the Report button above the lesson.
Lesson 5: Chatbots and Language Models | MeroSir