/** * LLM 連線測試元件 * 測試 Gemini, DeepSeek, OpenAI 三種 LLM API 的連線狀態 */ import React, { useState } from 'react'; import axios from 'axios'; import './LLMConnectionTest.css'; const LLMConnectionTest = () => { const [testResults, setTestResults] = useState({ gemini: null, deepseek: null, openai: null, }); const [testing, setTesting] = useState({ gemini: false, deepseek: false, openai: false, }); const [testingAll, setTestingAll] = useState(false); const providers = [ { id: 'gemini', name: 'Google Gemini', icon: '🤖', color: '#4285f4', }, { id: 'deepseek', name: 'DeepSeek', icon: '🧠', color: '#7c3aed', }, { id: 'openai', name: 'OpenAI', icon: '✨', color: '#10a37f', }, ]; /** * 測試單一 LLM 連線 */ const testConnection = async (provider) => { setTesting((prev) => ({ ...prev, [provider]: true })); setTestResults((prev) => ({ ...prev, [provider]: null })); try { const response = await axios.post(`/api/llm/test/${provider}`); setTestResults((prev) => ({ ...prev, [provider]: response.data })); } catch (error) { const errorData = error.response?.data || { success: false, message: error.message || '連線測試失敗', provider, }; setTestResults((prev) => ({ ...prev, [provider]: errorData })); } finally { setTesting((prev) => ({ ...prev, [provider]: false })); } }; /** * 測試所有 LLM 連線 */ const testAllConnections = async () => { setTestingAll(true); setTestResults({ gemini: null, deepseek: null, openai: null, }); try { const response = await axios.post('/api/llm/test/all'); setTestResults(response.data); } catch (error) { console.error('測試所有連線失敗:', error); } finally { setTestingAll(false); } }; /** * 取得測試結果樣式 */ const getResultClass = (result) => { if (!result) return ''; return result.success ? 'success' : 'failure'; }; /** * 取得測試結果圖示 */ const getResultIcon = (result) => { if (!result) return '⏳'; return result.success ? '✅' : '❌'; }; return (

LLM API 連線測試

測試與外部 LLM 服務的連線狀態,確保 API 金鑰配置正確

{providers.map((provider) => { const result = testResults[provider.id]; const isTesting = testing[provider.id]; return (
{provider.icon}

{provider.name}

{getResultIcon(result)}
{result && (
{result.success ? '連線成功' : '連線失敗'}

{result.message}

{result.model && (
模型: {result.model}
)} {result.error && (
錯誤詳情
{result.error}
)}
)} {!result && !isTesting && (

尚未測試

)}
); })}
ℹ️
提示:

請確保在 .env 文件中正確配置了對應的 API 金鑰

  • GEMINI_API_KEY - Google Gemini API 金鑰
  • DEEPSEEK_API_KEY - DeepSeek API 金鑰
  • OPENAI_API_KEY - OpenAI API 金鑰
); }; export default LLMConnectionTest;