LLM (Large Language Models)

How LLMs are Trained: Pretraining, Fine-tuning and Alignment (Instruction Tuning, RLHF)

Learning Outcome

4

Apply the right approach: prompting, fine-tuning, or retrieval.

3

Recognize how RLHF uses human preferences and a reward model.

2

Identify pretraining, instruction tuning, and alignment.

1

Understand the three stages that build an assistant LLM.

5

Connect training stages to reasoning models and open-weight releases.

In competitive exams, a machine checks OMR sheets using an answer key.

It checks every answer, on every sheet

Checking OMR Answer Sheets

In the same way, within seconds, and runs again whenever new sheets arrive.

Why use a machine instead of checking every sheet by hand?

Automatic checking is fast, complete, and never gets tired.

 

Similarly, after every change to our API, we need to check that everything still works:

We write small checks with expected answers, called tests, and a tool runs all of them with one command.This is called Automated Testing with pytest.

 

/health returns 200

Wrong input returns 422

Predictions come in the right format

What is Automated Testing?

Writing code that checks your code: known inputs in, expected results confirmed.

Runs in seconds, as often as needed, with exactly the same checks every time.

Regressions

Environment changes

Model changes

a change quietly breaks something that used to work.

a library upgrade or new base image changes behaviour.

a new model loads fine but returns a different format or worse quality.

How pytest Works

Test files

Test functions

Plain assert

Named test_*.py or *_test.py, usually in a tests folder.

Any function starting with test_.

Failures show actual vs expected side by side.

One command

Pytest runs everything; -v lists tests, -x stops at the first failure, -k predict filters by name.

Code

def test_health_returns_ok():
    response = client.get("/health")
    assert response.status_code == 200

Follow Arrange, Act, Assert: one behaviour per test, with a name that explains a failure.

Fixtures and Parametrisation

Prepares something tests need, such as a client or a valid sample input.

Runs one test with many inputs.

Tests receive a fixture by listing its name as a parameter; share fixtures via conftest.py.

Code

@pytest.mark.parametrize("age", [15, 61, -3])
def test_invalid_age_rejected(client, valid_input, age):
    payload = {**valid_input, "age": age}
    response = client.post("/predict", json=payload)
    assert response.status_code == 422

Testing a FastAPI Service with TestClient (Core Mechanism)

Create client

= TestClient(app).

No Uvicorn, no port, no network.

Send a request: client.get("/health") or client.post("/predict", json=payload).

Assert on the status code and response fields.

 Cover the suite: health and info, the happy path, 422 validation, 404/503 errors, batch limits and per-item errors.

Swap the real model for a small fake (dependency overrides, monkeypatch, or unittest.mock) for fast API tests; test the real model separately.

 

Testing the Model Itself

Smoke

Minimum quality

Invariance

Directional

Loads; one prediction per row; probabilities between

0 and 1.

On a fixed held-out set, F1 ≥ 0.75 (or the agreed metric).

 Changing the student's name doesn't change the prediction.

More support tickets never lower churn probability.

A model can pass every API test and still predict badly. Model tests check behaviour:

 

These become the quality gate for retraining: a model that fails them is never promoted.

Measuring Test Coverage

Pytest-cov reports which lines ran during tests.

Shows the percentage per file and highlights untouched code, such as an error branch.

A guide, not proof code run without meaningful asserts still counts as covered.

Code

pytest --cov=app

Base Model vs Instruction-tuned Model (Comparison)

AspectBase ModelInstruction-tuned (Chat) Model
Trained onNext-token prediction on raw textPretraining + instructions + preferences
BehaviourContinues whatever text it's givenFollows instructions, answers questions
"What is AI?"May continue with more questionsGives a direct, structured explanation
SafetyLittle or noneDeclines clearly harmful requests
  Typical useResearch, further fine-tuningChatbots, assistants, most apps
AnalogyA child who has only listenedA child taught how to answer politely

Think About This: Spotting the Difference

    

     

Pretraining

Next-token prediction on trillions of tokens of web text and books

    

     

Training on 50,000 instruction–response examples  

    

Reviewers compare two answers to the same prompt and pick the better one

Unit tests find where; integration tests prove it all works together.

Instruction tuning.

     

RLHF (preference collection).

 Real-World Applications

Test inside the image

Docker run churn-api:1.0 pytest checks the exact pinned environment.

LLM features

Assert schema, required fields, length limits, and banned content, never exact text.

Next step

CI runs this suite on every code change automatically

Every bug becomes a test

so it can never return unnoticed.

Summary

5

Changing facts belong in the prompt.

4

RLHF: compare, reward, optimise.

3

Alignment gives judgement.

2

Instruction tuning gives manners.

1

Pretraining gives knowledge.

Quiz

Your institute's fees change every quarter. What's the most reliable way for a chatbot to answer fee questions correctly?

A. Pretrain a new model each quarter

B. Fine-tune the model on new fee list each quarter

C. Run RLHF with counsellors' preferences

D. Include current fees when prompting.

Quiz-Answer

Your institute's fees change every quarter. What's the most reliable way for a chatbot to answer fee questions correctly?

A. Pretrain a new model each quarter

B. Fine-tune the model on new fee list each quarter

C. Run RLHF with counsellors' preferences

D. Include current fees when prompting.