86 lines
3.0 KiB
PHP
86 lines
3.0 KiB
PHP
<?php
|
|
// WEVIA DeepSeek Web Bridge
|
|
// Uses chat.deepseek.com internal API with extracted auth token
|
|
// Token must be set manually: POST /api/wevia-deepseek-proxy.php?action=set_token&token=xxx
|
|
|
|
$action = $_GET['action'] ?? $_POST['action'] ?? '';
|
|
$token_file = "/etc/weval/deepseek-web-token.txt";
|
|
|
|
// SET TOKEN (one-time setup by Yacine)
|
|
if ($action === 'set_token') {
|
|
$token = $_POST['token'] ?? $_GET['token'] ?? '';
|
|
if (strlen($token) > 20) {
|
|
file_put_contents($token_file, $token);
|
|
echo json_encode(["status" => "ok", "message" => "DeepSeek Web token saved", "len" => strlen($token)]);
|
|
} else {
|
|
echo json_encode(["error" => "Token too short"]);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
// CHECK TOKEN
|
|
if ($action === 'check_token') {
|
|
$exists = file_exists($token_file);
|
|
$len = $exists ? strlen(trim(file_get_contents($token_file))) : 0;
|
|
echo json_encode(["has_token" => $exists, "token_len" => $len]);
|
|
exit;
|
|
}
|
|
|
|
// CHAT via DeepSeek Web API
|
|
function deepseek_web_chat($message, $history = [], $model = "deepseek_chat") {
|
|
global $token_file;
|
|
if (!file_exists($token_file)) return null;
|
|
$token = trim(file_get_contents($token_file));
|
|
if (!$token) return null;
|
|
|
|
$messages = [];
|
|
foreach ($history as $h) {
|
|
$messages[] = ["role" => $h["role"], "content" => $h["content"]];
|
|
}
|
|
$messages[] = ["role" => "user", "content" => $message];
|
|
|
|
$data = json_encode([
|
|
"message" => $message,
|
|
"model_preference" => null,
|
|
"model_class" => $model, // deepseek_chat or deepseek_code
|
|
"temperature" => 0,
|
|
"top_p" => 0.7,
|
|
"max_tokens" => 8192,
|
|
"search_enabled" => false,
|
|
"thinking_enabled" => ($model === "deepseek_reasoner"),
|
|
], JSON_UNESCAPED_UNICODE);
|
|
|
|
// Call DeepSeek Web internal API
|
|
$ch = curl_init("https://chat.deepseek.com/api/v0/chat/completions");
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_POST => true,
|
|
CURLOPT_POSTFIELDS => $data,
|
|
CURLOPT_HTTPHEADER => [
|
|
"Content-Type: application/json",
|
|
"Authorization: Bearer $token",
|
|
"Accept: application/json",
|
|
"Origin: https://chat.deepseek.com",
|
|
"Referer: https://chat.deepseek.com/",
|
|
],
|
|
CURLOPT_TIMEOUT => 60,
|
|
CURLOPT_SSL_VERIFYPEER => false,
|
|
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
|
|
]);
|
|
$r = curl_exec($ch);
|
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
if ($code === 200) {
|
|
$d = @json_decode($r, true);
|
|
$content = $d["choices"][0]["message"]["content"] ?? null;
|
|
if ($content) {
|
|
return ["content" => $content, "provider" => "DeepSeek Web (FREE unlimited)", "model" => $model, "tokens" => ($d["usage"]["total_tokens"] ?? 0)];
|
|
}
|
|
} elseif ($code === 401) {
|
|
// Token expired
|
|
return ["error" => "Token expired. Re-extract from chat.deepseek.com DevTools."];
|
|
}
|
|
|
|
return null;
|
|
}
|