Recording the model's answers
Answers written down once and given back the same on every run, for tests and demos.
Sam writes a test for the card: when System One judges formal, the card says “Many happy returns”. A real model answers a little differently each time, takes seconds and needs a network. The test needs the same answer on every run, and it should fail loudly when the card asks something nobody answered.
You will learn
- How to answer from a list with
recorded - What a
Recordingholds - How to write down a model's answers, each for its inputs
- How a test fails on a request nobody recorded:
strictandMissingRecording
Answers from a list
Sam's card, with System One and System Two both answered from a list:
/** @jsxImportSource @reintent/core */
import { action, choices, createContext, host, useChoiceStatus, useGenerate, type Id } from '@reintent/core';
export type Person = { to: string; age: number; from: string };
export const TONES = ['warm', 'funny', 'formal'] as const;
export type Tone = (typeof TONES)[number];
export const GRANDMA: Person = { to: 'Grandma', age: 90, from: 'Sam' };
/** What a button does: the intent gives it to every evaluation (index.intent.tsx). */
export const Decide = createContext<(id: Id, value: unknown) => void>('Decide', { key: () => undefined });
/** What “no preference” does: forget the person's decision, so the choice resolves on its own again. */
export const Forget = createContext<(id: Id) => void>('Forget', { key: () => undefined });
export const LOOK: Record<Tone, { background: string; accent: string }> = {
warm: { background: '#fdebd3', accent: '#b5562b' },
funny: { background: '#fff2a8', accent: '#7a4fd6' },
formal: { background: '#f1eee8', accent: '#2b2b2b' },
};
export function title(p: Person, tone: Tone) {
if (tone === 'formal') return `Many happy returns, ${p.to}.`;
if (tone === 'warm') return `Happy ${p.age}th birthday, ${p.to}!`;
return `${p.age} laps around the sun, ${p.to}!`;
}
/** The card's tone, and which layer it came from: System One weighs it; a person's decision is above its judgment. */
export function useToneStatus() {
return useChoiceStatus(choices<Tone>(...TONES), {
of: 'card',
name: 'tone',
defaultValue: 'warm',
question: 'Which tone suits a birthday card for Grandma, turning 90?',
ask: ['model'],
});
}
export function useTone() {
return useToneStatus().value;
}
export function Card(p: Person) {
const tone = useTone();
const message = useGenerate<string>({
of: 'card',
name: 'message',
instruction: 'Write the message inside a birthday card: two short sentences in this tone.',
uses: { to: p.to, age: p.age, tone },
});
return host('div', {
style: {
maxWidth: 380,
padding: '1.5em',
borderRadius: 12,
background: LOOK[tone].background,
color: '#3a2a1c',
},
children: [
host('h1', { style: { color: LOOK[tone].accent }, children: title(p, tone) }),
host('p', { children: message.value ?? '…' }),
host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: `— ${p.from}` }),
],
});
}
export function Buttons() {
const tone = useToneStatus();
const row = <T extends string>(name: string, values: readonly T[], now: { value: T; source: string }) =>
host('p', {
children: [
...values.map((v) =>
// A person's decision is pressed; a value the card took on its own is only marked.
host('button', {
disabled: now.source === 'decision' && v === now.value,
...(now.source !== 'decision' &&
v === now.value && { style: { textDecoration: 'underline dotted' } }),
onClick: action.decide({ of: 'card', name }, v),
children: v,
}),
),
// The person's way of not deciding: forget the decision, and the card picks again.
host('button', {
disabled: now.source !== 'decision',
onClick: action.forget({ of: 'card', name }),
style: {
marginLeft: 12,
borderStyle: 'dashed',
fontStyle: 'italic',
...(now.source === 'decision' && { background: 'transparent' }),
},
children: 'no preference',
}),
],
});
return host('div', { children: [row('tone', TONES, tone)] });
}
export default function App() {
return host('div', { children: [<Card {...GRANDMA} />, <Buttons />] });
}
Starting…
Starting…
Nothing logged.
recorded(recordings) returns a Model that answers from the list. Each Recording holds:
effect:'judge','generate'or'reprogram';id: the choice's key, itsofandnamejoined by#, herecard#tone;answer: weights for a judge, the value for a generate;stamp, optional: the inputs it answers. Without one, it answers any inputs;goal, optional: for a judgment asked again by arejudge({ goal })step, the goal it was asked under (When no possibility fits).
System One leans funny here, so the card is funny on every run. Press formal under the card. The message has new inputs, and the same recorded message answers them, because it has no stamp. Change the judge's weights so that warm leads, and the card turns warm.
A recorded reprogramming names its program
A recording with effect: 'reprogram' answers { program, summary }, where program is a name, and its id is the prompt it answers. recorded(recordings, { programs }) maps each name to the program itself, the way Quick start's recordedModel({ family: <Family /> }) gives the family card (Quick start).
Writing down your model's answers
Sam asks the model once, writes its answers down, and runs the card from them afterwards:
/** @jsxImportSource @reintent/core */
import {
createIntent,
recorded,
type Id,
type Model,
type ModelRequest,
type Recording,
} from '@reintent/core';
import App, { Decide, Forget } from './App.intent';
/** Sam's System One, as in the step before. */
function judge(r: ModelRequest) {
const formal = r.facts?.some((f) => f.includes('formal'));
return formal ? { warm: 0.2, funny: 0.1, formal: 0.7 } : { warm: 0.7, funny: 0.2, formal: 0.1 };
}
/** The card's intent, answered by `model`. */
function open(model: Model) {
const intent = createIntent({
program: <App />,
model,
provide: [
[Decide, (id: Id, value: unknown) => intent.decide(id, value)],
[Forget, (id: Id) => intent.forget(id)],
],
});
return intent;
}
// Once, with the model: every answer is written down with the request's stamp.
const tape: Recording[] = [];
const live = open({
judge: (r) => {
const answer = judge(r);
tape.push({ effect: 'judge', id: r.key, stamp: r.stamp, answer });
return answer;
},
});
live.connect({ commit() {} });
console.log(tape[0].stamp); // → '{"facts":[],"kinds":["warm","funny","formal"],"prompt":"Which tone suits a birthday card for Grandma, turning 90?","uses":{}}'
// From then on, without it: the same answers, from the tape.
const replay = open(recorded(tape));
replay.connect({ commit() {} });
const tone = replay.realize().reads[0];
console.log(tone.value, tone.source); // → 'warm' 'belief'
Starting…
Starting…
Nothing logged.
The live model is wrapped in a function that keeps each answer in tape, together with the request's key and stamp. A second intent answers from recorded(tape), and the card comes out the same, with no model behind it. The stamp ties each recording to the inputs it was given for: the prompt, the possibilities, uses and what was said, in full. recorded answers a request with the recording of the same stamp first, then with one that has no stamp. A request with other inputs, and no recording without a stamp, is left unanswered.
The sandboxes are recorded too
Every sandbox's model.ts, where there is one, is recorded over recordings.json, answers a real model gave once, each with its stamp and its prompt. Open the recordings.json tab in any sandbox that uses the recorded model to read them.
In a test
import assert from 'node:assert/strict';
import { test } from 'node:test';
import { createIntent, MissingRecording, recorded } from '@reintent/core';
import { Card, GRANDMA } from './App.intent';
test('a formal judgment makes a formal card', () => {
const intent = createIntent({
program: <Card {...GRANDMA} />,
strict: true,
model: recorded([{ effect: 'judge', id: 'card#tone', answer: { warm: 0.2, funny: 0.1, formal: 0.7 } }]),
});
const connection = intent.connect({ commit() {} });
assert.equal(intent.realize().reads[0].value, 'formal');
connection.disconnect();
});
test('a question nobody recorded fails the test', () => {
const intent = createIntent({ program: <Card {...GRANDMA} />, strict: true, model: recorded([]) });
assert.throws(() => intent.connect({ commit() {} }), MissingRecording);
});A request with no recording fails with MissingRecording, whose message names the effect, the key and the stamp. createIntent({ strict: true }) throws it from intent.connect(host), so the test fails where the card asked. Without strict, the request is marked 'failed' in intent.pending() and the card goes on with its default (When a request fails).
strict throws what fails at once
Recorded answers come at once, so a missing one throws during intent.connect(host). A function that returns a promise and rejects only marks its request 'failed', even on a strict intent. A strict intent with no model at all throws MissingRecording for the first question it asks.
Recap
recorded(recordings)gives aModelthat answers from a list, byeffectandid.- A recording with a
stampanswers only those inputs; one with no stamp answers any. - Wrapping a live model to keep each answer with the request's
keyandstampwrites the list. - A request with no recording fails with
MissingRecording; on astrictintent it throws.
Last updated on
Edit on GitHub