Check Every Citation

When a call succeeds, the rules desk gives two kinds of reply. When the rulebooks cover a question, it passes on the model’s answer. When they don’t, reply() gives the fixed “the rulebooks don’t say” reply. A player who gets an answer will also want the page it came from, so the table can open the rulebook and settle the argument.

In this lesson, you’ll show each answer’s sources as a game and a page. Then you’ll check in code that every citation the model returns refers to a chunk you sent, and you’ll make the desk say it couldn’t find the rule when a citation fails that check. Start with what the player sees.

Show the Game and Page

A citation such as lantern-harbor-11 means something to your code but nothing to a player. The player needs “Lantern Harbor, page 4.” Each chunk you sent already holds its game and page, and answerQuestion() returns those chunks as result.chunks, so the desk can look each cited ID up there.

The desk’s replies are about to grow, and the next scripts will need them too, so give them a module of their own. Create desk.js in the project folder, and copy DONT_SAY and reply() from ask-covered.js into it, with export in front of each.

Two things change as you copy. The fixed reply gets shorter, “The rulebooks don’t say.”, because a player at the table needs the verdict, not how the desk searched. And an answer that cites nothing will get a reply of its own later in this lesson, because claiming a rule without a page is a different failure from a rule the rulebooks don’t cover. Until then, keep the last lesson’s check for it.

If you’d like a refresher on sharing code between files, the Modules and JSON course covers import and export. Then make the covered branch add the sources:

Language: JavaScript Filename: desk.js
export const DONT_SAY =
  "The rulebooks don't say. Please ask a staff member.";

function sourceLabel(id, chunks) {
  const chunk = chunks.find((entry) => entry.id === id);
  return `${chunk.game}, page ${chunk.page}`;
}

export function reply(result) {
  if (!result.record) {
    return "Sorry, the desk can't answer right now.";
  }
  const { covered, answer, citations } = result.record;
  if (!covered || citations.length === 0) {
    return DONT_SAY;
  }
  const labels = citations.map((id) => sourceLabel(id, result.chunks));
  const pages = [...new Set(labels)];
  return `${answer}\nSources: ${pages.join("; ")}`;
}

sourceLabel() finds the chunk whose id matches the citation and turns it into a label with a game and a page. Two chunks often come from the same page, as lantern-harbor-10 and lantern-harbor-11 both do, so a Set drops the repeated labels before .join() puts them on one line.

To try it, create ask-desk.js, which asks table 6’s trading question and table 5’s Glasshouse scoring question. It prints what the desk would say, then each call’s token counts and cost:

Language: JavaScript Filename: ask-desk.js
import { readFile } from "node:fs/promises";
import { answerQuestion } from "./answer.js";
import { costInCents } from "./cost.js";
import { reply } from "./desk.js";

const text = await readFile("data/questions.json", "utf8");
const questions = JSON.parse(text);

for (const id of [10, 12]) {
  const question = questions.find((entry) => entry.id === id);
  const result = await answerQuestion(question.text);
  console.log(`Table ${question.table}: ${question.text}`);
  console.log(`Desk: ${reply(result)}`);
  const { inputTokens, outputTokens } = result.usage;
  const cents = costInCents(result.usage).toFixed(3);
  console.log(
    `${inputTokens} input and ${outputTokens} output tokens, ` +
      `${cents} cents`
  );
}

Run node --env-file=.env ask-desk.js. Here’s one real run with OpenAI’s capable model, with the long lines wrapped for display. Your wording, token counts, and cost will differ:

Language: Program Output
Table 6: Lantern Harbor: can I trade fish for oil with someone who isn't
next to me?
Desk: Yes, as long as your boats are at the same pier; where you sit at
the table doesn’t matter. Once on your turn, before or after your
actions, you can trade one fish for one oil if both players agree.
Sources: Lantern Harbor, page 4
672 input and 81 output tokens, 0.215 cents
Table 5: How does scoring work at the end of Glasshouse? Do unfinished
greenhouses count for anything?
Desk: After round eight, add 5 points for each finished greenhouse and 2
points for each grown plant to the points you earned during the rounds.
Unfinished greenhouses score nothing for their panes, but grown plants
inside them still score 2 points each; seedlings score nothing. The
highest score wins, with ties broken by most finished greenhouses; if
still tied, the players share the win.
Sources: Glasshouse, page 4; Glasshouse, page 5
521 input and 122 output tokens, 0.226 cents

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

Become a Member to join the conversation.