Chapter 19 · Hands-on: build real projects
Make embeddings on your computer
An embedding model turns each piece of text into a list of numbers; similar meanings get similar numbers.
Chapter 19 · Hands-on: build real projects
An embedding model turns each piece of text into a list of numbers; similar meanings get similar numbers.
Sentences go in…
You will run a small script that gives every sentence a list of numbers, then compares the lists. Sentences that mean similar things end up close together, even when they share few words.
This is the first project in a series. By the end you will chat with your own PDFs using only your computer. We keep one project folder, pdf-chat, for every lesson. These tutorials cover Windows, macOS and Linux desktops, not phones.
You need the terminal and a virtual environment from the setup lessons, Terminal basics for AI work and Set up Python and a virtual environment. This lesson only reminds you of the commands.
Open a terminal. Make a folder called pdf-chat (mkdir makes a folder, cd moves into it), create a virtual environment inside it, then activate it. A virtual environment is a private box of Python packages for one project. If you already made one in the setup lessons, just cd into that folder and activate it. On macOS and Linux, use python3 if python is not found.
mkdir pdf-chat
cd pdf-chat
python -m venv .venv
.venv\Scripts\Activate.ps1 Check: Your terminal line now starts with (.venv).
One popular option is sentence-transformers, a free open-source library with many small embedding models. pip is Python’s package installer.
pip install sentence-transformers Check: The command ends without a red error. Then python -c "import sentence_transformers; print('ok')" prints ok.
Create a file called embed_demo.py inside pdf-chat with any text editor, paste this in and save. The model all-MiniLM-L6-v2 is a small embedding model (or use any small model your tool lists). The script embeds six sentences, then compares every pair. normalize_embeddings=True scales each list of numbers to length 1, so a simple multiplication gives the similarity score.
from sentence_transformers import SentenceTransformer
sentences = [
"The library closes at 8 pm on weekdays.",
"Books can be borrowed for two weeks.",
"You must return borrowed books within 14 days.",
"The canteen serves lunch from 12 to 2.",
"Fresh vegetables are on the lunch menu every day.",
"Wi-Fi passwords are changed every month.",
]
model = SentenceTransformer("all-MiniLM-L6-v2")
vectors = model.encode(sentences, normalize_embeddings=True)
print("Each sentence became", vectors.shape[1], "numbers")
print("First five numbers of sentence 1:", vectors[0][:5].round(3))
# vectors are length 1, so this dot product is the cosine similarity
scores = vectors @ vectors.T
pairs = []
for i in range(len(sentences)):
for j in range(i + 1, len(sentences)):
pairs.append((float(scores[i][j]), i, j))
pairs.sort(reverse=True)
print("\nMost similar pairs:")
for score, i, j in pairs[:3]:
print(f"{score:.2f} {sentences[i]!r} <-> {sentences[j]!r}") Check: The file is saved in the pdf-chat folder, not your Downloads folder.
The first run downloads the model once and stores it on your computer. After that it works offline. You may see a progress bar or a harmless warning first.
python embed_demo.py Check: You see Each sentence became 384 numbers, five small numbers, then three pairs. The two sentences about borrowing books should come first with the highest score. Your exact scores may differ a little; the order is what matters. If you used another model, the count of numbers will differ too.
A question is just another sentence. Embed it, then score it against each sentence. The highest score is the best match, even though the question and the answer share almost no words. This is the heart of search in a RAG app. Save as ask_demo.py and run it.
from sentence_transformers import SentenceTransformer
sentences = [
"The library closes at 8 pm on weekdays.",
"Books can be borrowed for two weeks.",
"The canteen serves lunch from 12 to 2.",
"Wi-Fi passwords are changed every month.",
]
model = SentenceTransformer("all-MiniLM-L6-v2")
vectors = model.encode(sentences, normalize_embeddings=True)
question = "How long can I keep a book?"
q = model.encode(question, normalize_embeddings=True)
scores = vectors @ q
for idx in scores.argsort()[::-1]:
print(f"{scores[idx]:.2f} {sentences[idx]}")python ask_demo.pyCheck: The line about borrowing books for two weeks is at the top, with a clearly higher score than the others.
Use the script builder below this tutorial to make a script from your own sentences. Try two sentences that mean the same thing in different words, and one unrelated sentence. Save the result as my_sentences.py and run it. Then try a pair with opposite meanings and see if the score still looks high; embeddings capture topic more than exact truth.
python my_sentences.pyCheck: The two same-meaning sentences get the top score.
Type a few sentences and copy the finished script. This does not run a model; it only writes the Python for you.
from sentence_transformers import SentenceTransformer
sentences = [
"The match starts at 5 pm.",
"Kick-off is at five in the evening.",
"My phone battery is low.",
]
model = SentenceTransformer("all-MiniLM-L6-v2")
vectors = model.encode(sentences, normalize_embeddings=True)
scores = vectors @ vectors.T
pairs = []
for i in range(len(sentences)):
for j in range(i + 1, len(sentences)):
pairs.append((float(scores[i][j]), i, j))
pairs.sort(reverse=True)
for score, i, j in pairs[:3]:
print(f"{score:.2f} {sentences[i]} <-> {sentences[j]}")Save this next to embed_demo.py and run python my_sentences.py. It prints the three closest pairs. Your text is only turned into a script here; nothing is sent anywhere.
What do you see?
What does an embedding model give you for a sentence?
Why can “How long can I keep a book?” match “Books can be borrowed for two weeks.”?
Why does the first run of the script take longer?