Files
html/api/opus-arch-prompt-evolution.php
opus 6931d33a7d
Some checks failed
WEVAL NonReg / nonreg (push) Has been cancelled
V39 architect final: 7 remaining R&D caps wired (4/9/10/11/13/15/16) + doctrines 94-100
2026-04-17 19:26:56 +02:00

53 lines
2.4 KiB
PHP

<?php
// opus-arch-prompt-evolution.php - Cap 4 (Doctrine 94)
header('Content-Type: application/json');
$action = $_GET['action'] ?? 'list';
$dir = '/opt/weval-ops/prompts';
@mkdir($dir, 0755, true);
$db = $dir . '/versions.json';
$versions = file_exists($db) ? json_decode(file_get_contents($db), true) : [];
if ($action === 'add') {
$intent = $_POST['intent'] ?? '';
$prompt = $_POST['prompt'] ?? '';
$v = count($versions[$intent] ?? []) + 1;
$versions[$intent][] = ['v'=>$v, 'prompt'=>$prompt, 'ts'=>date('c'), 'score'=>null, 'tests'=>0];
file_put_contents($db, json_encode($versions, JSON_PRETTY_PRINT));
echo json_encode(['ok'=>true, 'intent'=>$intent, 'version'=>$v]); exit;
}
if ($action === 'score') {
$intent = $_POST['intent'] ?? '';
$v = (int)$_POST['version'];
$score = (float)$_POST['score'];
foreach ($versions[$intent] ?? [] as &$pv) {
if ($pv['v'] == $v) {
$tests = $pv['tests'] ?? 0;
$pv['score'] = ((($pv['score'] ?? 0) * $tests) + $score) / ($tests + 1);
$pv['tests'] = $tests + 1;
}
}
file_put_contents($db, json_encode($versions, JSON_PRETTY_PRINT));
echo json_encode(['ok'=>true]); exit;
}
if ($action === 'best') {
$intent = $_GET['intent'] ?? '';
$list = $versions[$intent] ?? [];
usort($list, fn($a,$b) => ($b['score']??0) <=> ($a['score']??0));
echo json_encode(['ok'=>true, 'best'=>$list[0] ?? null]); exit;
}
if ($action === 'evolve') {
// Mutation: create new version from best with small tweak
$intent = $_POST['intent'] ?? '';
$list = $versions[$intent] ?? [];
if (!$list) { echo json_encode(['ok'=>false, 'error'=>'no parent']); exit; }
usort($list, fn($a,$b) => ($b['score']??0) <=> ($a['score']??0));
$parent = $list[0];
$mutations = ['soyez concis', 'répondez étape par étape', 'vérifiez vos hypothèses', 'donnez 3 options', 'évitez le jargon'];
$new_p = $parent['prompt'] . "\n\nAdjust: " . $mutations[array_rand($mutations)];
$v = count($versions[$intent]) + 1;
$versions[$intent][] = ['v'=>$v, 'prompt'=>$new_p, 'ts'=>date('c'), 'score'=>null, 'tests'=>0, 'parent'=>$parent['v']];
file_put_contents($db, json_encode($versions, JSON_PRETTY_PRINT));
echo json_encode(['ok'=>true, 'new_version'=>$v, 'parent'=>$parent['v']]); exit;
}
echo json_encode(['ok'=>true, 'versions'=>$versions, 'actions'=>['add','score','best','evolve']]);