← All lessons
Retrieval and RAG · Lesson 1 of 8

Before you start

This course follows How LLMs work. Here's the short version of what it uses from there, plus the bit of maths and Python behind the examples.

A large language model (LLM) takes text in and writes text back, one small piece at a time. The text you send is the prompt; it's measured in tokens, chunks of about four characters of English, and you pay per token. The context window is the most tokens one request can hold, so you can't paste in every document you have. The model remembers nothing between requests: anything it should use has to be in the prompt. Temperature is a setting for how varied its wording is; low means it sticks to its likeliest answer.

An eval (evaluation) is a set of test questions with known good answers, run after every change to see whether things got better or worse. When you're finding the right items in a big pile, recall is the share of the right ones you found, and precision is the share of what you found that was right.

A vector is just a list of numbers, like [0.9, 0.1, 0.3]. You can picture a list of two numbers as an arrow on a map: so far east, so far north. Lists of hundreds of numbers work the same way, in more directions than we can draw. The dot product of two vectors multiplies them item by item and adds the results: [1, 2] and [3, 4] give 1 × 3 + 2 × 4 = 11. It's big when the two point the same way.

The examples are short Python (Programming basics covers the language). A few shortcuts appear here: [x * 2 for x in xs if x > 2] builds a new list from an old one (a list comprehension); zip(a, b) pairs up two lists item by item; enumerate(xs, start=1) counts the items as it goes; and lambda c: c["score"] is a tiny unnamed function, used here to say what to compare when picking the biggest.

xs = [3, 8, 1]
print([x * 2 for x in xs if x > 2])
print(max(["ab", "c", "def"], key=lambda w: len(w)))
for i, w in enumerate(["a", "b"], start=1):
    print(i, w)
print(sum(a * b for a, b in zip([1, 2], [3, 4])))
Prints
[6, 16]
def
1 a
2 b
11

Practice

Why can't you just put all your documents in every prompt?

More info

Quick reference

Prompt, tokenWhat you send; the small pieces text is counted in.
Context windowThe most tokens one request can hold.
EvalA test set run on every change.
Recall, precisionShare of the right ones found; share of the found ones that are right.
VectorA list of numbers, picturable as an arrow.
Dot productMultiply item by item, then add.
[f(x) for x in xs], zip, enumerate, lambdaBuild a list; pair up; count along; a tiny function.

The other AI and data engineering courses