knowledge · beginner · 30 min
Search by meaning
Turn text into vectors and find the closest sentences, first in memory and then in a database.
HANDS-ON LAB · YOU RUN IT ON YOUR MACHINE · 4 STEPS
-
Pull an embedding model and embed one sentence
Run this to check
ollama pull nomic-embed-text && curl -s http://localhost:11434/api/embed -d '{"model":"nomic-embed-text","input":"my box arrived crushed"}' | head -c 200You pass when
JSON with an embeddings array of numbers.
-
Write 30 sentences about parcels and rank them against a question by cosine similarity
Run this to check
uv run --with numpy --with requests search.py "my box arrived crushed"You pass when
The top three are about damage, though none of them contains the word crushed.
Files you write
- sentences.txt
- search.py
-
Start Postgres with pgvector
Run this to check
docker run -d --name learn-pg -e POSTGRES_PASSWORD=learn -p 5433:5432 pgvector/pgvector:pg17 && sleep 5 && docker exec learn-pg psql -U postgres -c "CREATE EXTENSION IF NOT EXISTS vector; SELECT extversion FROM pg_extension WHERE extname='vector';"You pass when
One row with the extension version.
-
Store the vectors in a table and run the same search in SQL
Run this to check
uv run --with numpy --with requests --with "psycopg[binary]" search.py --db "my box arrived crushed"You pass when
The same top three as the in-memory search.
Files you write
- search.py
No transcript here on purpose. This page does not show terminal output, because model answers differ from run to run. Each step says what a pass looks like; your own terminal is the proof.