Introduction to Generative AI

Generative AI & LLM Fundamentals · Generative AI and LLMs · 12 min read · Updated

What a foundation model can do

  1. A foundation model is trained once on a very large amount of data, so it can be used for many tasks.
  2. Large language models focus on text: they write, summarise, translate and answer questions.
  3. Other models create images from a written description.
  4. Models can also draft and explain code.
  5. The output can be fluent and still wrong. A person must check anything that matters.

Until a few years ago, most AI products were built to judge or predict: is this email spam, will this customer leave, which job fits this resume best. Today, many AI products are built to create: they write an email, draw a picture, compose a tune, or write computer code. This second kind is called generative AI. In this lesson you will learn what makes it different, what it can make, how the simplest possible text generator works, and where generative AI goes wrong. You will build that simplest generator yourself, in a few lines of code, and see why it is a good first picture of how a large language model behaves.

Predictive AI and generative AI

Predictive AI, sometimes called discriminative AI, looks at an input and chooses from a small set of answers. A spam filter says “spam” or “not spam”. A resume screener gives a score from 0 to 100. The output is a label or a number.

Generative AI produces something new that was not in the input: a paragraph, an image, a song, a piece of code. The output is open-ended. There is no short list of correct answers. If you ask a generative model for “a cover letter for a warehouse job”, there are millions of acceptable letters.

Underneath, both kinds learn patterns from data. A generative model learns the patterns of how things look: how sentences are built, how pictures are arranged. Then it uses those patterns to produce new examples that look similar to what it learned from. It does not copy and paste a stored example. It produces new output one small piece at a time.

What generative AI can create

  • Text. Emails, summaries, translations, stories, explanations and answers. This is where large language models (LLMs) work.
  • Code. Programs, tests, and fixes for bugs, often from a plain-English description.
  • Images. Pictures from a written description, or edits to an existing photo.
  • Audio and music. Speech in a chosen voice, sound effects, songs.
  • Video. Short clips from a description or a still image.
  • Structured data. Tables, JSON and database queries. You will use this in the prompt engineering lessons.

This course focuses on text, because text models are the base of most business tools: chat assistants, resume writers, search with answers, and support bots.

The core idea: predict the next piece

A language model has one basic skill. Given some text, it estimates which piece of text is likely to come next. A piece is usually a word or part of a word, called a token (lesson 6 explains tokens). Write “Thank you for your” and the model finds that “time”, “help” and “application” are likely, while “banana” is not. It picks one of them, adds it to the text, and repeats. That loop, repeated hundreds of times, produces a whole paragraph.

It may sound too simple to produce useful answers, but there is a catch: to predict the next word well across all kinds of text, the model must learn a lot about grammar, facts, style and even some reasoning. Predicting the next word in a maths textbook rewards knowing some maths. Predicting the next word in a polite email rewards knowing how polite emails sound.

The simplest version of this idea needs no neural network at all. It is called a Markov chain. You look at the last one or two words, find every place in your training text where those words appeared, and note which word came next each time. To generate, you pick one of those next words at random, add it, shift forward by one word, and repeat. Words that followed more often are picked more often.

Example: suppose the training text contains “the manager reviews the resume” and “the manager calls the candidate”. After the words “the manager” the possible next words are “reviews” and “calls”. The chain picks one. If it picks “calls”, then looks at “manager calls”, the next word can only be “the”, and so on. The output can combine pieces in ways that never appeared as a whole in the training text.

Why a Markov chain is not enough

Your tiny generator will sound a little like the text it was trained on, but it has no real idea of meaning. It sees only the last one or two words. With a short memory it writes nonsense that drifts away from any topic. With a long memory (many words) it simply copies whole pieces of the original, because there is only one thing that ever followed those words. You will measure this in the exercise: a number that shows what share of the output is copied from the source.

A large language model solves this by looking at thousands of previous words at once, by using attention (lesson 4) and by learning from a vast amount of text. Instead of counting exact word matches, it learns a smooth sense of similarity, so it can handle sentences it has never seen. But the loop is the same: look at the text so far, estimate the likely next token, pick one, repeat.

Where generative AI fails

Because the model produces what is likely, not what is true, it has a few typical weaknesses. Learn them now, because later lessons show how to reduce each one.

  • Hallucination. The model states something false in a confident voice. A made-up quotation, a wrong date, a fake web address. It happens because a fluent guess looks the same as a fact from the model’s point of view.
  • Out-of-date knowledge. The model learned from text up to a certain date and knows nothing after it, unless you give it new information.
  • Bias. The training text came from people, so the model can repeat stereotypes found there.
  • Inconsistency. Ask the same question twice and you may get two different answers, because the choice of the next token has some randomness.
  • Prompt sensitivity. Small changes to the wording of a request can change the quality of the answer. This is why prompt engineering exists.

None of this means generative AI is useless. It means you should use it the way you would use a quick, well-read, over-confident assistant: great for drafts, ideas and summaries, but needing a check before important facts go out under your name.

Using generative AI responsibly

Three simple habits help. First, check facts that matter: numbers, names, dates, links and anything legal or medical. Second, protect private data: do not paste passwords, ID numbers or other people’s personal details into a tool unless you know how it stores them. Third, be honest about where text came from when it matters, for example in school or in a professional setting. A resume written with AI help is fine, but every claim in it must still be true.

Your task: build a Markov text generator

You will write the simplest text generator and a page that lets you change how much it remembers. You will see the difference between nonsense and copying with your own eyes.

What you will build

  • A buildChain function that reads a training text and records which words follow each group of words.
  • A generate function that starts at the beginning of a sentence and produces a chosen number of words.
  • A copiedShare function that measures how much of the output is a straight copy from the training text.
  • A page with a text area for the training text, sliders for memory and length, and a Generate again button.

Steps

  1. Create lib/markov.ts. Write a small tokens helper that splits text into words on spaces.
  2. Write buildChain(text, order). For every position in the text, take the previous order words as a key (joined with a space) and store the word that came next in a list for that key. Also remember which keys start a sentence.
  3. Write generate(chain, count, seed). Pick a starting key, then repeat: look up the possible next words, pick one with the seeded random generator from lesson 3, add it, and slide the key forward. When a key has no next word, start a new sentence.
  4. Write copiedShare(source, output): take every run of four words in the output and check whether it appears in the source. Return the share that does.
  5. Build the page. Show the generated text and the copied share as a percentage. Let the seed change so that Generate again gives a new text.

Hints

  • Start with a training text of five or six sentences on one topic, such as job interviews.
  • Use memory 1 and look at the text. Then try 2 and 3. Notice what happens to the copied share.
  • The seed makes the output repeatable. The same seed gives the same text, which is very useful for testing.
  • Keep a list for each key, with repeated words included. That is what makes common words more likely, with no extra code.

You know it works when memory 1 gives odd but varied text with a low copied share, memory 3 gives smooth text that is mostly copied, and pressing Generate again gives a different text each time.

Solution

The Markov chain, with building, generating and measuring the copied share:

import { makeRng } from './rng';

// A Markov chain is the simplest text generator: it only remembers the last few words.
// "order" is how many words it remembers. Order 1 = the last word. Order 2 = the last two words.
export type Chain = { order: number; next: Map<string, string[]>; starts: string[] };

const tokens = (text: string) => text.replace(/\s+/g, ' ').trim().split(' ').filter(Boolean);

export function buildChain(text: string, order: number): Chain {
  const words = tokens(text);
  const next = new Map<string, string[]>();
  const starts: string[] = [];
  for (let i = 0; i + order < words.length; i++) {
    const key = words.slice(i, i + order).join(' ');
    if (i === 0 || /[.!?]$/.test(words[i - 1]!)) starts.push(key); // a good place to begin: the start of a sentence
    next.set(key, [...(next.get(key) ?? []), words[i + order]!]);
  }
  return { order, next, starts };
}

// Pick a start, then repeat: look at the last words, choose one of the words that followed them in the text.
export function generate(chain: Chain, count: number, seed: number): string {
  const rng = makeRng(seed);
  if (!chain.starts.length) return '';
  let current = chain.starts[Math.floor(rng() * chain.starts.length)]!.split(' ');
  const out = [...current];
  while (out.length < count) {
    const options = chain.next.get(current.join(' '));
    if (!options?.length) {
      current = chain.starts[Math.floor(rng() * chain.starts.length)]!.split(' '); // dead end: start a new sentence
      out.push(...current);
    } else {
      const word = options[Math.floor(rng() * options.length)]!;
      out.push(word);
      current = [...current.slice(1), word];
    }
  }
  return out.slice(0, count).join(' ');
}

// How much of the output is copied straight from the source? (runs of 4 words that appear in it)
export function copiedShare(source: string, output: string) {
  const src = tokens(source).join(' ');
  const out = tokens(output);
  if (out.length < 4) return 0;
  let hit = 0;
  for (let i = 0; i + 4 <= out.length; i++) if (src.includes(out.slice(i, i + 4).join(' '))) hit++;
  return hit / (out.length - 3);
}

And the page:

import { Component, computed, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { buildChain, copiedShare, generate } from '../lib/markov';

const SAMPLE = `I am a hard working warehouse supervisor with five years of experience. I manage a team of twelve people and keep stock records accurate. I reduced picking errors by twenty percent in one year. I am skilled in inventory control and safety training. I am looking for a supervisor role where I can lead a team and improve daily operations. I am reliable and I work well under pressure. I enjoy training new staff and solving problems on the shop floor.`;

@Component({
  selector: 'app-markov',
  imports: [FormsModule],
  template: `
    <h1>The simplest text generator</h1>
    <p class="hint">This program only remembers the last one or two words. It writes new text that sounds a little like the source. It does not know what any word means.</p>

    <div class="card">
      <label for="t">Training text</label>
      <textarea id="t" [ngModel]="text()" (ngModelChange)="text.set($event)" style="min-height:140px"></textarea>
      <div class="grid2">
        <div><label for="o">Words remembered (order): {{ order() }}</label><input id="o" type="range" min="1" max="3" step="1" [ngModel]="order()" (ngModelChange)="order.set(+$event)" /></div>
        <div><label for="n">Length: {{ count() }} words</label><input id="n" type="range" min="10" max="80" step="5" [ngModel]="count()" (ngModelChange)="count.set(+$event)" /></div>
      </div>
      <button (click)="seed.set(seed() + 1)">Generate again</button>
    </div>

    <div class="card">
      <h2>Generated text</h2>
      <div class="answer">{{ output() }}</div>
      <p class="muted">{{ (copied() * 100).toFixed(0) }}% of it is copied word-for-word from the training text. The chain has {{ chain().next.size }} different "memories".</p>
      <p class="muted">Order 1 is creative but often nonsense. Order 3 is fluent but mostly copies the source. Real language models avoid this trade-off by learning patterns instead of storing exact word sequences.</p>
    </div>
  `,
})
export class MarkovPage {
  text = signal(SAMPLE);
  order = signal(1);
  count = signal(40);
  seed = signal(1);

  chain = computed(() => buildChain(this.text(), this.order()));
  output = computed(() => generate(this.chain(), this.count(), this.seed()));
  copied = computed(() => copiedShare(this.text(), this.output()));
}

What you learned

  • Predictive AI chooses a label or a number. Generative AI creates new content.
  • A language model repeatedly estimates the likely next piece of text and picks one.
  • A Markov chain is the simplest version of this idea and shows the trade-off between creativity and copying.
  • Generative AI can hallucinate, be out of date, be biased and be inconsistent, so important output needs checking.

In the next lesson you will look inside this loop: tokens, the context window, and the settings (temperature, top-k, top-p) that control how the next token is chosen.

Download the solution (genai-basics-lesson-05.zip)

Frequently asked questions

What is the difference between generative AI and a chatbot?

Generative AI is the technology that creates new content. A chatbot is one application that uses it, wrapped in a chat window. Many chatbots use a large language model to generate their replies.

Does generative AI copy existing text?

It does not look up and paste stored text, but it can reproduce passages that appeared many times in its training data. It generates output one token at a time based on learned patterns.

Why does generative AI sometimes say false things?

It produces text that is likely given the patterns it learned, and it has no built-in way to check facts. A fluent false sentence can be as likely as a true one, so the model may write it with confidence.

Is a Markov chain a real language model?

Yes, it is the oldest and simplest type of statistical language model. Modern models are far larger and use neural networks and attention, but they share the idea of predicting the next piece of text.