新增 logo、優化 AI 問答
This commit is contained in:
@@ -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(),
|
||||
|
Reference in New Issue
Block a user