52 lines
1.7 KiB
PHP
52 lines
1.7 KiB
PHP
<?php
|
|
header("Content-Type: application/json; charset=utf-8");
|
|
$in = json_decode(file_get_contents("php://input"), true) ?: $_POST ?: $_GET;
|
|
$q = trim($in["query"] ?? $in["q"] ?? "");
|
|
if (!$q) { echo json_encode(["error"=>"query required"]); exit; }
|
|
|
|
$t0 = microtime(true);
|
|
$secrets = @file_get_contents("/etc/weval/secrets.env");
|
|
preg_match("/^OPENROUTER_KEY=(\S+)/m", $secrets ?? "", $m);
|
|
$or_key = $m[1] ?? "";
|
|
|
|
$ch = curl_init("https://openrouter.ai/api/v1/chat/completions");
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_POST => true,
|
|
CURLOPT_TIMEOUT => 30,
|
|
CURLOPT_HTTPHEADER => [
|
|
"Content-Type: application/json",
|
|
"Authorization: Bearer $or_key",
|
|
"HTTP-Referer: https://weval-consulting.com",
|
|
"X-Title: WEVIA",
|
|
],
|
|
CURLOPT_POSTFIELDS => json_encode([
|
|
"model" => "perplexity/sonar",
|
|
"messages" => [
|
|
["role"=>"system","content"=>"Tu es un moteur de recherche. Réponds factuellement en français, cite les sources URLs inline entre crochets."],
|
|
["role"=>"user","content"=>$q],
|
|
],
|
|
"max_tokens" => 700,
|
|
"temperature" => 0.2,
|
|
]),
|
|
]);
|
|
$raw = curl_exec($ch);
|
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
$d = json_decode($raw, true);
|
|
$answer = $d["choices"][0]["message"]["content"] ?? "";
|
|
|
|
preg_match_all('/https?:\/\/[^\s\)\]\"]+/', $answer, $urls_m);
|
|
$urls = array_slice(array_unique($urls_m[0] ?? []), 0, 5);
|
|
|
|
echo json_encode([
|
|
"query" => $q,
|
|
"answer" => trim($answer),
|
|
"sources" => $urls,
|
|
"http" => $code,
|
|
"error" => $d["error"]["message"] ?? null,
|
|
"elapsed_ms" => round((microtime(true)-$t0)*1000),
|
|
"provider" => "WEVIA Search",
|
|
]);
|