Please enable JavaScript to use CodeHS

LLMs Under the Hood

From data to chatbot

A helpful chatbot is not built in one step. It is the result of a long pipeline: collecting data, tokenizing, pre-training, fine-tuning, aligning with feedback, and finally generating answers.

AI ~12 min Intermediate
The Big Idea

Data collection

Every LLM starts with data. Engineers gather text from books, websites, code repositories, encyclopedias, and other sources. The data is cleaned, filtered, and deduplicated.

Garbage in, garbage out. If the data is biased, outdated, or toxic, the model will learn those patterns. Data curation is a huge part of building a good model.

Key idea: the training data is the model's only window into the world. High-quality, diverse data matters as much as the architecture.


CS Minute

Tokenization and embeddings

The raw text is split into tokens. Each token gets an ID, and each ID is converted into an embedding vector. These vectors capture meaning and are the numbers the transformer actually processes.

By the end of this stage, a sentence has become a matrix of numbers: one vector per token.


CS Minute

Pre-training

The model is trained on next-token prediction with a massive corpus. It learns grammar, facts, reasoning patterns, and world knowledge. It also picks up the style of the data it saw.

At the end of pre-training, the model is a powerful text predictor, but it is not yet a helpful assistant.


CS Minute

Supervised fine-tuning

Pre-training produces a good mimic. Supervised fine-tuning (SFT) turns it into a conversational assistant. The model is trained on high-quality examples of questions and good answers.

SFT teaches the model the format of chat: a user asks something, and the assistant gives a clear, useful reply. It also narrows the model's behavior toward the style and tone the builders want.


CS Minute

RLHF

After SFT, the model goes through RLHF. Human labelers compare responses, a reward model is trained on those comparisons, and reinforcement learning nudges the policy to produce better answers.

RLHF helps the model be helpful, harmless, and honest. It is the final alignment stage before the chatbot is released.


CS Minute

Inference

At runtime, the chatbot takes your prompt, turns it into tokens, runs it through the trained model, and samples tokens one by one. Settings like temperature control randomness: low temperature gives safe, predictable answers; high temperature gives more creative, surprising ones.

Engineers also add system prompts to set the assistant's behavior and guardrails to block harmful requests.


Interactive

Build the pipeline

The stages below are out of order. Use the arrows to put them in the correct order, then click Check pipeline.


    Code

    The chatbot pipeline in code

    A chatbot is not one big box. It is a pipeline of steps. This Python code shows each stage as a function, from data to reply.


    Your Turn

    Check yourself

    Why is there a separate fine-tuning step after pre-training? What does SFT add that pre-training alone does not?

    Your answer:

    Sample answer: Pre-training makes the model good at language, but SFT teaches it the specific format of helpful conversations. It gives the model examples of how a good assistant should answer questions.


    Check Yourself

    Quick quiz

    1. What is the first stage of building an LLM chatbot?

    2. What does pre-training mostly teach the model?

    3. What does RLHF add to the pipeline?

    You made it through “LLMs Under the Hood”!

    You now know how LLMs turn words into vectors, how neural networks learn, how transformers pay attention, and how a raw model becomes a chatbot. Ready to go deeper into how these tools are changing software engineering?

    Part of the “LLMs Under the Hood” tutorial series.