File size: 15,407 Bytes
375741b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const manifestPath = path.join(__dirname, "benchlocal.pack.json");
const scenariosPath = path.join(__dirname, "scenarios.json");
export const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
function loadScenarios() {
return JSON.parse(fs.readFileSync(scenariosPath, "utf8"));
}
export function listScenarios() {
return loadScenarios().map((s) => ({
id: s.id,
name: s.name,
category: s.category || "Общие",
description: `[${(s.language || "ru").toUpperCase()}] ${s.name}`
}));
}
export function getScenarios() {
return listScenarios();
}
function normalizeStr(s) {
return String(s || "")
.trim()
.toLowerCase()
.replace(/[«»"'`]/g, "")
.replace(/\s+/g, " ");
}
/**
* Формирует человекочитаемое представление эталонного ответа для логов
*/
function formatExpectedAnswer(scenario) {
if (scenario.category === "murder_mysteries") {
return [
`Подозреваемый: (${scenario.expectedSuspect})`,
`Орудие преступления: (${scenario.expectedWeapon})`,
`Фактическое место: (${scenario.expectedLocation})`,
`Финальная строка: Мой ответ: (${scenario.expectedSuspect}), (${scenario.expectedWeapon}), (${scenario.expectedLocation})`
].join("\n");
}
if (scenario.category === "object_placements") {
const lines = ["Ожидания персонажей (Theory of Mind):"];
const sortedBeliefs = Object.entries(scenario.expectedBeliefs || {}).sort(([a], [b]) =>
a.localeCompare(b)
);
for (const [charLetter, locNum] of sortedBeliefs) {
lines.push(` (${charLetter}) ожидает найти в: ${locNum}`);
}
lines.push(`Фактическое физическое местоположение: (${scenario.expectedFinalLocation})`);
lines.push(`Эталонный блок ответа:`);
lines.push(`Мой ответ:`);
for (const [charLetter, locNum] of sortedBeliefs) {
lines.push(`(${charLetter}) - ${locNum}`);
}
lines.push(`(${scenario.expectedFinalLocation})`);
return lines.join("\n");
}
if (scenario.category === "team_allocation") {
const sorted = [...(scenario.expectedTeam || [])].map((x) => x.toUpperCase()).sort();
return [
`Отобранная тройка специалистов: (${sorted.join("), (")})`,
`Финальная строка: Мой ответ: (${sorted.join("), (")})`
].join("\n");
}
return "Не определено";
}
/**
* Валидатор строгих форматов ответов MuSR с детальным диффом ошибок
*/
function validateResponse(scenario, rawText) {
if (!rawText || !rawText.trim()) {
return {
pass: false,
score: 0,
reason: "Модель вернула пустой ответ.",
expected: formatExpectedAnswer(scenario)
};
}
const text = rawText.trim();
const category = scenario.category;
// 1. ДЕТЕКТИВЫ: Тройка (Кто, Чем, Где)
if (category === "murder_mysteries") {
const match = text.match(
/(?:мой ответ|my answer)\s*:\s*\(?([А-ЯA-Z])\)?\s*,\s*\(?(\d+)\)?\s*,\s*\(?([^)\n\r]+)\)?/i
);
const expSuspect = scenario.expectedSuspect.toUpperCase();
const expWeapon = String(scenario.expectedWeapon).trim();
const expLocation = normalizeStr(scenario.expectedLocation);
const expectedStr = `Мой ответ: (${expSuspect}), (${expWeapon}), (${expLocation})`;
if (!match) {
return {
pass: false,
score: 0,
reason: `Не найден маркерный формат строки: 'Мой ответ: (Буква), (Цифра), (место)'. Ожидалось: "${expectedStr}".`,
expected: formatExpectedAnswer(scenario)
};
}
const suspect = match[1].toUpperCase();
const weapon = String(match[2]).trim();
const location = normalizeStr(match[3]);
const okS = suspect === expSuspect;
const okW = weapon === expWeapon;
const okL = location === expLocation;
if (okS && okW && okL) {
return {
pass: true,
score: 100,
reason: `Дело раскрыто безошибочно: виновен (${suspect}), орудие (${weapon}), место (${location}).`,
expected: formatExpectedAnswer(scenario)
};
}
const diffs = [];
if (!okS) diffs.push(`Убийца: указан (${suspect}), верный (${expSuspect})`);
if (!okW) diffs.push(`Орудие: указано (${weapon}), верное (${expWeapon})`);
if (!okL) diffs.push(`Место: указано (${location}), верное (${expLocation})`);
return {
pass: false,
score: 0,
reason: `Ошибки в дедукции: ${diffs.join("; ")}. Правильный ответ: "${expectedStr}".`,
expected: formatExpectedAnswer(scenario)
};
}
// 2. ОБЪЕКТЫ: Theory of Mind (ToM) + Физическая реальность
if (category === "object_placements") {
const markerIdx = text.search(/(?:мой ответ|my answer)\s*:/i);
if (markerIdx === -1) {
return {
pass: false,
score: 0,
reason: "Отсутствует обязательный заголовок блока 'Мой ответ:' или 'My answer:'.",
expected: formatExpectedAnswer(scenario)
};
}
const block = text.slice(markerIdx);
const beliefsMatches = [...block.matchAll(/\(([А-ЯA-Z])\)\s*[-–—]\s*(\d+)/gi)];
const finalMatches = [...block.matchAll(/^\s*\((\d+)\)\s*$/gm)];
if (beliefsMatches.length === 0 || finalMatches.length === 0) {
return {
pass: false,
score: 0,
reason:
"Нарушен построчный синтаксис персонажей '(Буква) - Номер' либо отсутствует финальная строка '([номер])'.",
expected: formatExpectedAnswer(scenario)
};
}
const extractedBeliefs = {};
for (const m of beliefsMatches) {
extractedBeliefs[m[1].toUpperCase()] = Number(m[2]);
}
const extractedFinal = Number(finalMatches[finalMatches.length - 1][1]);
const expectedBeliefs = scenario.expectedBeliefs;
const expectedFinal = Number(scenario.expectedFinalLocation);
const diffs = [];
for (const [charLetter, expLoc] of Object.entries(expectedBeliefs)) {
const actLoc = extractedBeliefs[charLetter.toUpperCase()];
if (actLoc !== expLoc) {
diffs.push(`(${charLetter}): получено ${actLoc ?? "не указано"}, должно быть ${expLoc}`);
}
}
if (extractedFinal !== expectedFinal) {
diffs.push(`Реальное положение: получено (${extractedFinal}), должно быть (${expectedFinal})`);
}
if (diffs.length === 0) {
return {
pass: true,
score: 100,
reason: "Все когнитивные ожидания персонажей (ToM) и физическая реальность вычислены верно.",
expected: formatExpectedAnswer(scenario)
};
}
return {
pass: false,
score: 0,
reason: `Ошибки в Theory of Mind: ${diffs.join("; ")}.`,
expected: formatExpectedAnswer(scenario)
};
}
// 3. КОМАНДЫ: Алфавитный список отобранных кандидатов
if (category === "team_allocation") {
const match = text.match(/(?:мой ответ|my answer)\s*:\s*([^\n\r]+)/i);
const expectedTeam = [...scenario.expectedTeam].map((x) => x.toUpperCase()).sort();
const expectedStr = `Мой ответ: (${expectedTeam.join("), (")})`;
if (!match) {
return {
pass: false,
score: 0,
reason: `Не найдена завершающая строка 'Мой ответ: (Буква 1), (Буква 2), (Буква 3)'. Ожидалось: "${expectedStr}".`,
expected: formatExpectedAnswer(scenario)
};
}
const rawMembers = [...match[1].matchAll(/\(([А-ЯA-Z])\)/gi)].map((m) => m[1].toUpperCase());
const isAlphabetical = rawMembers
.slice()
.every((val, i, arr) => !i || arr[i - 1].localeCompare(val) <= 0);
if (!isAlphabetical) {
return {
pass: false,
score: 0,
reason: `Кандидаты не упорядочены по алфавиту: [${rawMembers.join(", ")}]. Ожидалось: "${expectedStr}".`,
expected: formatExpectedAnswer(scenario)
};
}
const sortedRaw = rawMembers.slice().sort();
const isMatch =
sortedRaw.length === expectedTeam.length &&
sortedRaw.every((val, idx) => val === expectedTeam[idx]);
if (isMatch) {
return {
pass: true,
score: 100,
reason: `Команда сформирована верно: (${expectedTeam.join("), (")}).`,
expected: formatExpectedAnswer(scenario)
};
}
return {
pass: false,
score: 0,
reason: `Неверный состав: получено [${rawMembers.join(", ")}], ожидалось: [${expectedTeam.join(", ")}].`,
expected: formatExpectedAnswer(scenario)
};
}
return {
pass: false,
score: 0,
reason: "Неизвестная категория сценария.",
expected: "Неизвестно"
};
}
function resolveHelpers(context) {
if (context && typeof context.getRequiredProvider === "function") return context;
if (context?.helpers && typeof context.helpers.getRequiredProvider === "function")
return context.helpers;
return {
getRequiredProvider(providerId) {
const providers = context?.providers || [];
const found = Array.isArray(providers)
? providers.find((p) => p.id === providerId || p.provider === providerId)
: providers[providerId];
return found || { baseUrl: "http://127.0.0.1:11434/v1", apiKey: "none" };
},
getInferenceEndpoint(modelId) {
return context?.endpoints?.[modelId] || null;
}
};
}
export async function prepare(context) {
const helpers = resolveHelpers(context);
return {
async runScenario(input) {
const allScenarios = loadScenarios();
const scenario = allScenarios.find((s) => s.id === input.scenario.id);
if (!scenario) {
return {
scenarioId: input.scenario.id,
status: "fail",
score: 0,
summary: "Сценарий не найден",
rawLog: "error=Scenario not found"
};
}
const provider = helpers.getRequiredProvider(input.model.provider, { enabledOnly: true });
const inference = helpers.getInferenceEndpoint
? helpers.getInferenceEndpoint(input.model.id)
: null;
const rawBaseUrl =
(inference && inference.status === "running" ? inference.baseUrl : provider?.baseUrl) ||
"http://127.0.0.1:11434/v1";
const baseUrl = rawBaseUrl.replace(/\/+$/, "");
const apiKey = provider?.apiKey || "none";
const requestBody = {
model: input.model.model,
messages: [{ role: "user", content: scenario.prompt }],
temperature: 0
};
const response = await fetch(`${baseUrl}/chat/completions`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`
},
body: JSON.stringify(requestBody)
});
const data = await response.json();
const text = (data.choices?.[0]?.message?.content || "").trim();
const result = validateResponse(scenario, text);
// Информативный журнал с задачей, эталоном, выводом модели и диффом ошибок
const rawLog = [
`============================================================`,
`СЦЕНАРИЙ: ${scenario.id} — ${scenario.name}`,
`КАТЕГОРИЯ: ${scenario.category} | ЯЗЫК: ${scenario.language}`,
`МОДЕЛЬ: ${input.model.model}`,
`============================================================`,
``,
`=== ИСХОДНАЯ ЗАДАЧА (PROMPT) ===`,
scenario.prompt,
``,
`=== ЭТАЛОННЫЙ ПРАВИЛЬНЫЙ ОТВЕТ ===`,
result.expected,
``,
`=== ОТВЕТ АССИСТЕНТА ===`,
text || "<МОДЕЛЬ ВЕРНУЛА ПУСТОЙ ОТВЕТ>",
``,
`=== ВЕРДИКТ И ПРОВЕРКА ===`,
`Статус: ${result.pass ? "ВЕРНО (PASS)" : "НЕВЕРНО (FAIL)"}`,
`Балл: ${result.score}/100`,
`Подробности проверки: ${result.reason}`,
``,
`--- СТАНДАРТНЫЕ МЕТКИ ДЛЯ BENCHLOCAL ---`,
`model=${input.model.model}`,
`scenario=${scenario.id} ${scenario.name}`,
`category=${scenario.category}`,
`language=${scenario.language}`,
`verdict=${result.pass ? "pass" : "fail"}`,
`score=${result.score}`,
`summary=${result.reason}`,
`\n--- RAW API RESPONSE (JSON) ---`,
JSON.stringify(data, null, 2)
].join("\n");
return {
scenarioId: scenario.id,
status: result.pass ? "pass" : "fail",
score: result.score,
summary: result.reason,
rawLog
};
},
async dispose() {}
};
}
export function scoreModelResults(results) {
const scored = results || [];
const total = scored.reduce((sum, r) => sum + (r.score || 0), 0);
const max = scored.length * 100;
const allScenarios = loadScenarios();
const categoryNames = {
murder_mysteries: "Детективы (Murder Mysteries - Triplet)",
object_placements: "Перемещение объектов (Object Placements - ToM)",
team_allocation: "Формирование команд (Team Allocation - Psych)"
};
const categories = Object.keys(categoryNames).map((catKey) => {
const subsetIds = new Set(allScenarios.filter((s) => s.category === catKey).map((s) => s.id));
const catResults = scored.filter((r) => subsetIds.has(r.scenarioId));
const catScore = catResults.reduce((sum, r) => sum + (r.score || 0), 0);
const catMax = catResults.length * 100;
return {
id: catKey,
name: categoryNames[catKey],
score: catMax > 0 ? Math.round((catScore / catMax) * 100) : 0
};
});
return {
totalScore: max > 0 ? Math.round((total / max) * 100) : 0,
categories
};
}
export default {
manifest,
listScenarios,
getScenarios,
prepare,
createRuntime: prepare,
scoreModelResults
}; |