Concepts / Text Preprocessing for LLMs

Text Preprocessing for LLMs

Tokenization splits input text into individual tokens, and it is a required preprocessing step for creating embeddings for an LLM.

  • Programming

Why a Model Cannot Start with a Book

A large language model does not read a book the way a person does. Before any learning can happen, the text has to be broken into smaller pieces that a model can work with. This article focuses on the first of those pieces of work: tokenization, the process of splitting input text into individual tokens.

Tokenization is a required preprocessing step for creating embeddings for an LLM. Keep that dependency in mind throughout: the embeddings come after the tokens, not before them.

What do you think happens?

You load a text file into a Python variable called raw_text. Is raw_text now a list of tokens, or raw text?

  • A list of tokens, one per word

  • Raw text, not yet tokens

Reveal answer
Answer:

Raw text, not yet tokens

Reading a file into a variable gives you raw text. Tokenization is a separate step that happens afterward. Mixing up the raw text with the tokens is one of the most common sources of confusion about tokenization.

Tracing the Text from File to Raw Input

This article follows one practical path. The text used is The Verdict, a short story by Edith Wharton that has been released into the public domain. You can copy the text into a plain text file; the file used here is named the-verdict.txt. A Python program reads that file into a variable using standard file reading utilities.

At this point the state of your program is raw text. The variable holds the characters of the story as they appeared in the file. Nothing has been split yet, so there are no tokens. If you print a slice of the variable, you are looking at the material that tokenization will later process, not at tokens themselves.

What Counts as a Token

Definition

Tokenization is the process of splitting input text into individual tokens. A token is either an individual word or a special character, including punctuation characters.

The definition has two kinds of token. A word is one kind. A special character, such as a comma, a full stop, a question mark, or an exclamation mark, is the other kind. Punctuation is therefore not thrown away during tokenization; it becomes tokens of its own.

Because tokens are not the same thing as words, the number of tokens in a sentence is not always the number of words. Punctuation adds tokens without adding words.

FeatureWordToken
What it isA unit of language such as a name or a verbAn individual word or a special character, including punctuation
Does punctuation count?No, punctuation is not a wordYes, punctuation characters are tokens
Example from 'Hello, world!'Two words: Hello and worldFour tokens: Hello, comma, world, exclamation mark

Words and tokens are related but not identical

Where Tokenization Sits in the Pipeline

The source presents tokenization as one stage in a sequence of text processing steps that surround an LLM. Read the sequence below from left to right. It begins with the input text and ends with the output. Tokenization happens after the text is loaded and before token IDs and token embeddings are produced, and before the text reaches the transformer.

Input textloaded from a fileRaw textnot yet splitTokenizationsplit into tokensToken IDsToken embeddingsTransformerLLM outputloadsplitproduce IDsproduce embeddingsprocessgenerate
Which steps does raw text pass through, in what order, before the model produces an output, and where does tokenization fit among them?

Notice what this sequence does and does not claim. It places tokenization directly after the input text and before the token IDs. It does not show how the tokenizer decides where one token ends and the next begins. So the rest of this article works only from the definition given above, and it does not claim anything beyond that about the internal rules of a real tokenizer.

Hand-Tokenizing a Short Sentence

Now apply the definition by hand. Take the sentence 'Hello, world!' and split it into tokens, using the rule that a token is either a word or a special character. Work through it one piece at a time, and decide what each piece is before you move on.

Finding Where the Expected Split Breaks

A common first guess is to split the sentence at its spaces. That gives two pieces: 'Hello,' and 'world!'. Compare that expectation with the rule from the definition. The comma and the exclamation mark are special characters, so each should be its own token. The space-based guess keeps them stuck to the words, and this is the exact point where the expected split stops matching the token boundaries.

Hello,space-based pieceHelloword tokenworld!space-based piececommaspecial character tokenworldword tokenexclamation markspecial character token
At which exact character does the expected split stop matching the token boundaries?

Mistakes Beginners Make

  • Common mistake

    Treating the loaded file as already tokenized

    What it looks like

    After reading the-verdict.txt into a variable, assuming the variable holds a list of words.

    Why it fails

    Reading a file gives raw text. Tokenization is a separate step that comes after loading.

    Do this instead

    Label the variable as raw text, and only call it tokens after the splitting step has run.

  • Common mistake

    Counting words when you need tokens

    What it looks like

    Counting the two words in 'Hello, world!' and concluding that the sentence is two tokens long.

    Why it fails

    Special characters, including punctuation, are tokens too, so the sentence has more tokens than words.

    Do this instead

    Count each word and each punctuation mark as a separate token when applying the definition.

  • Common mistake

    Splitting only on spaces

    What it looks like

    Splitting 'Hello, world!' at spaces to get 'Hello,' and 'world!'.

    Why it fails

    This keeps punctuation attached to words, which does not match the rule that special characters are tokens.

    Do this instead

    Check each punctuation mark against the definition and decide whether it is a separate token.

  • Common mistake

    Assuming tokenization is finished once the text is loaded, when token IDs and embeddings have not been produced yet

Practice: Tokenize a New Sentence

EASY

Tokenize the sentence 'Ready? Go, now!' by hand. List every token in order, marking each one as a word or a special character. Then identify the first point where splitting on spaces would give a different result from your token list.

Hints
  • Start from the left and treat each word and each punctuation mark as its own piece.
  • A question mark is a special character, just as a comma and an exclamation mark are.
  • Compare your token list with what a space-based split would give, and find the first mismatch.

Key Takeaways from the Pipeline

Key point

Tokenization splits input text into individual tokens, and it is a required preprocessing step for creating embeddings for an LLM.

At a glance

  • Tokenization is the process of splitting input text into individual tokens, and it is a required preprocessing step for creating embeddings for an LLM.
  • A token is either an individual word or a special character, including punctuation characters, so the token count of a sentence can differ from its word count.
  • Reading a file into a variable gives raw text, not tokens; tokenization is a separate step that happens after loading and before token IDs and token embeddings are produced.
  • When tokenizing by hand, a space-based split keeps punctuation attached to words, and the divergence from the token rule appears at the first punctuation mark.

Related concepts

More concepts starting with T