AI support copilot project (TypeScript) · Chapter 3 of 12
Embeddings in Postgres
By the end of this chapter you know what an embedding is and how to ask a model for many of them at once, you can store 384 numbers in a Postgres column with pgvector and declare it in Drizzle, and you can load every help page and chunk into Postgres in one transaction that a failing model cannot break. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The pages
- 3 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of AI support copilot project (TypeScript) are free. This one comes with the all labs plan.
Why you need this
Chapter 2 cut Parda's 37 current pages into 179 chunks. The search in chapter 4 must find, among those 179, the few that answer a member's question, and it must find them by meaning, not by spelling. Pranav writes "my show got called off, when do I get my money?". The chunk that answers him says "When a show is cancelled, you get a full refund". The two share almost no words: called off and cancelled, money and refund.
So every chunk needs a form a program can compare by meaning, kept where a search reaches it in milliseconds. Today the chunks are cut from the files on every request and thrown away, nothing in the project can turn text into numbers, and the database has no tables.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
pages.ts: the pages and chunks tables
Write the two Drizzle tables in src/db/schema/pages.ts to match the given migration (drizzle/0000_pages_and_chunks.sql). Every column is not null; the property comes first, then its Postgres column.
pages (the table pages): id (id), text and the primary key; title (title), text; updated (updated), a date read as ISO text ({ mode: 'string' }); tags (tags), an array of text that Postgres defaults to an empty array ('{}'::text[]); body (body), text; ingestedAt (ingested_at), timestamptz defaulting to now.
chunks (the table chunks): id (id), text and the primary key; pageId (page_id), text, referencing pages.id with on delete cascade; position (position) and tokens (tokens), integers; heading (heading) and text (text), text; embedding (embedding), a vector of 384 numbers; and a unique constraint called chunks_page_position on (page_id, position).
The Check compares the SQL your tables make with the migration, line by line, then saves a page and a chunk of 384 numbers through your tables and deletes the page.
- Task 2
embeddings.ts: embed texts in batches
Write embedTexts(ai, texts, batchSize = 64) in src/corpus/embeddings.ts. Return one embedding per text, an array of 384 numbers, in the texts' order. Send the texts batchSize at a time, one ai.embeddings.create call per batch with model config.embedModel, input the batch and dimensions config.embedDims; put each answer's data items (each with .embedding and .index) back in index order. No texts: return [] without a request.
The Check embeds 10 texts with batchSize 4 through a model that gives each batch back in reverse order (each item keeps its index), and wants 3 requests of 4, 4 and 2 texts and each embedding in its text's place; then an empty array.
- Task 3 · interview question
Interview: ingest.ts, the pages into Postgres in one transaction
Write ingest(ai, pages) in src/corpus/ingest.ts. Chunk every page (chunkPage with config.chunkTokens and config.chunkOverlap) and embed every chunk's embeddingText with one embedTexts call, before you change the database. Then, in one db.transaction: delete every page (their chunks go with them), insert a row per page (id, title, updated, tags, body), then a row per chunk with its embedding. Return { pages: n, chunks: m }.
The Check ingests data/help (37 pages, 179 chunks; it uses your Tasks 1 and 2, so write them first), reads kids-tickets-03 back, ingests again (nothing doubled), ingests without kids-tickets (it is gone), makes the model fail (the database must be unchanged and the error must reach the caller), sends a page twice (Postgres refuses it, and the database must be unchanged), then calls POST /ingest and GET /health.
Nikhil will ask: why embed everything before the first delete, and do the delete and the inserts in one transaction?
What you will learn
- Embeddings: what the 384 numbers are, and how the embeddings API answers many texts in one request.
- Batches: why and how to send texts a batch at a time, in order.
- pgvector: Postgres's vector(384) type, and arrays of text.
- The tables in Drizzle: vector, .array(), defaults Postgres fills in, a cascading reference, a named unique constraint, matching a given migration.
- One transaction: db.transaction, what other connections see meanwhile, a rollback, and why the model is asked first.
More in The pages
Build "Embeddings in Postgres" yourself
Parda answers the same audience questions all day. You build the copilot that answers them from its own help pages, with sources, and says so when it does not know. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of AI support copilot project (TypeScript) are free. This one comes with the all labs plan.