Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Checking what a model saw

After this lesson: I can check each claim in a model's description against known facts and decide whether to accept the reply.

75 min lesson7 commandsAI coding agent lab: use opencode to explain and fix code, and check its work

The description sounds right, but is it?

A model describes a photo: two cats, one red ball and a sign that says OPEN. It sounds sure. But a model can add a dog that is not there, or say three cats when there are two.

This lesson shows how to check each claim against what you know about the input.

By the end of this lesson

Note

I can check each claim in a model's description against known facts and decide whether to accept the reply.

Key words

WordWhat it means
Claimone thing a reply says about the input
Evidencethe known fact that a claim can be checked against
Inventeda claim about something that is not in the input
Uncheckeda claim with no fact to check it against
Verdictthe result of checking one claim

The cells students fill in are left blank in this preview.

Split a reply into claims

A reply is easier to check when you cut it into claims, each one small enough to be true or false. "Two cats and a red ball" is three claims.

You can only check a claim you have a fact for. The facts may come from the input itself, such as counting items in a grid, or from a person who knows the picture.

Four claims and their facts

ClaimFact heldVerdict
There are 2 catscats: 2supported
There are 3 catscats: 2wrong number
There is a dogno dog listedinvented
The door is redno colour for the doorunchecked

The cells students fill in are left blank in this preview.

Different faults need different verdicts

A wrong number, a wrong colour and an invented thing are different faults. An invented thing is the worst, because the reply is about something that is not there.

So the decision follows the worst verdict. Any invented claim means reject. A wrong or unchecked claim means a person reviews it. Only a reply with nothing wrong is accepted.

A common mistake

Warning

Many people accept a reply because most of it is right. Four true claims do not make a fifth one true. Check every claim. One invented thing can be enough to reject the whole reply.

Write the model helper, part 1

Command
docker exec -w /home/node/project agent sh -c "printf '%s\n' 'const base = process.env.AI_URL || \"http://ai:4000\";' '// The token counts of a reply, or null when the reply carries none.' 'function count(data, name) {' '  return data.usage && typeof data.usage[name] === \"number\" ? data.usage[name] : null;' '}' > model.js"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Write the model helper, part 2

Command
docker exec -w /home/node/project agent sh -c "printf '%s\n' 'async function ask(messages, sample, max) {' '  try {' '    const res = await fetch(base + \"/v1/chat/completions\", {' '      method: \"POST\",' '      headers: { \"content-type\": \"application/json\", authorization: \"Bearer lab\" },' '      body: JSON.stringify({ model: \"lab\", messages: messages, max_tokens: max || 100 }),' '      signal: AbortSignal.timeout(30000),' '    });' '    if (res.ok === false) throw new Error(\"status \" + res.status);' '    const data = await res.json();' '    const text = data.choices[0].message.content;' '    return { text, inTokens: count(data, \"prompt_tokens\"), outTokens: count(data, \"completion_tokens\"), recorded: false };' '  } catch (error) {' '    console.log(\"Recorded sample: no model answered (\" + error.message + \").\");' '    return { text: sample, inTokens: null, outTokens: null, recorded: true };' '  }' '}' 'module.exports = { ask };' >> model.js"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Write the checking script, part 1

Command
docker exec -w /home/node/project agent sh -c "printf '%s\n' 'const { ask } = require(\"./model\");' 'const grid = [\"..R...\", \".B..R.\", \"R.....\", \"...G..\"];' 'const claims = [' '  { kind: \"count\", thing: \"R\", value: 3 },' '  { kind: \"count\", thing: \"B\", value: 2 },' '  { kind: \"count\", thing: \"Y\", value: 1 },' '  { kind: \"exists\", thing: \"G\" },' '];' > check5.js"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Write the checking script, part 2

Command
docker exec -w /home/node/project agent sh -c "printf '%s\n' 'function facts(g) {' '  const counts = {};' '  for (const row of g) {' '    for (const ch of row) {' '      if (ch === \".\") continue;' '      counts[ch] = (counts[ch] || 0) + 1;' '    }' '  }' '  return counts;' '}' >> check5.js"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Write the checking script, part 3

Command
docker exec -w /home/node/project agent sh -c "printf '%s\n' 'function verdict(counts, c) {' '  if (c.kind === \"exists\") return c.thing in counts ? \"supported\" : \"invented\";' '  if ((c.thing in counts) === false) return \"invented\";' '  return counts[c.thing] === c.value ? \"supported\" : \"wrong number\";' '}' >> check5.js"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Write the checking script, part 4

Command
docker exec -w /home/node/project agent sh -c "printf '%s\n' 'async function main() {' '  const counts = facts(grid);' '  console.log(\"facts \" + JSON.stringify(counts));' '  for (const c of claims) {' '    const value = c.value === undefined ? \"\" : \" \" + c.value;' '    console.log(c.kind + \" \" + c.thing + value + \": \" + verdict(counts, c));' '  }' '  const q = \"In one short sentence, why should a description of a picture be checked against the picture?\";' '  const reply = await ask([{ role: \"user\", content: q }], \"A description can add things or get a number wrong, so check it against the picture.\", 40);' '  console.log(\"reply: \" + reply.text);' '}' 'main();' >> check5.js"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Check four recorded claims

Command
docker exec -w /home/node/project agent node check5.js

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

The lines it prints

LineWhat it shows
facts {"R":3,"B":1,"G":1}What the script counted in its grid of letters
count R 3: supportedThe claim matches the count
count B 2: wrong numberThe count was 1
count Y 1: inventedThere is no Y in the grid

The cells students fill in are left blank in this preview.

About this lab and its source: AI coding agent lab: use opencode to explain and fix code, and check its work

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.