Search by meaning
Turn text into vectors and find the closest sentences, first in memory and then in a database.
You build: search.py: finds the right sentence even when it shares no words with the question.
- Python 3.11 or newer and uv
- Ollama
- Docker
-
01Pull an embedding model and embed one sentence
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.
-
02Write 30 sentences about parcels and rank them against a question by cosine similarity
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.
-
03Start Postgres with pgvector
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.
-
04Store the vectors in a table and run the same search in SQL
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.
The check runs on the computer, not on your word: a step passes when its command exits with 0 there.
Computers are not set up on this site yet. Run each command on your own machine and compare with "You pass when".
Commands run in labs/20-embeddings and may take up to 60 seconds each. Every command starts a new shell: put keys and settings in a .env file there, it is loaded each time.