40 lines
2.4 KiB
PHP
40 lines
2.4 KiB
PHP
<?php
|
|
header("Content-Type: application/json; charset=utf-8");
|
|
$input = json_decode(file_get_contents("php://input"), true) ?: [];
|
|
$task = $input["task"] ?? "";
|
|
if (!$task) { echo json_encode(["error"=>"task required"]); exit; }
|
|
$secrets=[];
|
|
foreach(file("/etc/weval/secrets.env",2|4) as $l){if(strpos($l,"=")!==false){list($k,$v)=explode("=",$l,2);$secrets[trim($k)]=trim($v," \t\"'");}}
|
|
$key = $secrets["CEREBRAS_API_KEY"] ?? "";
|
|
function llm($prompt, $key, $sys="") {
|
|
$msgs = [];
|
|
if ($sys) $msgs[] = ["role"=>"system","content"=>$sys];
|
|
$msgs[] = ["role"=>"user","content"=>$prompt];
|
|
$ch = curl_init("https://api.cerebras.ai/v1/chat/completions");
|
|
curl_setopt_array($ch, [CURLOPT_POST=>true,CURLOPT_POSTFIELDS=>json_encode(["model"=>"qwen-3-235b-a22b-instruct-2507","messages"=>$msgs,"max_tokens"=>600,"temperature"=>0.3]),CURLOPT_HTTPHEADER=>["Content-Type: application/json","Authorization: Bearer $key"],CURLOPT_RETURNTRANSFER=>true,CURLOPT_TIMEOUT=>12]);
|
|
$r = curl_exec($ch); curl_close($ch);
|
|
return json_decode($r, true)["choices"][0]["message"]["content"] ?? "";
|
|
}
|
|
$trace = [];
|
|
$plan = llm("Task: $task\nCreate max 4 numbered bash steps. Reply ONLY steps.", $key, "WEVIA planner. Concise steps.");
|
|
$trace[] = ["phase"=>"plan","output"=>$plan];
|
|
$results = [];
|
|
preg_match_all('/\d+\.\s*(.+)/', $plan, $steps);
|
|
foreach (array_slice($steps[1] ?? [], 0, 4) as $i => $step) {
|
|
$cmd = llm("Convert to ONE bash command (Ubuntu, nginx/docker/php/python3/git/psql). Step: $step\nReply ONLY the command.", $key);
|
|
$cmd = trim(preg_replace('/^```\w*|```$/m', '', $cmd));
|
|
$cmd = strtok($cmd, "\n");
|
|
if (strlen($cmd) > 3 && strlen($cmd) < 300) {
|
|
$out = shell_exec("timeout 8 $cmd 2>&1");
|
|
$results[] = ["step"=>$i+1,"desc"=>$step,"cmd"=>$cmd,"output"=>substr($out ?? "", 0, 300)];
|
|
}
|
|
$trace[] = ["phase"=>"act","step"=>$i+1];
|
|
}
|
|
$obs = "";
|
|
foreach ($results as $r) $obs .= "Step {$r['step']}: {$r['output']}\n";
|
|
$reflect = llm("Task: $task\nResults:\n$obs\nScore 1-10. If <7 say what failed.", $key, "Quality evaluator. Be precise.");
|
|
preg_match('/(\d+)/', $reflect, $sm);
|
|
$score = intval($sm[1] ?? 7);
|
|
$synthesis = llm("Task: $task\nResults:\n$obs\nReflection: $reflect\nSummarize in French, 3 lines max.", $key, "WEVIA Master AI. French.");
|
|
echo json_encode(["task"=>$task,"score"=>$score,"synthesis"=>$synthesis,"steps"=>count($results),"trace"=>$trace], JSON_UNESCAPED_UNICODE);
|