|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { mkdirSync, readFileSync, writeFileSync } from 'node:fs' |
| 4 | +import { dirname, join } from 'node:path' |
| 5 | + |
| 6 | +const reportPath = |
| 7 | + process.argv[2] ?? 'evals/intent-discovery/runs/latest/vitest-results.json' |
| 8 | +const apiKey = process.env.OPENAI_API_KEY |
| 9 | +const model = process.env.INTENT_DISCOVERY_LLM_JUDGE_MODEL ?? 'gpt-4o-mini' |
| 10 | +const requestTimeoutMs = Number( |
| 11 | + process.env.INTENT_DISCOVERY_LLM_JUDGE_TIMEOUT_MS ?? '30000', |
| 12 | +) |
| 13 | + |
| 14 | +if (!apiKey) { |
| 15 | + console.log('Skipped LLM judge: OPENAI_API_KEY is not set.') |
| 16 | + process.exit(0) |
| 17 | +} |
| 18 | + |
| 19 | +const report = JSON.parse(readFileSync(reportPath, 'utf8')) |
| 20 | +const cases = reportCases(report) |
| 21 | +const judgments = [] |
| 22 | + |
| 23 | +for (const item of cases) { |
| 24 | + judgments.push(await judgeCase({ apiKey, item, model })) |
| 25 | +} |
| 26 | + |
| 27 | +const output = { |
| 28 | + generatedAt: new Date().toISOString(), |
| 29 | + judgments, |
| 30 | + model, |
| 31 | +} |
| 32 | +const outDir = dirname(reportPath) |
| 33 | +mkdirSync(outDir, { recursive: true }) |
| 34 | +writeFileSync( |
| 35 | + join(outDir, 'llm-judge.json'), |
| 36 | + `${JSON.stringify(output, null, 2)}\n`, |
| 37 | +) |
| 38 | +console.log(JSON.stringify(output, null, 2)) |
| 39 | + |
| 40 | +function reportCases(report) { |
| 41 | + return (report.testResults ?? []).flatMap((suite) => |
| 42 | + (suite.assertionResults ?? []) |
| 43 | + .filter((test) => test.meta?.eval) |
| 44 | + .map((test) => { |
| 45 | + const run = test.meta.harness?.run ?? {} |
| 46 | + const artifacts = run.artifacts ?? {} |
| 47 | + const scores = Object.fromEntries( |
| 48 | + (test.meta.eval.scores ?? []).map((score) => [ |
| 49 | + score.name, |
| 50 | + score.score ?? 0, |
| 51 | + ]), |
| 52 | + ) |
| 53 | + |
| 54 | + return { |
| 55 | + artifacts: pick(artifacts, [ |
| 56 | + 'condition', |
| 57 | + 'expectedSkillAreas', |
| 58 | + 'intentCommandsInvoked', |
| 59 | + 'loadedSkills', |
| 60 | + 'runnerStatus', |
| 61 | + 'taskId', |
| 62 | + ]), |
| 63 | + finalAnswer: test.meta.eval.output?.finalAnswer ?? '', |
| 64 | + scores, |
| 65 | + title: test.title, |
| 66 | + } |
| 67 | + }), |
| 68 | + ) |
| 69 | +} |
| 70 | + |
| 71 | +async function judgeCase({ apiKey, item, model }) { |
| 72 | + const controller = new AbortController() |
| 73 | + const timeout = setTimeout(() => controller.abort(), requestTimeoutMs) |
| 74 | + let response |
| 75 | + |
| 76 | + try { |
| 77 | + response = await fetch('https://api.openai.com/v1/chat/completions', { |
| 78 | + body: JSON.stringify({ |
| 79 | + messages: [ |
| 80 | + { |
| 81 | + role: 'system', |
| 82 | + content: |
| 83 | + 'You judge whether a coding agent output appears to apply loaded library skill guidance. You must not decide whether Intent was invoked; that is provided by deterministic scores. Return strict JSON only.', |
| 84 | + }, |
| 85 | + { |
| 86 | + role: 'user', |
| 87 | + content: JSON.stringify({ |
| 88 | + instruction: |
| 89 | + 'Assess final output quality only. Return {"appliedGuidance":"yes"|"no"|"unknown","rationale":"..."}. Use unknown when evidence is insufficient.', |
| 90 | + item, |
| 91 | + }), |
| 92 | + }, |
| 93 | + ], |
| 94 | + model, |
| 95 | + response_format: { type: 'json_object' }, |
| 96 | + temperature: 0, |
| 97 | + }), |
| 98 | + headers: { |
| 99 | + authorization: `Bearer ${apiKey}`, |
| 100 | + 'content-type': 'application/json', |
| 101 | + }, |
| 102 | + method: 'POST', |
| 103 | + signal: controller.signal, |
| 104 | + }) |
| 105 | + } catch (error) { |
| 106 | + return { |
| 107 | + deterministicScores: item.scores, |
| 108 | + error: `LLM judge request failed: ${String(error)}`, |
| 109 | + title: item.title, |
| 110 | + } |
| 111 | + } finally { |
| 112 | + clearTimeout(timeout) |
| 113 | + } |
| 114 | + |
| 115 | + if (!response.ok) { |
| 116 | + return { |
| 117 | + error: await response.text(), |
| 118 | + title: item.title, |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + const body = await response.json() |
| 123 | + const content = body.choices?.[0]?.message?.content ?? '{}' |
| 124 | + let judgment |
| 125 | + try { |
| 126 | + judgment = JSON.parse(content) |
| 127 | + } catch (error) { |
| 128 | + return { |
| 129 | + deterministicScores: item.scores, |
| 130 | + error: `Invalid JSON from model: ${String(error)}`, |
| 131 | + rawContent: content, |
| 132 | + title: item.title, |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return { |
| 137 | + deterministicScores: item.scores, |
| 138 | + judgment, |
| 139 | + title: item.title, |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +function pick(value, keys) { |
| 144 | + return Object.fromEntries( |
| 145 | + keys |
| 146 | + .filter((key) => Object.prototype.hasOwnProperty.call(value, key)) |
| 147 | + .map((key) => [key, value[key]]), |
| 148 | + ) |
| 149 | +} |
0 commit comments