The rules desk now has every piece it needs. It splits the rulebooks into chunks with their game and page, searches them by meaning with a bonus for exact card numbers and game names, answers from the top chunks, checks each citation, and says when the rulebooks don’t cover a question. You wrote those pieces one lesson at a time, across six files, as each problem showed up.
In this project, you’ll build the desk again from a brief, as one module, the way you’d build a feature at work. You’ll write it in the next exercise, where tests check each step against a stand-in embedding model and a stand-in language model. Then you’ll bring it into your project and answer real players’ questions using your provider. Start with what the café asked for.
The Brief
On a busy evening, the café’s staff can’t stop to read rulebooks, and the players at table 3 can’t agree whether a runner on the summit path can still take the baton. The café wants a rules desk on its tablets, where a player types a question and the desk answers from the café’s own rulebooks, with the game name and page number so the player can check the answer.
A player will act on whatever the desk says right away, so a wrong rule is worse than no answer. Here’s what the finished desk has to do:
- Chunk the rulebooks with their game and page: Split each of the eight rulebooks into chunks small enough for the embedding model, each one labeled with its game and the heading or page it came from.
- Keep a paragraph that runs over a page together: When a paragraph continues past a page marker, its continuation, rule and exception alike, stays in a chunk with the line before it. That chunk cites the pages it spans, such as “Summit Relay, pages 4-5”.
- Embed the rulebooks once: Embed every chunk ahead of time with the local model, and save the vectors with the model’s name, so a question costs one embedding, not eighty-eight.
- Search by meaning and by exact names: Rank the chunks by similarity to the question, with a bonus for a card or space number and a game name that the question shares with a chunk.
- Answer only from the top chunks: Send the top four chunks between delimiters, each marked with its ID, and ask for an answer with the IDs of the chunks it uses.
- Check every citation, and say when the rulebooks don’t say: Show an answer only when every ID it cites was sent. Reply that the rulebooks don’t say when the model reports they don’t cover the question, and that the desk couldn’t find an answer when a citation fails the check.
The desk must never show a player a rule with no page behind it or a page the model wasn’t sent. The model writes the answers and the citations, so your code checks every citation before a player sees the answer. A citation that passes shows only that the page was sent, not that it says what the answer says.
The Starting Project
You’ll write the desk’s functions in the exercise after this brief, then copy them into the project you’ve grown during the course. Read this section now, and come back to it once the exercise’s tests pass. If you’ve followed along, your folder already has the helpers the desk builds on:
rulebook-text.jsexportsreadLines(),pageNumber(),isHeading(),gameName(), andfitLines(), the helpers for reading the rulebook files.keywords.jsexportsfindNumbers()andfindGames(), andsimilarity.jsexportssimilarity().embed.jsexportsembed()andMODEL_NAME, andchunks.jsexportscountTokens(), which counts tokens with the embedding model’s own tokenizer.model/index.jsexportsaskForJson(), which returns{ record, failure, issues, usage }, andcost.jsexportscostInCents(usage).answer.jsanddesk.jshold the instructions, the schema, and the desk’s fixed replies, which you’ll copy into the new module.
If you’re starting here, the course’s code download has the same files, with chunks.js as it was at the end of Debug Retrieval Before the Prompt. Install the packages with npm install, and check that .env sets MODEL_PROVIDER, your provider’s key, MAX_OUTPUT_TOKENS=400, and MODEL_TIMEOUT_SECONDS=30.
The Plan
The desk goes in a new file, rules-desk.js. Its functions take embed() and askForJson() as arguments instead of importing them, so the exercise’s tests can pass in stand-ins that return fixed vectors and records, with no model download and no key. In your project, the scripts that call these functions pass in the real ones. If you’d like a refresher on functions that take other functions, the Functions You Can Pass Around course covers them.