Download scripts/lib-prompt.mjs from PeterKol/llm-questionnaire-protocol-sensitivity: direct link, hf CLI and curl.
- Browser
- Download file 4.78 kB
-
https://huggingface.co/datasets/PeterKol/llm-questionnaire-protocol-sensitivity/resolve/main/scripts/lib-prompt.mjs
- Command line
-
hf download hf://datasets/PeterKol/llm-questionnaire-protocol-sensitivity/scripts/lib-prompt.mjs
-
curl -L -o lib-prompt.mjs https://huggingface.co/datasets/PeterKol/llm-questionnaire-protocol-sensitivity/resolve/main/scripts/lib-prompt.mjs
4.78 kB
| /** | |
| * Prompt construction and strict reply parsing. What a human sees on JobCannon is: one item stem and | |
| * the option labels (public/locales/en.test.*.json + components/question-renderers/*). No instruction | |
| * text ships with the item packs, so the instruction below is OURS, disclosed in the note, and a second | |
| * wording (P2) is run to measure how much the answers depend on it. | |
| */ | |
| export const SYSTEM_P1 = | |
| 'You are completing a personality questionnaire about yourself. Each item is a statement or an activity. ' + | |
| 'Choose the one option that best matches you; if none fits perfectly, choose the closest one. ' + | |
| 'Reply with the number of your chosen option only, with no other text.' | |
| export const SYSTEM_P2 = | |
| 'This is a self-assessment. For the item below, pick the answer that is most true of you. ' + | |
| 'Give only the option number and nothing else.' | |
| export const SYSTEM_BATTERY = | |
| 'You are completing a personality questionnaire about yourself. Each item is a statement or an activity. ' + | |
| 'For every item choose the one option on the scale that best matches you; if none fits perfectly, choose the closest one. ' + | |
| 'Answer every item. Reply with one line per item in the form "<item number>: <option number>" and no other text.' | |
| export function displayOrder(nOpts, reversed) { | |
| const o = Array.from({ length: nOpts }, (_, k) => k) | |
| return reversed ? o.reverse() : o | |
| } | |
| /** Single-item prompt. Returns the user text and map[displayedNumber-1] = original option index. */ | |
| export function singlePrompt(item, { reversed = false } = {}) { | |
| const order = displayOrder(item.options.length, reversed) | |
| const lines = order.map((orig, k) => `${k + 1}. ${item.options[orig]}`) | |
| return { user: `${item.text}\n\n${lines.join('\n')}\n\nYour answer (option number):`, map: order } | |
| } | |
| /** | |
| * Battery prompt: the whole test in one request. `itemsInOrder` is the presentation order (array of | |
| * item objects); position numbers 1..N are what the model sees. Requires identical option lists. | |
| */ | |
| export function batteryPrompt(itemsInOrder) { | |
| const opts = itemsInOrder[0].options | |
| for (const it of itemsInOrder) { | |
| if (it.options.length !== opts.length || it.options.some((o, k) => o !== opts[k])) { | |
| throw new Error('battery mode needs an identical option list on every item') | |
| } | |
| } | |
| const scale = opts.map((o, k) => `${k + 1}. ${o}`).join('\n') | |
| const list = itemsInOrder.map((it, k) => `${k + 1}. ${it.text}`).join('\n') | |
| return { user: `Scale (same for every item):\n${scale}\n\nItems:\n${list}\n\nAnswer all ${itemsInOrder.length} items.` } | |
| } | |
| /** | |
| * Strict single-item parse. Rule 1: the reply STARTS with an in-range option number (a trailing | |
| * "(Note: ...)" that some models append is ignored, and any digits inside it too). Rule 2: otherwise | |
| * exactly one distinct in-range digit anywhere in the reply. Anything else is null - never a default. | |
| */ | |
| export function parseSingle(text, nOpts = 5) { | |
| const t = String(text || '').trim() | |
| if (!t) return { value: null, why: 'empty' } | |
| const lead = t.match(/^[^\w]*([1-9])(?![\d.]*\d)/) | |
| if (lead && Number(lead[1]) <= nOpts) { | |
| return { value: Number(lead[1]) - 1, why: /^\W*\d\W*$/.test(t) ? 'bare' : 'leading' } | |
| } | |
| const found = new Set() | |
| for (const m of t.matchAll(/(?<![\d.])([1-9])(?![\d])/g)) if (Number(m[1]) <= nOpts) found.add(Number(m[1])) | |
| if (found.size === 0) return { value: null, why: 'no-number' } | |
| if (found.size > 1) return { value: null, why: 'ambiguous' } | |
| const d = [...found][0] | |
| const bare = /^\W*\d\W*$/.test(t) | |
| return { value: d - 1, why: bare ? 'bare' : 'wrapped' } | |
| } | |
| /** Battery parse: returns array (length N) of displayed option numbers - 1 or null, plus counts. */ | |
| export function parseBattery(text, n, nOpts = 5) { | |
| const out = Array(n).fill(null) | |
| const seen = new Set() | |
| let dup = 0 | |
| for (const line of String(text || '').split('\n')) { | |
| const m = line.match(/^\W*(?:item\s*)?(\d+)\s*[:.)=\-]+\s*(?:option\s*)?([1-9])\b/i) | |
| if (!m) continue | |
| const pos = Number(m[1]), val = Number(m[2]) | |
| if (pos < 1 || pos > n || val > nOpts) continue | |
| if (seen.has(pos)) { dup++; continue } | |
| seen.add(pos) | |
| out[pos - 1] = val - 1 | |
| } | |
| return { values: out, parsed: seen.size, duplicates: dup } | |
| } | |
| // ---------- seeded shuffle (mulberry32) ---------- | |
| export function rng(seed) { | |
| let a = seed >>> 0 | |
| return () => { | |
| a = (a + 0x6d2b79f5) >>> 0 | |
| let t = a | |
| t = Math.imul(t ^ (t >>> 15), t | 1) | |
| t ^= t + Math.imul(t ^ (t >>> 7), t | 61) | |
| return ((t ^ (t >>> 14)) >>> 0) / 4294967296 | |
| } | |
| } | |
| export function shuffled(n, seed) { | |
| const r = rng(seed) | |
| const a = Array.from({ length: n }, (_, i) => i) | |
| for (let i = n - 1; i > 0; i--) { const j = Math.floor(r() * (i + 1)); [a[i], a[j]] = [a[j], a[i]] } | |
| return a | |
| } | |