新增 logo、優化 AI 問答

This commit is contained in:
2025-09-19 13:34:23 +08:00
parent 04f685ec0c
commit bac364a667
12 changed files with 131 additions and 75 deletions

View File

@@ -264,8 +264,12 @@ export function AdminLayout({ children, currentPage, onPageChange }: AdminLayout
{/* Logo */}
<div className="p-4 border-b">
<div className="flex items-center space-x-3">
<div className="w-8 h-8 bg-gradient-to-r from-blue-600 to-purple-600 rounded-lg flex items-center justify-center">
<Settings className="w-5 h-5 text-white" />
<div className="h-8 rounded-lg flex items-center justify-center">
<img
src="/logo.png"
alt="強茂集團 AI 展示平台"
className="h-8 object-contain"
/>
</div>
{sidebarOpen && (
<div>

View File

@@ -25,8 +25,8 @@ interface Message {
quickQuestions?: string[]
}
const DEEPSEEK_API_KEY = process.env.NEXT_PUBLIC_DEEPSEEK_API_KEY || "sk-30cac9533e5b451fa1e277fe34a7f64b"
const DEEPSEEK_API_URL = process.env.NEXT_PUBLIC_DEEPSEEK_API_URL || "https://api.deepseek.com/v1/chat/completions"
const GEMINI_API_KEY = process.env.NEXT_PUBLIC_GEMINI_API_KEY || "AIzaSyAN3pEJr_Vn2xkCidGZAq9eQqsMVvpj8g4"
const GEMINI_API_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent"
const systemPrompt = generateSystemPrompt()
@@ -77,63 +77,62 @@ export function ChatBot() {
.trim()
}
const callDeepSeekAPI = async (userMessage: string): Promise<string> => {
const callGeminiAPI = async (userMessage: string): Promise<string> => {
try {
// 構建對話歷史,只保留最近的幾條對話
const recentMessages = messages
.filter(msg => msg.sender === "user")
.slice(-5) // 只保留最近5條用戶消息
.map(msg => ({
role: "user" as const,
content: msg.text
}))
.map(msg => msg.text)
const response = await fetch(DEEPSEEK_API_URL, {
// 構建完整的對話內容
const conversationHistory = recentMessages.length > 0
? `之前的對話:\n${recentMessages.map(msg => `用戶:${msg}`).join('\n')}\n\n`
: ''
const fullPrompt = `${systemPrompt}\n\n${conversationHistory}用戶:${userMessage}`
const response = await fetch(`${GEMINI_API_URL}?key=${GEMINI_API_KEY}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${DEEPSEEK_API_KEY}`
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "deepseek-chat",
messages: [
{
role: "system",
content: systemPrompt
},
...recentMessages,
{
role: "user",
content: userMessage
}
],
max_tokens: 300,
temperature: 0.7,
stream: false
contents: [{
parts: [{
text: fullPrompt
}]
}],
generationConfig: {
maxOutputTokens: 300,
temperature: 0.7,
topP: 0.8,
topK: 10
}
})
})
if (!response.ok) {
const errorText = await response.text()
console.error("API Error:", response.status, errorText)
console.error("Gemini API Error:", response.status, errorText)
throw new Error(`API request failed: ${response.status} - ${errorText}`)
}
const data = await response.json()
if (!data.choices || !data.choices[0] || !data.choices[0].message) {
console.error("Invalid API response:", data)
if (!data.candidates || !data.candidates[0] || !data.candidates[0].content) {
console.error("Invalid Gemini API response:", data)
throw new Error("Invalid API response format")
}
const rawResponse = data.choices[0].message.content || "抱歉,我現在無法回答您的問題,請稍後再試。"
const rawResponse = data.candidates[0].content.parts[0].text || "抱歉,我現在無法回答您的問題,請稍後再試。"
return cleanResponse(rawResponse)
} catch (error) {
console.error("DeepSeek API error:", error)
console.error("Gemini API error:", error)
// 根據錯誤類型提供不同的錯誤信息
if (error instanceof Error) {
if (error.message.includes('401')) {
if (error.message.includes('401') || error.message.includes('403')) {
return "API 密鑰無效,請聯繫管理員檢查配置。"
} else if (error.message.includes('429')) {
return "API 請求過於頻繁,請稍後再試。"
@@ -261,7 +260,7 @@ export function ChatBot() {
setIsLoading(true)
try {
const aiResponse = await callDeepSeekAPI(text)
const aiResponse = await callGeminiAPI(text)
const botMessage: Message = {
id: (Date.now() + 1).toString(),