Build a 'more like this' recommender with embeddings
Every shop, streaming service and news site has a "more like this" row. In this mission you build your own, over any catalogue you like: films, products, articles, songs. Give it one item and it returns the most similar others; give it three things a person liked and it learns their taste and recommends from it. It works on day one with no ratings at all, and every piece runs on your own laptop with a small model and no graphics card.
- uv, the Python runner used across this site. It fetches Python for you. If you have not got it, Install anything from GitHub safely (Level 0) has the one-line install.
- You can read a little Python. You do not write any from scratch: every file is complete and in the kit.
- Helpful but not required: Build a semantic search engine that understands meaning. That lesson finds passages that match a question; this one recommends items like the ones you already have. They share the same embedding model, so if you did it the setup here will feel familiar.
- A few GB of free disk space. Almost all of it is PyTorch, which the embedding model runs on; the model itself is about 90MB. No graphics card, no accounts, no API keys, nothing to pay for.
Two kinds of recommender, and why content-based
There are two ways to recommend things. One needs a pile of ratings before it works; the other works on day one. You are building the second.
Recommenders come in two families. Collaborative filtering learns from behaviour: it watches who liked what and finds patterns across people, so it can tell you "readers who bought this also bought that" without knowing a single thing about the books themselves. It is what powers most big recommendation systems, and it is powerful, but it has a cold opening problem: with no ratings yet, it has nothing to learn from.
Content-based filtering works the other way. It looks at the item itself, its title, its description, its text, and recommends other items whose content is similar. It needs no ratings and no other users at all: one item is enough to find things like it. That is what you build here. It is the right tool when you are starting out, when your catalogue changes often, or when you want recommendations to run entirely on your own machine with nothing uploaded.
The engine behind it is the same one from the semantic search lesson: a small model turns each item into a list of numbers that stands for its meaning, and "similar" becomes "close together". Download the kit into a new folder: save recommend.py and requirements.txt in the folder itself, and films.json inside a sub-folder called
catalogue. Then set it up and see the headline result:Windowscd $HOME\Documents mkdir recommender; cd recommender # put recommend.py and requirements.txt here, and films.json in a catalogue\ sub-folder uv venv --python 3.13 uv pip install -r requirements.txt uv run recommend.py build uv run recommend.py similar "The Matrix"Mac / Linuxcd ~/Documents mkdir recommender && cd recommender # put recommend.py and requirements.txt here, and films.json in a catalogue/ sub-folder uv venv --python 3.13 uv pip install -r requirements.txt uv run recommend.py build uv run recommend.py similar "The Matrix"buildreads the 80 films and turns each into numbers (the first run downloads the 90MB model, so give it a minute).similarthen finds the films most like The Matrix:OutputMore like "The Matrix" (1999) [sci-fi] 1. 0.674 The Matrix Reloaded (2003) [sci-fi] 2. 0.492 Ex Machina (2014) [sci-fi] 3. 0.416 The Dark Knight (2008) [action] 4. 0.382 Casino Royale (2006) [action] 5. 0.381 Blade Runner 2049 (2017) [sci-fi]The closest film is its own sequel at 0.674, then more science fiction and action. Here is the part worth holding on to: the model was never told the genre. It read only each film's title and one-line description and worked out that these belong together. The
[sci-fi]and[action]labels are printed for you to judge the results; the recommender never sees them. The rest of the lesson takes this apart and shows you how to recommend from a person's taste, not one film.TipThe score is cosine similarity, which runs from -1 to 1. Around 0.7 is a very close match; 0.4 is loosely related; near 0 is unrelated. Step 4 explains exactly what the number measures.
similar "The Matrix" returns The Matrix Reloaded first at about 0.67, followed by more science-fiction and action films.If it breaks'uv' is not recognised + 2 more
'uv' is not recognised
winget install --id astral-sh.uv -e (Windows) or curl -LsSf https://astral.sh/uv/install.sh | sh (Mac or Linux), then open a new terminal. Full walkthrough in Install anything from GitHub.'No .json catalogue files in ...catalogue'
films.json is in the wrong place. It must be inside a catalogue sub-folder of the project, not next to recommend.py. Make the folder and move it there.