Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Debug Retrieval Before the Prompt

In this video, you traced a wrong answer from the rules desk back to a chunk the search never sent, and fixed it in the chunker rather than the prompt.

Key Points

  • A wrong answer with a valid citation. Table 3 asked whether a runner who’s already on the summit path can still take the baton in Summit Relay. The desk said no and cited Summit Relay, page 4. Your wording will differ, and so could the answer, but every run here said the same no. Page 4 ends partway through a sentence, “A runner who is already on the summit path can’t take the baton,” and page 5 finishes it: “unless the runner is on space 12, the wide ledge halfway up.” So the answer is yes. The citation passed the desk’s check, because a valid citation only proves the chunk was sent, not that it was the right one.
  • Check retrieval first. It’s tempting to reword the prompt. Instead, check what was retrieved before you touch the prompt or the model.
  • Log what was sent. logChunks() prints each chunk the desk sent, result.chunks, with its rank, score, game, page and heading, the same printout you used to search the rulebooks. The script prints the input tokens too, so you can see what a change costs. Four chunks went to the model, from pages 4, 2, 3 and 3, each score including the game bonus. There was no page 5. The Hand-offs chunk stops mid-sentence, at “can’t take the baton,” and the model answered as if that were the whole rule. The problem is retrieval, not the prompt.
  • Why page 5 wasn’t sent. The chunker never let a chunk cross a page marker, so the start of page 5 became a chunk of its own, labeled with the page alone. It never mentions hand-offs or the baton.
  • Why a bigger k isn’t the fix. With k at 8, the page 5 chunk came in eighth and the desk answered right. But it barely made it, so different wording could leave it out. Every question would pay for eight chunks: 964 input tokens here instead of 609, about half as much again (about 0.27 cents instead of 0.18 for this question). And the rule would still be split.
  • Keep the sentence together. In chunks.js, MID_SENTENCE matches a line that starts with a lowercase letter. It’s a rough test that fits these rulebooks, where other pages open with a heading or a capital. A sentence that breaks before a name or a number would still split, so other documents need their own check. By the time splitSections() reads page 5’s first line, the page marker has already opened page 5’s empty section, so the section before is sections.at(-2). When that first line matches, it finishes the sentence of the section before, so splitSections() pushes it there and sets that section’s endPage to 5. In chunkRulebook(), a section without a carried line ends on its own page (endPage = page). A carried line lands in the section’s last chunk, so only that chunk gets endPage, not a long section’s earlier chunks. Rebuilding the index embedded only the two changed chunks, and two stray chunks like page 5’s were gone: 88 chunks, down from 90.
  • Pages 4 to 5. search() copies endPage into each result, and sourceLabel() in desk.js then gives a page range when a chunk ends on a later page. The same four chunks came back, but the top one now held the exception too. The desk said yes, on space 12, and cited Summit Relay, pages 4-5, so a player who checks finds both halves. That took 625 input tokens, close to the 609 before.
  • Rerun every question. A chunker change touches every rulebook, so rerun check-all.js. Its lines only count citations: question 5’s line looks as it did, though its answer flipped. So print each reply too, and read the answers against the rulebooks. All nine answers still match the rulebooks, and the other three still decline. In the counts, question 7 changed because of the fix: Glasshouse’s frost rule had the same split, so its answer now cites one chunk, pages 3 to 4, not two. Question 12’s count varies from run to run.

The rule:

  1. Log the chunks that were sent (logChunks(result.chunks)).
  2. Right chunk missing: fix retrieval (here, the chunker).
  3. Right chunk sent, answer still wrong: look at the prompt or the model.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Every citation the desk shows is now one it sent. But a sent chunk isn’t always the right one. Here, you’ll trace a wrong answer to its cause.

00:10 Table 3 has a Summit Relay question: can a runner who’s already on the summit path still take the baton? In a new script, trace-question.js, ask it and print the desk’s reply.

00:23 Run it. Your wording will differ, but the desk says no, and cites Summit Relay, page 4.

00:31 In the rulebook, page 4 ends mid-sentence: a runner on the summit path can’t take the baton. Page 5 finishes it: unless the runner is on space 12. So the answer is yes.

00:45 Where did it go wrong? It’s tempting to reword the prompt. Instead, check what was retrieved before you touch the prompt or the model. Add logChunks(), the printout you used to search the rulebooks.

00:59 Call it on result.chunks, the chunks the desk sent, and print the input tokens too, so you can see what a change costs. Rerun it. Four chunks went to the model, from pages 4, 2, 3 and 3.

01:13 Each score includes the game bonus. There’s no page 5. The Hand-offs chunk stops mid-sentence, and the model answered as if that were the whole rule. The problem is retrieval, not the prompt.

01:28 Your chunker never lets a chunk cross a page marker. So the start of page 5 became a chunk of its own, labeled with the page alone. It never mentions hand-offs or the baton.

01:40 Try a bigger k. Pass 8 to answerQuestion(), and run it. The page 5 chunk with no heading comes in eighth, and the desk now gets it right. But that’s not a fix.

01:53 The chunk barely made it, so different wording could leave it out. Every question would pay for eight chunks: 964 input tokens here, up from 609, about half as much again.

02:05 And the rule’s still split. Put k back to 4.

02:10 Open chunks.js, where splitSections() turns pages into sections. Add MID_SENTENCE, a pattern for a line that starts with a lowercase letter. It’s a rough test that fits these rulebooks, where other pages open with a capital.

02:26 Other documents need their own check. The marker already opened page 5’s empty section. When its first line matches, it finishes the sentence of the section before.

02:37 So push it there, and set endPage to 5. In chunkRulebook(), a section without a carried line ends on its own page. A carried line lands in the last chunk, so only that one gets endPage, not a long section’s earlier chunks.

02:54 Rerun build-index.js. It embeds only the two changed chunks, and two stray chunks like page 5’s are gone: 88, down from 90.

03:06 In search.js, copy endPage into each result. In desk.js, sourceLabel() then gives a page range when a chunk ends on a later page. Run trace-question.js again.

03:20 The same four chunks come back, but the top one now holds the exception too. The desk says yes, on space 12, and cites pages 4 to 5, where a player finds both halves.

03:31 That’s 625 input tokens, close to the 609 before.

03:37 A chunker change touches every rulebook, so rerun check-all.js. Its lines only count citations: question 5’s line looks as it did, though its answer flipped.

03:49 So print each reply. All nine answers still match the rulebooks, and three still decline. In the counts, question 7 changed because of the fix. Glasshouse’s frost rule had the same split, so its answer now cites one chunk, pages 3 to 4, not two.

04:07 Question 12’s count varies from run to run.

04:12 So when an answer is wrong, log the chunks that were sent first. If the right chunk is missing, fix retrieval. If it’s there and the answer’s still wrong, look at the prompt or the model.

04:25 Next, in Exercise: Answer With Checked Citations, you’ll write the desk’s answer step yourself.

Become a Member to join the conversation.