← AI Practitioner study path

Generative AI basics · 12 min

Tokens, embeddings and chunking

In this lesson you will learn what a token is, why the same prompt can give different responses, how an embedding lets you search texts by their meaning and why a document is split into chunks.

Tokens, embeddings and chunking are three terms that the AIF-C01 exam guide names explicitly in the generative AI domain. All three describe how a foundation model works with text: how it reads it, how it generates a response and how documents are searched by their meaning. For example, when an online store's assistant answers "how do I return a product?", the model reads the question in tokens, searches for the text of the return policy with embeddings and finds it in a chunk of that policy.

What is a token?

A token is a sequence of characters that the model interprets or predicts as a single unit of meaning. According to the Amazon Bedrock documentation, a token can be a word, a sub-word with grammatical meaning (the documentation gives the example "-ed"), a punctuation mark such as "?" or a common phrase. For example, the question "when do my boxes arrive?" can be split into pieces such as "when", "do", "my", "box", "es", "arrive" and "?", although the exact split depends on the model.

Tokens are also the unit in which almost everything is measured in Amazon Bedrock. A model's price is charged by tokens, and the context window, which is the amount of text the model can consider in one call, is also measured in tokens. For example, if the store sends the model a short question, it pays for few tokens, and if it sends the whole catalog along with the question, it pays for many more.

How tokens are counted

Each model uses its own tokenizer, which is the part that decides how the text is cut into tokens, so the same text can produce a different number of tokens in two models. To find out how many input tokens a model would use before you send it the request, Amazon Bedrock has the CountTokens , and the number it returns matches the one you would be charged for. For example, before sending a long product description, the store can use CountTokens to find out how many tokens it will pay for in that model.

How does a model generate text?

A model generates its response one token at a time, and at each step it chooses the next token based on the previous ones. For example, to answer "Your order arrives on Tuesday", the model first chooses "Your", then "order", then "arrives", and so on until the end.

Probability distribution of the next token for the prompt "I hear the hoof beats of": horses 0.7, zebras 0.2, unicorns 0.1

The Amazon Bedrock documentation explains it with an example. With the "I hear the hoof beats of...", the model calculates a probability for each token that could come next: horses 0.7, zebras 0.2 and unicorns 0.1. It then chooses one by sampling that distribution, that is, by drawing it at random with those probabilities, adds it to the text and repeats the process with the longer sentence until it finishes.

Because that choice is random, the model will say "horses" most of the time, but not always, and that is why the same prompt can give different responses. Inference parameters, which are the options you choose on each call, change how much risk the model takes when it chooses, and one of them is temperature. For example, with a low temperature the model almost always says "horses", and with a higher one it says "zebras" or "unicorns" more often. A store that answers questions about shipping wants similar answers every time, and one that asks for ideas for an ad wants more variety.

Why did the response change?

You send the same prompt three times to the same model in Amazon Bedrock, without changing anything, and you get three different responses. What explains it?

What is an embedding?

An embedding is an input converted into a vector, which is a list of numbers that represents its meaning. Embeddings are produced by an embeddings model and are used to measure how similar two objects are, and AWS gives three uses: comparing sentences by their meaning, comparing images by their visual similarity, and comparing a text with an image to see whether they are related.

Three sentences converted into vectors: the two about not being able to sign in to the account end up together and the one about the store ends up far away

For example, the sentences "I forgot my password" and "I can't sign in to my account" share almost no words, but they talk about the same thing, and that is why their vectors end up close together, while the vector for "What time does the store open?" ends up far away. A real vector has hundreds or thousands of numbers, and the one from Amazon Titan Embeddings reaches 1,536. Thanks to that, the store's help search can find the right article even if the customer uses other words.

Vector databases

Vectors are stored in a vector database, which is a built to quickly find the vectors closest to a given one. That search is called nearest neighbor search (k-NN). For example, the store stores one vector for each help article and, when a customer types their question, the database returns the articles whose vectors are closest to the question's vector.

AWS recommends Amazon OpenSearch Service as the vector database for Amazon Bedrock. However, if your data is already in , Amazon Aurora PostgreSQL and Amazon RDS for PostgreSQL also store vectors with the pgvector . For example, if the store's catalog already lives in Aurora PostgreSQL, it can add its products' vectors there without moving anything to another database.

True or false?

0 of 3
  1. Two sentences with no words in common can have very close vectors.

  2. To search by meaning in a catalog that is already in Aurora PostgreSQL, you have to move everything to another database.

  3. An embedding only works for text.

What is chunking?

Chunking consists of splitting a document into pieces before converting it into vectors, because an 80-page manual isn't stored as a single vector. The AWS Generative AI Lens gives the reason for splitting it: so that the data fits in the model's context window. For example, if the store has a very long policy manual, it splits it into pieces so that the model receives only the piece that talks about returns and not the whole manual.

A document split into chunks; each chunk is converted into a vector and stored in an index with the reference to its page

Chunking is the first step in indexing a document, that is, in preparing it so it can be searched. First the document is split into chunks, then each chunk is converted into a vector, and finally that vector is stored in a vector index together with the reference to the document it came from. That way, when someone asks something, only the chunk that talks about it is retrieved. For example, if a customer asks how many days they have to return a product, the returns chunk is retrieved with the reference to the page of the manual where it is.

Chunking strategies

The five chunking strategies of Amazon Bedrock Knowledge Bases: default, fixed-size, hierarchical, semantic and no chunking, with what each one does

Amazon Bedrock Knowledge Bases offers five strategies for splitting a document:

  • Default chunking, which makes chunks of about 300 tokens that respect complete sentences.
  • Fixed-size chunking, where you choose how many tokens each chunk has and what percentage overlaps with the next one, that is, how much text from the end of one chunk is repeated at the start of the next so that an idea cut in half isn't lost.
  • Hierarchical chunking, which searches with small chunks, which are more precise, and replaces them with their parent chunk, which is larger and contains the small one, to provide context.
  • Semantic chunking, which splits where the meaning changes and which has an extra cost because it uses a foundation model.
  • The no chunking option, where each complete file is one chunk.

For example, if each of the store's frequently asked questions is already a short file, the no chunking option leaves each question as one chunk, and if the manual is long and changes topic often, semantic chunking splits where one topic ends and another begins.

Chunk size involves a trade-off. Very large chunks bring in text that is beside the point and use up tokens, and very small chunks lose the context. For example, a chunk that only says "you have one month" no longer says what that month is for. The complete flow of searching for chunks and passing them to the model so it answers with them is called RAG (Retrieval Augmented Generation).

Match each strategy with what it does

0 of 5 pairs

Tap a strategy and then its description.

0 of 5 pairs.

In both accounts

Tokens, embeddings and chunking work the same way in the full AWS account and in the new experience account. If you try the Amazon Bedrock from the new experience, choose a model available within us-east-2, because that account doesn't support cross-Region inference, which is sending your request to a model that runs in another Region. For example, if a model only shows up as available by sending the request to another Region, choose a different model for your test.

On the exam

In a question on this topic, some phrases in the question point to the answer:

  • "Search by meaning", "find similar documents" or "semantic search", which point to embeddings stored in a vector database.
  • "Same prompt, different responses", which points to the way the model samples the next token.
  • "Split documents before indexing them", which points to chunking.

For example, if a question says a team wants its chatbot to find the right manual even if the user doesn't use the same words, the answer is embeddings and a vector database.

cloud path tip: be wary of options that propose a larger context window to search documents. With a larger window the model reads more text in one call, but it doesn't search anything.

Review question

An internal search tool must return the article "Reset your password" when someone types "I can't sign in to my account". What makes it possible to find texts by their meaning?

Next

  • Key terminology, with the definitions of token, embedding and RAG from the Amazon Bedrock documentation.
  • Take an AI Practitioner practice exam at /examenes and review your result in domain 2.