UNPKG

gepa-spo

Version:

Genetic-Pareto prompt optimizer to evolve system prompts from a few rollouts with modular support and intelligent crossover

28 lines (27 loc) 1.15 kB
import { clampNumber, safeParseWithSchema } from './json.js'; export function buildJudgePrompt(rubric) { return [ 'You are a strict evaluator. Return ONLY JSON: {"score":0.00,"feedback":"..."}', 'Score in [0,1] with two decimals. Keep feedback brief.', 'Rubric:', rubric ].join('\n'); } /** Call the judge LLM to evaluate system+user+assistant => {score, feedback} */ export async function judgeScore(chatLLM, rubric, system, user, assistant) { const sys = buildJudgePrompt(rubric); const content = await chatLLM.chat([ { role: 'system', content: sys }, { role: 'user', content: `System:\n${system}\n\nUser:\n${user}\n\nAssistant:\n${assistant}` } ]); // Structured parse with clamping and defaults const schema = { parse(input) { const o = (typeof input === 'object' && input) ? input : {}; const score = clampNumber(o.score, 0, 1, 0); const feedback = typeof o.feedback === 'string' ? o.feedback : ''; return { score, feedback }; } }; const parsed = safeParseWithSchema(content, schema); return parsed; }