Add sandboxed Python for the assistant and a Python check of generated quizzes
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 51s
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 51s
- Pyodide (Python 3.14, WebAssembly) in a separate Node process under the permission model: read-only access to Pyodide's files, no writes, processes or network, empty environment, JS bridges removed, time limit with kill and restart. - Assistant tool run_python, which can load a draft or quiz as Python data. - After quiz generation the model checks question count, type mix, correct answers per type, duplicates and recomputed calculations with Python, then submits fixes that are applied only if the question still validates. Switch in the Generate dialog. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
f0aabe4d5d
commit
5ea61c882c
13 changed files with 800 additions and 6 deletions
290
server/ai/verify.ts
Normal file
290
server/ai/verify.ts
Normal file
|
|
@ -0,0 +1,290 @@
|
|||
/**
|
||||
* Python-backed checking of generated quizzes, and the `run_python` tool the assistant uses.
|
||||
*
|
||||
* After a quiz is generated, a short agent loop gives the model the draft (as Python data and
|
||||
* as text) plus the original request and asks it to verify everything mechanically with the
|
||||
* sandbox: question count, the select-all / single / numeric mix, options and correct answers
|
||||
* per type, duplicates, and every calculation recomputed from its stem. It then submits fixes,
|
||||
* which are applied here only if the question still validates afterwards.
|
||||
*/
|
||||
import { z } from 'zod';
|
||||
import { validateDraftQuestion, type DraftQuestion, type QuizDraft } from '../../shared/content.ts';
|
||||
import type { AppContext } from '../http.ts';
|
||||
import { runPython } from './sandbox.ts';
|
||||
import { resolveModel, runChat } from './service.ts';
|
||||
import { AIError, type ChatMessage, type ContentPart, type ToolDef } from './types.ts';
|
||||
|
||||
/* ------------------------------ data handed to Python ------------------------------ */
|
||||
|
||||
/** Compact, 1-based view of a quiz used both in prompts and as the `draft` variable in Python. */
|
||||
export function quizForPython(quiz: { title: string; questions: Pick<DraftQuestion, 'type' | 'stem' | 'options' | 'rationale' | 'topic'>[] }) {
|
||||
return {
|
||||
title: quiz.title,
|
||||
question_count: quiz.questions.length,
|
||||
questions: quiz.questions.map((q, i) => ({
|
||||
number: i + 1,
|
||||
type: q.type,
|
||||
topic: q.topic,
|
||||
stem: q.stem,
|
||||
options: q.options.map((o, k) => ({ number: k + 1, text: o.text, correct: Boolean(o.correct), why: o.why ?? '' })),
|
||||
rationale: q.rationale ?? '',
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
/** Python source that defines the given variables from JSON (a JSON string is a valid Python string literal). */
|
||||
export function pythonPrelude(vars: Record<string, unknown>): string {
|
||||
const lines = ['import json as _json'];
|
||||
for (const [name, value] of Object.entries(vars)) {
|
||||
if (!/^[a-z_][a-z0-9_]*$/i.test(name)) continue;
|
||||
lines.push(`${name} = _json.loads(${JSON.stringify(JSON.stringify(value))})`);
|
||||
}
|
||||
return `${lines.join('\n')}\n`;
|
||||
}
|
||||
|
||||
/** Formats a sandbox result for a model: short, and never an exception. */
|
||||
export function pythonOutput(r: Awaited<ReturnType<typeof runPython>>) {
|
||||
return {
|
||||
ok: r.ok,
|
||||
...(r.stdout ? { stdout: r.stdout } : {}),
|
||||
...(r.result !== null ? { result: r.result } : {}),
|
||||
...(r.stderr ? { stderr: r.stderr.slice(0, 2000) } : {}),
|
||||
...(r.error ? { error: r.error } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
export const RUN_PYTHON_DESCRIPTION =
|
||||
'Run Python 3 in a sandbox and get stdout and the value of the last expression. Standard library only (math, fractions, decimal, statistics, re, json, collections, itertools, datetime); no files, network or packages; 10 second limit. Use it whenever exactness matters: recompute dosage, rate and conversion answers, count questions, options and correct answers, check lengths, tally topics. Print what you need to see.';
|
||||
|
||||
export const RUN_PYTHON_TOOL: ToolDef = {
|
||||
name: 'run_python',
|
||||
description: RUN_PYTHON_DESCRIPTION,
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: { code: { type: 'string', description: 'Python source. Variables listed in the instructions are already defined.' } },
|
||||
required: ['code'],
|
||||
},
|
||||
};
|
||||
|
||||
/* ------------------------------ verification of a generated quiz ------------------------------ */
|
||||
|
||||
export interface QuizRequestFacts {
|
||||
count: number;
|
||||
style: string;
|
||||
sataShare: number;
|
||||
difficulty?: string;
|
||||
}
|
||||
|
||||
/** Long text is shortened rather than rejected: a verbose but correct review must not be lost. */
|
||||
const text = (max: number) => z.coerce.string().transform((v) => v.slice(0, max));
|
||||
const int = z.coerce.number().int();
|
||||
const fixSchema = z.object({
|
||||
question: int.min(1),
|
||||
action: z.enum(['set_correct', 'set_numeric_answer', 'set_type', 'remove']),
|
||||
correct_options: z.array(int.min(1)).max(12).optional(),
|
||||
answer: text(200).optional(),
|
||||
type: z.enum(['single', 'multi', 'numeric']).optional(),
|
||||
reason: text(600).optional().default(''),
|
||||
});
|
||||
const reviewSchema = z.object({
|
||||
summary: text(1500).optional().default(''),
|
||||
checks: z
|
||||
.array(z.object({ name: text(160), passed: z.coerce.boolean(), detail: text(600).optional().default('') }))
|
||||
.transform((v) => v.slice(0, 40))
|
||||
.optional()
|
||||
.default([]),
|
||||
fixes: z.array(z.unknown()).optional().default([]).transform((list) => list.flatMap((f) => { const r = fixSchema.safeParse(f); return r.success ? [r.data] : []; })),
|
||||
});
|
||||
export type QuizReview = z.infer<typeof reviewSchema>;
|
||||
|
||||
const SUBMIT_REVIEW: ToolDef = {
|
||||
name: 'submit_review',
|
||||
description: 'Finish the check: report every check you ran (with its result) and the fixes to apply. Call this exactly once, at the end.',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
summary: { type: 'string', description: 'One or two sentences for the student.' },
|
||||
checks: {
|
||||
type: 'array',
|
||||
items: { type: 'object', properties: { name: { type: 'string' }, passed: { type: 'boolean' }, detail: { type: 'string' } }, required: ['name', 'passed'] },
|
||||
},
|
||||
fixes: {
|
||||
type: 'array',
|
||||
description: 'Only for definite errors. Question and option numbers are 1-based.',
|
||||
items: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
question: { type: 'integer' },
|
||||
action: {
|
||||
type: 'string',
|
||||
enum: ['set_correct', 'set_numeric_answer', 'set_type', 'remove'],
|
||||
description: 'set_correct: replace which options are correct. set_numeric_answer: replace the answer of a numeric question (number with unit). set_type: change single/multi to match the correct answers. remove: drop an unfixable question.',
|
||||
},
|
||||
correct_options: { type: 'array', items: { type: 'integer' } },
|
||||
answer: { type: 'string' },
|
||||
type: { type: 'string', enum: ['single', 'multi', 'numeric'] },
|
||||
reason: { type: 'string' },
|
||||
},
|
||||
required: ['question', 'action', 'reason'],
|
||||
},
|
||||
},
|
||||
},
|
||||
required: ['summary', 'checks', 'fixes'],
|
||||
},
|
||||
};
|
||||
|
||||
function rulesFor(req: QuizRequestFacts): string {
|
||||
const expectedMulti = req.sataShare <= 0 ? 'none' : req.sataShare >= 1 ? 'all non-numeric questions' : `about ${Math.round(req.sataShare * 100)}% of the non-numeric questions`;
|
||||
return [
|
||||
`Requested: ${req.count} questions, style "${req.style}", difficulty "${req.difficulty ?? 'mixed'}", select-all (multi) share: ${expectedMulti}.`,
|
||||
'Rules each question must meet:',
|
||||
'- single: exactly one correct option; 4 options for NCLEX/standard/recall styles.',
|
||||
'- multi (select all that apply): at least 2 correct options; for NCLEX style 5 or 6 options with 2 to 5 correct and a stem ending with "Select all that apply."',
|
||||
'- numeric: the student types a number; options holds the accepted answer(s), all marked correct. The answer must equal the value computed from the stem, rounded as the stem says.',
|
||||
'- No duplicate or near-duplicate stems; no identical options within a question; no "all of the above" / "none of the above".',
|
||||
'- The keyed answer must be the clinically correct one (check against the rationale and standard practice).',
|
||||
].join('\n');
|
||||
}
|
||||
|
||||
const VERIFY_SYSTEM = `You are the quality checker for quiz questions written for a nursing student. Accuracy is critical: they are used to prepare for licensing exams.
|
||||
Check the draft mechanically with the run_python tool. The variables \`draft\` (the quiz) and \`request\` (what was asked for) are already defined in every run. Write Python that counts and verifies instead of eyeballing:
|
||||
1. The number of questions against request["count"].
|
||||
2. The mix of types (single / multi / numeric) against the requested select-all share.
|
||||
3. For every question: option count, number of correct options, and that it fits its type's rules.
|
||||
4. Duplicate stems or options.
|
||||
5. Every numeric question: recompute the answer from the numbers in the stem with Python, apply the rounding the stem asks for, and compare with the keyed answer numerically (ignore units).
|
||||
Also read each question and judge whether the keyed answer is clinically correct.
|
||||
Then call submit_review once with every check you ran and a fix for each definite error. Do not "fix" things that are only stylistic. A count or mix that is off cannot be fixed here; report it as a failed check.`;
|
||||
|
||||
/** Applies validated fixes; returns notes describing what changed or was refused. */
|
||||
export function applyQuizFixes(questions: DraftQuestion[], fixes: z.infer<typeof fixSchema>[]): { questions: DraftQuestion[]; applied: string[]; refused: string[] } {
|
||||
const out = questions.map((q) => ({ ...q, options: q.options.map((o) => ({ ...o })) }));
|
||||
const remove = new Set<number>();
|
||||
const applied: string[] = [];
|
||||
const refused: string[] = [];
|
||||
for (const fix of fixes) {
|
||||
const i = fix.question - 1;
|
||||
const q = out[i];
|
||||
if (!q) {
|
||||
refused.push(`Question ${fix.question}: does not exist.`);
|
||||
continue;
|
||||
}
|
||||
const before = { ...q, options: q.options.map((o) => ({ ...o })) };
|
||||
const why = fix.reason ? ` (${fix.reason.replace(/\s+/g, ' ').slice(0, 200)})` : '';
|
||||
if (fix.action === 'remove') {
|
||||
remove.add(i);
|
||||
applied.push(`Question ${fix.question} removed${why}.`);
|
||||
continue;
|
||||
}
|
||||
if (fix.action === 'set_correct' && fix.correct_options?.length) {
|
||||
const set = new Set(fix.correct_options.map((n) => n - 1));
|
||||
q.options = q.options.map((o, k) => ({ ...o, correct: set.has(k) }));
|
||||
if (q.type === 'single' && set.size > 1) q.type = 'multi';
|
||||
} else if (fix.action === 'set_numeric_answer' && fix.answer?.trim() && q.type === 'numeric') {
|
||||
q.options = [{ text: fix.answer.trim(), correct: true, why: q.options[0]?.why ?? '' }];
|
||||
} else if (fix.action === 'set_type' && fix.type && fix.type !== 'numeric') {
|
||||
q.type = fix.type;
|
||||
} else {
|
||||
refused.push(`Question ${fix.question}: the suggested ${fix.action.replace(/_/g, ' ')} was incomplete.`);
|
||||
continue;
|
||||
}
|
||||
if (validateDraftQuestion(q, i).some((p) => p.level === 'error')) {
|
||||
out[i] = before;
|
||||
refused.push(`Question ${fix.question}: the suggested ${fix.action.replace(/_/g, ' ')} would have made it invalid, so it was not applied${why}.`);
|
||||
continue;
|
||||
}
|
||||
applied.push(`Question ${fix.question}: ${fix.action === 'set_numeric_answer' ? `answer corrected to ${fix.answer!.trim()}` : fix.action === 'set_type' ? `type changed to ${fix.type}` : `answer key corrected`}${why}.`);
|
||||
}
|
||||
return { questions: out.filter((_, i) => !remove.has(i)), applied, refused };
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks a generated quiz with the model and the Python sandbox, then applies the fixes.
|
||||
* Never throws: a failed check leaves the draft as it was and says so in the notes.
|
||||
*/
|
||||
export async function verifyQuizDraft(
|
||||
app: AppContext,
|
||||
draft: QuizDraft,
|
||||
req: QuizRequestFacts,
|
||||
opts: { modelId?: string | null; signal?: AbortSignal; onStep?: (label: string) => void } = {},
|
||||
): Promise<{ draft: QuizDraft; notes: string[]; review: QuizReview | null; pythonRuns: number }> {
|
||||
const resolved = resolveModel(app, 'generate', opts.modelId);
|
||||
if (!resolved.model.capabilities.tools) {
|
||||
return { draft, notes: ['The Python answer check was skipped: the generation model cannot use tools.'], review: null, pythonRuns: 0 };
|
||||
}
|
||||
const data = quizForPython(draft);
|
||||
const prelude = pythonPrelude({ draft: data, request: { count: req.count, style: req.style, sata_share: req.sataShare, difficulty: req.difficulty ?? 'mixed' } });
|
||||
const listing = data.questions
|
||||
.map((q) => `Q${q.number} [${q.type}] ${q.stem}\n${q.options.map((o) => ` ${o.number}. ${o.correct ? '(correct) ' : ''}${o.text}`).join('\n')}${q.rationale ? `\n Rationale: ${q.rationale.slice(0, 600)}` : ''}`)
|
||||
.join('\n\n');
|
||||
const messages: ChatMessage[] = [{ role: 'user', content: [{ type: 'text', text: `${rulesFor(req)}\n\nTHE DRAFT (${data.question_count} questions):\n\n${listing}` }] }];
|
||||
let review: QuizReview | null = null;
|
||||
let pythonRuns = 0;
|
||||
// Up to WORK_STEPS turns with Python, then only submit_review is offered until it arrives.
|
||||
const WORK_STEPS = 8;
|
||||
const MAX_STEPS = 12;
|
||||
try {
|
||||
for (let step = 0; step < MAX_STEPS && !review; step += 1) {
|
||||
if (opts.signal?.aborted) throw new AIError('aborted', 'Cancelled.');
|
||||
const wrapUp = step >= WORK_STEPS || pythonRuns >= 16;
|
||||
if (wrapUp && !messages.some((m) => m.content.some((p) => p.type === 'text' && p.text.startsWith('You have run enough checks')))) {
|
||||
const nudge: ContentPart = { type: 'text', text: 'You have run enough checks. Call submit_review now with your results and fixes.' };
|
||||
const lastMsg = messages[messages.length - 1];
|
||||
// Ride along with the tool results so user and assistant turns keep alternating.
|
||||
if (lastMsg?.role === 'user') lastMsg.content.push(nudge);
|
||||
else messages.push({ role: 'user', content: [nudge] });
|
||||
}
|
||||
const res = await runChat(app, {
|
||||
task: 'generate',
|
||||
purpose: 'quiz check',
|
||||
modelId: opts.modelId,
|
||||
system: VERIFY_SYSTEM,
|
||||
messages,
|
||||
tools: wrapUp ? [SUBMIT_REVIEW] : [RUN_PYTHON_TOOL, SUBMIT_REVIEW],
|
||||
toolChoice: wrapUp ? { name: 'submit_review' } : 'auto',
|
||||
maxOutputTokens: 16_000,
|
||||
signal: opts.signal,
|
||||
});
|
||||
messages.push(res.message);
|
||||
const calls = res.message.content.filter((p): p is Extract<ContentPart, { type: 'tool_call' }> => p.type === 'tool_call');
|
||||
if (calls.length === 0) {
|
||||
messages.push({ role: 'user', content: [{ type: 'text', text: 'Run your checks with run_python, then call submit_review.' }] });
|
||||
continue;
|
||||
}
|
||||
const results: ContentPart[] = [];
|
||||
for (const call of calls) {
|
||||
if (call.name === 'submit_review') {
|
||||
const parsed = reviewSchema.safeParse(call.input);
|
||||
if (parsed.success) review = parsed.data;
|
||||
results.push({ type: 'tool_result', toolCallId: call.id, name: call.name, output: parsed.success ? '{"ok":true}' : JSON.stringify({ error: parsed.error.issues.map((i) => `${i.path.join('.')}: ${i.message}`).join('; ') }), isError: !parsed.success });
|
||||
} else if (call.name === 'run_python' && pythonRuns >= 16) {
|
||||
results.push({ type: 'tool_result', toolCallId: call.id, name: call.name, output: '{"error":"Python budget used up. Call submit_review now."}', isError: true });
|
||||
} else if (call.name === 'run_python') {
|
||||
pythonRuns += 1;
|
||||
opts.onStep?.(`Checking the questions with Python (run ${pythonRuns})`);
|
||||
const r = await runPython(prelude + String(call.input.code ?? ''));
|
||||
results.push({ type: 'tool_result', toolCallId: call.id, name: call.name, output: JSON.stringify(pythonOutput(r)), isError: !r.ok });
|
||||
} else {
|
||||
results.push({ type: 'tool_result', toolCallId: call.id, name: call.name, output: '{"error":"unknown tool"}', isError: true });
|
||||
}
|
||||
}
|
||||
// The last user message carries the tool results; keep wrap-up instructions in front of the model.
|
||||
messages.push({ role: 'user', content: results });
|
||||
}
|
||||
} catch (err) {
|
||||
if (err instanceof AIError && err.code === 'aborted') throw err;
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
return { draft, notes: [`The Python answer check could not finish (${message.slice(0, 200)}). Review the answers yourself.`], review: null, pythonRuns };
|
||||
}
|
||||
if (!review) return { draft, notes: ['The Python answer check did not report back. Review the answers yourself.'], review: null, pythonRuns };
|
||||
|
||||
const { questions, applied, refused } = applyQuizFixes(draft.questions, review.fixes);
|
||||
const failed = review.checks.filter((c) => !c.passed);
|
||||
const notes = [
|
||||
`Checked with Python (${pythonRuns} run${pythonRuns === 1 ? '' : 's'}): ${review.checks.length - failed.length} of ${review.checks.length} checks passed${applied.length ? `, ${applied.length} fix${applied.length === 1 ? '' : 'es'} applied` : ''}.`,
|
||||
...failed.map((c) => `Check failed: ${c.name}${c.detail ? `: ${c.detail}` : ''}`),
|
||||
...applied,
|
||||
...refused,
|
||||
];
|
||||
return { draft: { ...draft, questions }, notes, review, pythonRuns };
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue