DeepSeek API 连接测试

API密钥已保存
'; } if ($_POST['action'] == 'test') { $api_key = $_SESSION['api_key'] ?? ''; $message = trim($_POST['message']); if (empty($api_key)) { echo '
请先设置API密钥
'; } elseif (empty($message)) { echo '
请输入测试消息
'; } else { // 调用DeepSeek API $url = 'https://api.deepseek.com/v1/chat/completions'; $data = [ 'model' => 'deepseek-chat', 'messages' => [['role' => 'user', 'content' => $message]], 'stream' => false, 'temperature' => 0.7, 'max_tokens' => 500 ]; $ch = curl_init(); curl_setopt_array($ch, [ CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($data), CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', 'Authorization: Bearer ' . $api_key ], CURLOPT_TIMEOUT => 30 ]); $response = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($http_code == 200) { $result = json_decode($response, true); $ai_response = $result['choices'][0]['message']['content']; // 保存到会话记录 if (!isset($_SESSION['chat_history'])) { $_SESSION['chat_history'] = []; } $_SESSION['chat_history'][] = ['user' => $message, 'ai' => $ai_response]; echo '
API连接成功!
'; } else { echo '
API错误: HTTP ' . $http_code; if ($http_code == 401) echo ' - API密钥无效'; elseif ($http_code == 402) echo ' - 余额不足'; elseif ($http_code == 429) echo ' - 请求过于频繁'; else echo ' - ' . $response; echo '
'; } } } if ($_POST['action'] == 'clear') { unset($_SESSION['chat_history']); echo '
对话记录已清空
'; } ?>

请输入消息开始测试...

您:
AI: