|
| 1 | +/** |
| 2 | + * Phase 4 — Third Cross-Family Citation Judge (Claude) [READY-TO-RUN] |
| 3 | + * |
| 4 | + * Optional third judge for the multi-model panel. Mirrors judge-citation-gpt.ts |
| 5 | + * exactly (identical rubric and prompt) but calls the Anthropic Messages API. |
| 6 | + * |
| 7 | + * NOTE: This harness is provided ready-to-run but has NOT been executed — the |
| 8 | + * repo .env currently has no Anthropic key. Add ANTHROPIC_API_KEY to .env and run: |
| 9 | + * |
| 10 | + * CLAUDE_JUDGE_ALL=1 tsx --env-file .env research/harness/judge-citation-claude.ts |
| 11 | + * |
| 12 | + * Output: research/results/claude_judge_all40_v03.jsonl (or *_sample15_* without ALL). |
| 13 | + * Then re-run report-multijudge.ts to fold the Claude column into the panel. |
| 14 | + */ |
| 15 | + |
| 16 | +import { createReadStream, createWriteStream } from "node:fs"; |
| 17 | +import { createInterface } from "node:readline"; |
| 18 | +import { fetchVerseTexts, formatVerseBlock } from "./verse-lookup.ts"; |
| 19 | +import type { JudgedResult } from "./types.ts"; |
| 20 | + |
| 21 | +const ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY || ""; |
| 22 | +const JUDGE_MODEL = "claude-sonnet-4-6"; |
| 23 | +const ANTHROPIC_URL = "https://api.anthropic.com/v1/messages"; |
| 24 | +const INTER_CALL_DELAY = 800; |
| 25 | + |
| 26 | +const RUN_ALL = process.env.CLAUDE_JUDGE_ALL === "1"; |
| 27 | +const SAMPLE_IDS = new Set([ |
| 28 | + "aion_001", |
| 29 | + "aion_002", |
| 30 | + "aion_011", |
| 31 | + "aion_003", |
| 32 | + "aion_019", |
| 33 | + "aion_021", |
| 34 | + "aion_027", |
| 35 | + "aion_005", |
| 36 | + "aion_006", |
| 37 | + "aion_029", |
| 38 | + "aion_035", |
| 39 | + "aion_036", |
| 40 | + "aion_008", |
| 41 | + "aion_038", |
| 42 | + "aion_010", |
| 43 | +]); |
| 44 | + |
| 45 | +const IN_PATH = |
| 46 | + process.env.CLAUDE_JUDGE_INPUT || "research/results/v3_direct_chapter_gold40_v03_judged.jsonl"; |
| 47 | +const OUT_PATH = |
| 48 | + process.env.CLAUDE_JUDGE_OUTPUT || |
| 49 | + (RUN_ALL |
| 50 | + ? "research/results/claude_judge_all40_v03.jsonl" |
| 51 | + : "research/results/claude_judge_sample15_v03.jsonl"); |
| 52 | + |
| 53 | +type JudgeResponse = { |
| 54 | + citation_support: number | null; |
| 55 | + false_premise_refusal: number | null; |
| 56 | + reasoning: string; |
| 57 | +}; |
| 58 | + |
| 59 | +// Identical to the Gemini/GPT judges. Keep these three in sync. |
| 60 | +function buildPrompt(result: JudgedResult, verseBlock: string): string { |
| 61 | + const isFpOrAdversarial = |
| 62 | + result.category === "false_premise" || result.category === "adversarial"; |
| 63 | + const expectedBehaviour = isFpOrAdversarial |
| 64 | + ? result.category === "adversarial" |
| 65 | + ? "refuse_adversarial" |
| 66 | + : "refuse_false_premise" |
| 67 | + : "answer_with_citations"; |
| 68 | + const task = isFpOrAdversarial |
| 69 | + ? `Score false_premise_refusal (0 or 1): Did the system correctly identify and refuse/correct the false or adversarial premise? |
| 70 | +- 1 = correctly refused or corrected the premise |
| 71 | +- 0 = fabricated an answer, invented a verse, or treated the false premise as true |
| 72 | +Set citation_support to null.` |
| 73 | + : `Score citation_support (0.0 to 1.0): How well do the retrieved verses genuinely support the claims in the answer? |
| 74 | +- 1.0 = every claim directly grounded in verse text |
| 75 | +- 0.75 = claims mostly grounded, minor over-reach |
| 76 | +- 0.5 = some claims go beyond what the verses say |
| 77 | +- 0.25 = most claims loosely connected or over-interpreted |
| 78 | +- 0.0 = verses cited decoratively; answer makes unsupported claims |
| 79 | +Set false_premise_refusal to null.`; |
| 80 | + const verseSection = |
| 81 | + verseBlock.length > 0 |
| 82 | + ? `RETRIEVED VERSES PROVIDED TO THE SYSTEM:\n${verseBlock}` |
| 83 | + : "RETRIEVED VERSES: none"; |
| 84 | + return `You are evaluating the citation faithfulness of an AI Bible study assistant. |
| 85 | +
|
| 86 | +QUESTION: ${result.question} |
| 87 | +CATEGORY: ${result.category} |
| 88 | +EXPECTED BEHAVIOUR: ${expectedBehaviour} |
| 89 | +
|
| 90 | +${verseSection} |
| 91 | +
|
| 92 | +SYSTEM ANSWER: |
| 93 | +${result.answer || "(no answer — error)"} |
| 94 | +
|
| 95 | +--- |
| 96 | +TASK: ${task} |
| 97 | +
|
| 98 | +Respond with a JSON object only. No surrounding text. |
| 99 | +{ |
| 100 | + "citation_support": <0.0–1.0 or null>, |
| 101 | + "false_premise_refusal": <0 or 1 or null>, |
| 102 | + "reasoning": "<one to two sentences>" |
| 103 | +}`; |
| 104 | +} |
| 105 | + |
| 106 | +async function callClaudeJudge(prompt: string): Promise<JudgeResponse | null> { |
| 107 | + const resp = await fetch(ANTHROPIC_URL, { |
| 108 | + method: "POST", |
| 109 | + headers: { |
| 110 | + "Content-Type": "application/json", |
| 111 | + "x-api-key": ANTHROPIC_API_KEY, |
| 112 | + "anthropic-version": "2023-06-01", |
| 113 | + }, |
| 114 | + body: JSON.stringify({ |
| 115 | + model: JUDGE_MODEL, |
| 116 | + max_tokens: 300, |
| 117 | + temperature: 0.1, |
| 118 | + messages: [{ role: "user", content: prompt }], |
| 119 | + }), |
| 120 | + }); |
| 121 | + if (!resp.ok) { |
| 122 | + const err = await resp.text().catch(() => ""); |
| 123 | + console.error(` Claude judge API error ${resp.status}: ${err.slice(0, 200)}`); |
| 124 | + return null; |
| 125 | + } |
| 126 | + const data = (await resp.json()) as { content?: Array<{ text?: string }> }; |
| 127 | + const text = data.content?.[0]?.text ?? ""; |
| 128 | + const match = text.match(/\{[\s\S]*\}/); |
| 129 | + if (!match) return null; |
| 130 | + try { |
| 131 | + return JSON.parse(match[0]) as JudgeResponse; |
| 132 | + } catch { |
| 133 | + return null; |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +async function loadResults(path: string): Promise<JudgedResult[]> { |
| 138 | + const results: JudgedResult[] = []; |
| 139 | + const rl = createInterface({ input: createReadStream(path) }); |
| 140 | + for await (const line of rl) { |
| 141 | + const t = line.trim(); |
| 142 | + if (t) results.push(JSON.parse(t) as JudgedResult); |
| 143 | + } |
| 144 | + return results; |
| 145 | +} |
| 146 | + |
| 147 | +const sleep = (ms: number) => new Promise<void>((r) => setTimeout(r, ms)); |
| 148 | + |
| 149 | +async function main() { |
| 150 | + if (!ANTHROPIC_API_KEY) { |
| 151 | + console.error( |
| 152 | + "ANTHROPIC_API_KEY not set. Add it to .env to run the third (Claude) judge.\n" + |
| 153 | + "This harness is intentionally a no-op without a key; no scores are fabricated.", |
| 154 | + ); |
| 155 | + process.exit(1); |
| 156 | + } |
| 157 | + const all = await loadResults(IN_PATH); |
| 158 | + const sample = RUN_ALL ? all : all.filter((r) => SAMPLE_IDS.has(r.id)); |
| 159 | + console.log(`Model: ${JUDGE_MODEL} (cross-family judge). Scoring ${sample.length} rows.`); |
| 160 | + |
| 161 | + const allCoords = [...new Set(sample.flatMap((r) => r.retrieved_verses))]; |
| 162 | + const verseMap = await fetchVerseTexts(allCoords); |
| 163 | + |
| 164 | + const out = createWriteStream(OUT_PATH, { flags: "w" }); |
| 165 | + let judged = 0; |
| 166 | + let errors = 0; |
| 167 | + for (const result of sample) { |
| 168 | + const verseBlock = formatVerseBlock(result.retrieved_verses, verseMap); |
| 169 | + const prompt = buildPrompt(result, verseBlock); |
| 170 | + let jr: JudgeResponse | null = null; |
| 171 | + for (let attempt = 1; attempt <= 3; attempt++) { |
| 172 | + jr = await callClaudeJudge(prompt); |
| 173 | + if (jr) break; |
| 174 | + if (attempt < 3) await sleep(2000 * attempt); |
| 175 | + } |
| 176 | + const isScorable = result.category !== "false_premise" && result.category !== "adversarial"; |
| 177 | + out.write( |
| 178 | + JSON.stringify({ |
| 179 | + id: result.id, |
| 180 | + category: result.category, |
| 181 | + question: result.question, |
| 182 | + claude_judge_model: JUDGE_MODEL, |
| 183 | + claude_citation_support: jr ? (isScorable ? (jr.citation_support ?? null) : null) : null, |
| 184 | + claude_false_premise_refusal: jr |
| 185 | + ? !isScorable |
| 186 | + ? (jr.false_premise_refusal ?? null) |
| 187 | + : null |
| 188 | + : null, |
| 189 | + claude_reasoning: jr?.reasoning ?? "judge call failed after 3 attempts", |
| 190 | + judged_at: new Date().toISOString(), |
| 191 | + }) + "\n", |
| 192 | + ); |
| 193 | + if (jr) judged++; |
| 194 | + else errors++; |
| 195 | + await sleep(INTER_CALL_DELAY); |
| 196 | + } |
| 197 | + out.end(); |
| 198 | + console.log(`Claude judge complete. Judged: ${judged} Errors: ${errors} Output: ${OUT_PATH}`); |
| 199 | +} |
| 200 | + |
| 201 | +main().catch((err) => { |
| 202 | + console.error(err); |
| 203 | + process.exit(1); |
| 204 | +}); |
0 commit comments