Files
html/api/opus-arch-deepseek-r1.php
2026-04-17 19:14:29 +02:00

72 lines
2.7 KiB
PHP

<?php
// opus-arch-deepseek-r1.php — Cap 8 System 2 reasoning (Doctrine 86)
// Force DeepSeek-R1 / o1-like CoT via cascade providers
header('Content-Type: application/json');
$message = $_POST['message'] ?? $_GET['message'] ?? ($_POST['q'] ?? '');
if (!$message) {
echo json_encode(['ok'=>false,'error'=>'missing message']);
exit;
}
// Chain-of-thought system prompt
$cot_system = 'You are an expert reasoner. Before answering, think step by step in <thinking>...</thinking> tags. Consider multiple angles, verify assumptions, check logic. Then provide the final answer in <answer>...</answer> tags.';
$providers = [
['name'=>'sambanova-r1', 'url'=>'https://api.sambanova.ai/v1/chat/completions', 'model'=>'DeepSeek-R1', 'env'=>'SAMBANOVA_API_KEY'],
['name'=>'cerebras-llama-r1', 'url'=>'https://api.cerebras.ai/v1/chat/completions', 'model'=>'llama3.3-70b', 'env'=>'CEREBRAS_API_KEY'],
['name'=>'groq-deepseek', 'url'=>'https://api.groq.com/openai/v1/chat/completions', 'model'=>'deepseek-r1-distill-llama-70b', 'env'=>'GROQ_API_KEY'],
];
// Load secrets
$secrets_file = '/etc/weval/secrets.env';
$secrets = [];
if (file_exists($secrets_file)) {
foreach (file($secrets_file) as $line) {
if (preg_match('/^([A-Z_]+)=(.+)$/', trim($line), $m)) {
$secrets[$m[1]] = trim($m[2], '"\'');
}
}
}
foreach ($providers as $p) {
$key = $secrets[$p['env']] ?? '';
if (!$key) continue;
$payload = json_encode([
'model' => $p['model'],
'messages' => [
['role'=>'system', 'content'=>$cot_system],
['role'=>'user', 'content'=>$message]
],
'max_tokens' => 2000,
'temperature' => 0.3
]);
$ch = curl_init($p['url']);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'Authorization: Bearer ' . $key],
CURLOPT_TIMEOUT => 45,
]);
$out = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($code >= 200 && $code < 300) {
$data = json_decode($out, true);
$content = $data['choices'][0]['message']['content'] ?? '';
preg_match('/<thinking>(.*?)<\/thinking>/s', $content, $thinking);
preg_match('/<answer>(.*?)<\/answer>/s', $content, $answer);
echo json_encode([
'ok'=>true,
'provider'=>$p['name'],
'thinking'=>$thinking[1] ?? '',
'answer'=>$answer[1] ?? $content,
'full'=>$content,
]);
exit;
}
}
echo json_encode(['ok'=>false, 'error'=>'All R1 providers failed']);