Initial commit: HR Position System
- Database schema with MySQL support - LLM API integration (Gemini 2.5 Flash, DeepSeek, OpenAI) - Error handling with copyable error messages - CORS fix for API calls - Complete setup documentation 🤖 Generated with Claude Code https://claude.com/claude-code Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
520
api_test.html
Normal file
520
api_test.html
Normal file
@@ -0,0 +1,520 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-TW">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>LLM API 連線測試 - HR 系統</title>
|
||||
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+TC:wght@300;400;500;700&display=swap" rel="stylesheet">
|
||||
<style>
|
||||
:root {
|
||||
--primary: #1a5276;
|
||||
--primary-light: #2980b9;
|
||||
--success: #27ae60;
|
||||
--danger: #e74c3c;
|
||||
--warning: #f39c12;
|
||||
--border: #ddd;
|
||||
--bg-light: #f8f9fa;
|
||||
}
|
||||
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: 'Noto Sans TC', sans-serif;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
min-height: 100vh;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.container {
|
||||
max-width: 1000px;
|
||||
margin: 0 auto;
|
||||
background: white;
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.header {
|
||||
background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
|
||||
color: white;
|
||||
padding: 30px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.header h1 {
|
||||
font-size: 2rem;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.header p {
|
||||
opacity: 0.9;
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
.content {
|
||||
padding: 30px;
|
||||
}
|
||||
|
||||
.api-card {
|
||||
background: var(--bg-light);
|
||||
border: 2px solid var(--border);
|
||||
border-radius: 8px;
|
||||
padding: 20px;
|
||||
margin-bottom: 20px;
|
||||
transition: all 0.3s;
|
||||
}
|
||||
|
||||
.api-card:hover {
|
||||
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
|
||||
}
|
||||
|
||||
.api-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
|
||||
.api-name {
|
||||
font-size: 1.3rem;
|
||||
font-weight: 600;
|
||||
color: var(--primary);
|
||||
}
|
||||
|
||||
.status-badge {
|
||||
padding: 6px 12px;
|
||||
border-radius: 20px;
|
||||
font-size: 0.85rem;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.status-pending {
|
||||
background: #e8e8e8;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.status-success {
|
||||
background: #d4edda;
|
||||
color: var(--success);
|
||||
}
|
||||
|
||||
.status-error {
|
||||
background: #f8d7da;
|
||||
color: var(--danger);
|
||||
}
|
||||
|
||||
.status-testing {
|
||||
background: #fff3cd;
|
||||
color: var(--warning);
|
||||
}
|
||||
|
||||
.api-info {
|
||||
display: grid;
|
||||
grid-template-columns: 120px 1fr;
|
||||
gap: 10px;
|
||||
margin-bottom: 15px;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.api-info-label {
|
||||
font-weight: 600;
|
||||
color: #555;
|
||||
}
|
||||
|
||||
.api-info-value {
|
||||
color: #333;
|
||||
word-break: break-all;
|
||||
}
|
||||
|
||||
.api-key-display {
|
||||
font-family: 'Courier New', monospace;
|
||||
background: white;
|
||||
padding: 8px;
|
||||
border-radius: 4px;
|
||||
border: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.api-key-masked {
|
||||
color: #999;
|
||||
}
|
||||
|
||||
.api-actions {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
margin-top: 15px;
|
||||
}
|
||||
|
||||
.btn {
|
||||
padding: 10px 20px;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
font-size: 0.9rem;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s;
|
||||
font-family: 'Noto Sans TC', sans-serif;
|
||||
}
|
||||
|
||||
.btn:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background: var(--primary);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-primary:hover:not(:disabled) {
|
||||
background: var(--primary-light);
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 4px 12px rgba(26, 82, 118, 0.3);
|
||||
}
|
||||
|
||||
.btn-success {
|
||||
background: var(--success);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-success:hover:not(:disabled) {
|
||||
background: #229954;
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
.result-message {
|
||||
margin-top: 15px;
|
||||
padding: 12px;
|
||||
border-radius: 6px;
|
||||
font-size: 0.9rem;
|
||||
display: none;
|
||||
}
|
||||
|
||||
.result-message.show {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.result-success {
|
||||
background: #d4edda;
|
||||
color: var(--success);
|
||||
border: 1px solid var(--success);
|
||||
}
|
||||
|
||||
.result-error {
|
||||
background: #f8d7da;
|
||||
color: var(--danger);
|
||||
border: 1px solid var(--danger);
|
||||
}
|
||||
|
||||
.test-all-section {
|
||||
text-align: center;
|
||||
margin: 30px 0;
|
||||
padding: 20px;
|
||||
background: var(--bg-light);
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.test-all-btn {
|
||||
padding: 15px 40px;
|
||||
font-size: 1.1rem;
|
||||
}
|
||||
|
||||
.loading-spinner {
|
||||
display: inline-block;
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
border: 3px solid rgba(255,255,255,.3);
|
||||
border-radius: 50%;
|
||||
border-top-color: white;
|
||||
animation: spin 1s ease-in-out infinite;
|
||||
margin-left: 8px;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
to { transform: rotate(360deg); }
|
||||
}
|
||||
|
||||
.summary {
|
||||
background: white;
|
||||
border: 2px solid var(--primary);
|
||||
border-radius: 8px;
|
||||
padding: 20px;
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
|
||||
.summary h2 {
|
||||
color: var(--primary);
|
||||
margin-bottom: 15px;
|
||||
font-size: 1.3rem;
|
||||
}
|
||||
|
||||
.summary-stats {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
|
||||
gap: 15px;
|
||||
}
|
||||
|
||||
.stat-box {
|
||||
text-align: center;
|
||||
padding: 15px;
|
||||
border-radius: 6px;
|
||||
background: var(--bg-light);
|
||||
}
|
||||
|
||||
.stat-value {
|
||||
font-size: 2rem;
|
||||
font-weight: 700;
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
|
||||
.stat-label {
|
||||
font-size: 0.85rem;
|
||||
color: #666;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="header">
|
||||
<h1>🔌 LLM API 連線測試</h1>
|
||||
<p>HR 基礎資料維護系統 - API 配置檢測</p>
|
||||
</div>
|
||||
|
||||
<div class="content">
|
||||
<!-- Summary Section -->
|
||||
<div class="summary">
|
||||
<h2>📊 API 配置總覽</h2>
|
||||
<div class="summary-stats">
|
||||
<div class="stat-box">
|
||||
<div class="stat-value" id="totalApis">3</div>
|
||||
<div class="stat-label">API 總數</div>
|
||||
</div>
|
||||
<div class="stat-box">
|
||||
<div class="stat-value" id="configuredApis" style="color: var(--success);">0</div>
|
||||
<div class="stat-label">已配置</div>
|
||||
</div>
|
||||
<div class="stat-box">
|
||||
<div class="stat-value" id="testedApis" style="color: var(--primary);">0</div>
|
||||
<div class="stat-label">已測試</div>
|
||||
</div>
|
||||
<div class="stat-box">
|
||||
<div class="stat-value" id="successApis" style="color: var(--success);">0</div>
|
||||
<div class="stat-label">測試成功</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Test All Button -->
|
||||
<div class="test-all-section">
|
||||
<button class="btn btn-success test-all-btn" onclick="testAllAPIs()">
|
||||
🚀 測試所有已配置的 API
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- Gemini API Card -->
|
||||
<div class="api-card" id="gemini-card">
|
||||
<div class="api-header">
|
||||
<div class="api-name">🔷 Google Gemini</div>
|
||||
<div class="status-badge status-pending" id="gemini-status">未測試</div>
|
||||
</div>
|
||||
<div class="api-info">
|
||||
<div class="api-info-label">API Key:</div>
|
||||
<div class="api-info-value">
|
||||
<div class="api-key-display" id="gemini-key">
|
||||
<span class="api-key-masked">••••••••••••••••</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="api-info-label">Endpoint:</div>
|
||||
<div class="api-info-value">https://generativelanguage.googleapis.com</div>
|
||||
<div class="api-info-label">狀態:</div>
|
||||
<div class="api-info-value" id="gemini-config-status">等待檢測...</div>
|
||||
</div>
|
||||
<div class="api-actions">
|
||||
<button class="btn btn-primary" onclick="testAPI('gemini')" id="gemini-test-btn">
|
||||
🧪 測試連線
|
||||
</button>
|
||||
</div>
|
||||
<div class="result-message" id="gemini-result"></div>
|
||||
</div>
|
||||
|
||||
<!-- DeepSeek API Card -->
|
||||
<div class="api-card" id="deepseek-card">
|
||||
<div class="api-header">
|
||||
<div class="api-name">🔵 DeepSeek</div>
|
||||
<div class="status-badge status-pending" id="deepseek-status">未測試</div>
|
||||
</div>
|
||||
<div class="api-info">
|
||||
<div class="api-info-label">API Key:</div>
|
||||
<div class="api-info-value">
|
||||
<div class="api-key-display" id="deepseek-key">
|
||||
<span class="api-key-masked">••••••••••••••••</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="api-info-label">Endpoint:</div>
|
||||
<div class="api-info-value">https://api.deepseek.com/v1</div>
|
||||
<div class="api-info-label">狀態:</div>
|
||||
<div class="api-info-value" id="deepseek-config-status">等待檢測...</div>
|
||||
</div>
|
||||
<div class="api-actions">
|
||||
<button class="btn btn-primary" onclick="testAPI('deepseek')" id="deepseek-test-btn">
|
||||
🧪 測試連線
|
||||
</button>
|
||||
</div>
|
||||
<div class="result-message" id="deepseek-result"></div>
|
||||
</div>
|
||||
|
||||
<!-- OpenAI API Card -->
|
||||
<div class="api-card" id="openai-card">
|
||||
<div class="api-header">
|
||||
<div class="api-name">🟢 OpenAI</div>
|
||||
<div class="status-badge status-pending" id="openai-status">未測試</div>
|
||||
</div>
|
||||
<div class="api-info">
|
||||
<div class="api-info-label">API Key:</div>
|
||||
<div class="api-info-value">
|
||||
<div class="api-key-display" id="openai-key">
|
||||
<span class="api-key-masked">••••••••••••••••</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="api-info-label">Endpoint:</div>
|
||||
<div class="api-info-value">https://api.openai.com/v1</div>
|
||||
<div class="api-info-label">狀態:</div>
|
||||
<div class="api-info-value" id="openai-config-status">等待檢測...</div>
|
||||
</div>
|
||||
<div class="api-actions">
|
||||
<button class="btn btn-primary" onclick="testAPI('openai')" id="openai-test-btn">
|
||||
🧪 測試連線
|
||||
</button>
|
||||
</div>
|
||||
<div class="result-message" id="openai-result"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// Check API configuration on page load
|
||||
window.addEventListener('DOMContentLoaded', checkAPIConfiguration);
|
||||
|
||||
async function checkAPIConfiguration() {
|
||||
try {
|
||||
const response = await fetch('/api/llm/config');
|
||||
const data = await response.json();
|
||||
|
||||
let configured = 0;
|
||||
let tested = 0;
|
||||
|
||||
// Update UI for each API
|
||||
['gemini', 'deepseek', 'openai'].forEach(api => {
|
||||
const config = data[api];
|
||||
const isConfigured = config && config.enabled;
|
||||
|
||||
if (isConfigured) {
|
||||
configured++;
|
||||
document.getElementById(`${api}-config-status`).textContent = '✓ 已配置';
|
||||
document.getElementById(`${api}-config-status`).style.color = 'var(--success)';
|
||||
|
||||
// Show partial API key
|
||||
if (config.api_key) {
|
||||
const maskedKey = config.api_key.substring(0, 8) + '••••••••' + config.api_key.substring(config.api_key.length - 4);
|
||||
document.getElementById(`${api}-key`).innerHTML = `<span>${maskedKey}</span>`;
|
||||
}
|
||||
} else {
|
||||
document.getElementById(`${api}-config-status`).textContent = '✗ 未配置';
|
||||
document.getElementById(`${api}-config-status`).style.color = 'var(--danger)';
|
||||
document.getElementById(`${api}-test-btn`).disabled = true;
|
||||
}
|
||||
});
|
||||
|
||||
// Update summary
|
||||
document.getElementById('configuredApis').textContent = configured;
|
||||
|
||||
} catch (error) {
|
||||
showError('無法載入 API 配置');
|
||||
}
|
||||
}
|
||||
|
||||
async function testAPI(apiName) {
|
||||
const statusBadge = document.getElementById(`${apiName}-status`);
|
||||
const resultDiv = document.getElementById(`${apiName}-result`);
|
||||
const testBtn = document.getElementById(`${apiName}-test-btn`);
|
||||
|
||||
// Update UI to testing state
|
||||
statusBadge.className = 'status-badge status-testing';
|
||||
statusBadge.innerHTML = '測試中<span class="loading-spinner"></span>';
|
||||
testBtn.disabled = true;
|
||||
resultDiv.className = 'result-message';
|
||||
resultDiv.style.display = 'none';
|
||||
|
||||
try {
|
||||
const response = await fetch(`/api/llm/test/${apiName}`);
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success) {
|
||||
statusBadge.className = 'status-badge status-success';
|
||||
statusBadge.textContent = '✓ 連線成功';
|
||||
resultDiv.className = 'result-message result-success show';
|
||||
resultDiv.textContent = `✓ ${data.message}`;
|
||||
updateSummary('tested', true);
|
||||
} else {
|
||||
statusBadge.className = 'status-badge status-error';
|
||||
statusBadge.textContent = '✗ 連線失敗';
|
||||
resultDiv.className = 'result-message result-error show';
|
||||
resultDiv.textContent = `✗ ${data.message}`;
|
||||
updateSummary('tested', false);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
statusBadge.className = 'status-badge status-error';
|
||||
statusBadge.textContent = '✗ 錯誤';
|
||||
resultDiv.className = 'result-message result-error show';
|
||||
resultDiv.textContent = `✗ 發生錯誤: ${error.message}`;
|
||||
updateSummary('tested', false);
|
||||
} finally {
|
||||
testBtn.disabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function testAllAPIs() {
|
||||
const apis = ['gemini', 'deepseek', 'openai'];
|
||||
const testBtn = document.querySelector('.test-all-btn');
|
||||
|
||||
testBtn.disabled = true;
|
||||
testBtn.innerHTML = '測試中<span class="loading-spinner"></span>';
|
||||
|
||||
for (const api of apis) {
|
||||
const isEnabled = !document.getElementById(`${api}-test-btn`).disabled;
|
||||
if (isEnabled) {
|
||||
await testAPI(api);
|
||||
// Small delay between tests
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
}
|
||||
}
|
||||
|
||||
testBtn.disabled = false;
|
||||
testBtn.innerHTML = '🚀 測試所有已配置的 API';
|
||||
}
|
||||
|
||||
function updateSummary(type, success) {
|
||||
const testedEl = document.getElementById('testedApis');
|
||||
const successEl = document.getElementById('successApis');
|
||||
|
||||
if (type === 'tested') {
|
||||
const currentTested = parseInt(testedEl.textContent) || 0;
|
||||
testedEl.textContent = currentTested + 1;
|
||||
|
||||
if (success) {
|
||||
const currentSuccess = parseInt(successEl.textContent) || 0;
|
||||
successEl.textContent = currentSuccess + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function showError(message) {
|
||||
alert(message);
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user