68 lines
2.7 KiB
PHP
68 lines
2.7 KiB
PHP
<?php
|
|
// WEVIA Sovereign Gateway v2 — Routes through WEVIA Brain
|
|
header('Content-Type: application/json');
|
|
header('Access-Control-Allow-Origin: *');
|
|
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
|
|
header('Access-Control-Allow-Headers: Content-Type, Authorization, x-api-key, anthropic-version');
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(204); exit; }
|
|
|
|
$input = json_decode(file_get_contents('php://input'), true) ?: [];
|
|
$messages = $input['messages'] ?? [];
|
|
$max_tokens = $input['max_tokens'] ?? 2000;
|
|
|
|
// Extract last user message
|
|
$msg = '';
|
|
foreach (array_reverse($messages) as $m) {
|
|
if (($m['role'] ?? '') === 'user') { $msg = $m['content'] ?? ''; break; }
|
|
}
|
|
if (!$msg && $messages) $msg = end($messages)['content'] ?? 'hello';
|
|
|
|
// Add system context
|
|
$system = '';
|
|
foreach ($messages as $m) {
|
|
if (($m['role'] ?? '') === 'system') { $system = $m['content'] ?? ''; break; }
|
|
}
|
|
if ($system) $msg = "[System: " . substr($system, 0, 500) . "] " . $msg;
|
|
|
|
// Call WEVIA Brain (sovereign: Groq/Cerebras/Mistral)
|
|
$ch = curl_init('http://127.0.0.1/wevia-ia/weval-chatbot-api.php');
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_POST => true,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TIMEOUT => 60,
|
|
CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'Host: weval-consulting.com'],
|
|
CURLOPT_POSTFIELDS => json_encode(['message' => $msg, 'mode' => 'fast']),
|
|
]);
|
|
$resp = curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
$data = json_decode($resp, true);
|
|
$text = $data['response'] ?? 'Sovereign AI processing...';
|
|
$provider = $data['provider'] ?? 'WEVIA Sovereign';
|
|
|
|
// Detect format: Anthropic or OpenAI
|
|
$uri = $_SERVER['REQUEST_URI'] ?? '';
|
|
$is_anthropic = strpos($uri, '/v1/messages') !== false ||
|
|
!empty($_SERVER['HTTP_ANTHROPIC_VERSION']) ||
|
|
isset($input['anthropic_version']);
|
|
|
|
if ($is_anthropic) {
|
|
echo json_encode([
|
|
'id' => 'msg_wevia_' . substr(md5(uniqid()), 0, 12),
|
|
'type' => 'message',
|
|
'role' => 'assistant',
|
|
'content' => [['type' => 'text', 'text' => $text]],
|
|
'model' => 'wevia-sovereign-' . preg_replace('/[^a-zA-Z0-9]/', '', $provider),
|
|
'stop_reason' => 'end_turn',
|
|
'usage' => ['input_tokens' => 100, 'output_tokens' => intval(strlen($text) / 4)],
|
|
]);
|
|
} else {
|
|
echo json_encode([
|
|
'id' => 'chatcmpl-wevia-' . substr(md5(uniqid()), 0, 8),
|
|
'object' => 'chat.completion',
|
|
'model' => 'wevia-sovereign-' . preg_replace('/[^a-zA-Z0-9]/', '', $provider),
|
|
'choices' => [['index' => 0, 'message' => ['role' => 'assistant', 'content' => $text], 'finish_reason' => 'stop']],
|
|
'usage' => ['prompt_tokens' => 100, 'completion_tokens' => intval(strlen($text) / 4)],
|
|
]);
|
|
}
|