在上一篇我們探討了檔案上傳的技術實作,今天我們要深入「智能分析引擎」:六大專家分析系統。
這個系統能夠讓 AI 智能助手根據不同的專業領域切換身份,從數學解題專家到文學評析大師,從翻譯服務專家到商業分析顧問,提供深度的專業分析服務。
六大專家分析系統提供:
def analyze_file_content_tool(self, analysis_type: str = "詳細分析", expert_type: str = "學術分析專家", expert_prompt: str = "") -> str:
"""分析已上傳的檔案內容,根據AI智能判斷的專家身份進行分析"""
try:
# 檢查是否有已上傳的檔案
if not self.app_instance or not hasattr(self.app_instance, 'last_uploaded_file_content'):
return "請先上傳檔案再進行分析。"
file_content = self.app_instance.last_uploaded_file_content
filename = getattr(self.app_instance, 'last_uploaded_filename', '未知檔案')
if not file_content.strip():
return "檔案內容為空,無法進行分析。"
主函數特色:
if "數學專家" in expert_type and ("解題" in analysis_type or "解答" in analysis_type or "求解" in analysis_type):
analysis_prompt = f"""{expert_prompt}
請仔細分析並解決以下檔案中的數學題目或問題:
檔案名稱:{filename}
檔案內容:
{file_content}
請以數學專家的身份提供:
1. 問題識別和理解
2. 詳細的解題步驟
3. 計算過程(如果需要)
4. 最終答案
5. 驗證或檢查(如果適用)
6. 相關的數學概念解釋
請用繁體中文回應,步驟要清晰易懂,展現專業的數學知識。"""
數學專家特色:
elif "文學專家" in expert_type:
analysis_prompt = f"""{expert_prompt}
請根據以下檔案內容寫出專業的文學分析:
檔案名稱:{filename}
檔案內容:
{file_content}
請以文學專家的身份提供:
1. 作品主題和文學意義
2. 寫作技巧和文學手法分析
3. 人物形象和情節發展
4. 語言風格和修辭特色
5. 個人的專業見解和評價
6. 文學價值和啟發
請用繁體中文回應,展現深度的文學洞察力。"""
文學專家特色:
elif "翻譯專家" in expert_type:
analysis_prompt = f"""{expert_prompt}
請將以下檔案內容進行專業翻譯:
檔案名稱:{filename}
檔案內容:
{file_content}
請以翻譯專家的身份提供:
1. 準確且自然的翻譯
2. 重要術語的翻譯說明
3. 文化背景的適當處理
4. 語言風格的保持
請提供高品質的專業翻譯。"""
翻譯專家特色:
elif "信息整理專家" in expert_type and ("總結" in analysis_type or "摘要" in analysis_type or "重點" in analysis_type):
analysis_prompt = f"""{expert_prompt}
請對以下檔案進行專業的信息整理:
檔案名稱:{filename}
檔案內容:
{file_content}
請以信息整理專家的身份提供:
1. 核心主題和關鍵概念
2. 重要信息的結構化整理
3. 關鍵數據和事實
4. 需要特別注意的要點
5. 邏輯清晰的總結
請用繁體中文回應,以專業的方式進行信息整理。"""
資訊整理專家特色:
elif "科學專家" in expert_type:
analysis_prompt = f"""{expert_prompt}
請對以下科學相關檔案進行專業分析:
檔案名稱:{filename}
檔案內容:
{file_content}
請以科學專家的身份提供:
1. 科學概念和理論背景
2. 實驗設計和方法分析
3. 數據解釋和結論評估
4. 科學意義和應用價值
5. 可能的改進建議
6. 相關的科學知識擴展
請用繁體中文回應,展現嚴謹的科學分析能力。"""
科學專家特色:
elif "商業分析專家" in expert_type:
analysis_prompt = f"""{expert_prompt}
請對以下商業相關檔案進行專業分析:
檔案名稱:{filename}
檔案內容:
{file_content}
請以商業分析專家的身份提供:
1. 商業模式和策略分析
2. 市場環境和競爭分析
3. 財務和營運評估
4. 風險和機會識別
5. 專業的商業建議
6. 未來發展趨勢預測
請用繁體中文回應,展現專業的商業洞察力。"""
商業分析專家特色:
else:
# 預設使用學術分析專家
analysis_prompt = f"""{expert_prompt}
請對以下檔案進行學術層面的專業分析:
檔案名稱:{filename}
檔案內容:
{file_content}
請以學術分析專家的身份,根據檔案的具體內容類型(如論文、報告、文獻等),提供相應的學術分析:
1. 內容主題和學術意義
2. 方法論和研究設計評估
3. 論證邏輯和證據分析
4. 學術貢獻和創新點
5. 批判性思考和改進建議
6. 相關領域的學術價值
請用繁體中文回應,展現嚴謹的學術分析能力。"""
學術分析專家特色:
# 使用Gemini模型進行內容分析
response = self.content_model.generate_content(analysis_prompt)
analysis_result = response.text
# 轉換為繁體中文
analysis_result = self.opencc_converter.convert(analysis_result)
處理流程特色:
# 保存分析結果到app_instance,供後續Word操作使用
if self.app_instance:
import time
self.app_instance.last_analysis_result = analysis_result
self.app_instance.last_analysis_time = time.time()
print(f"[DEBUG] 已保存分析結果到app_instance,長度: {len(analysis_result)},時間戳: {self.app_instance.last_analysis_time}")
return f"📋 {expert_type}分析結果:{filename}\n\n{analysis_result}"
狀態管理特色:
except Exception as e:
# 當API配額用完時,提示用戶
if "quota" in str(e).lower() or "429" in str(e):
return f"檔案分析功能暫時不可用(API配額已用完)。請稍後重試或考慮升級API方案。原始錯誤:{str(e)}"
else:
return f"分析檔案內容時發生錯誤: {e}"
錯誤處理特色:
# 檢查是否有已上傳的檔案
if not self.app_instance or not hasattr(self.app_instance, 'last_uploaded_file_content'):
return "請先上傳檔案再進行分析。"
file_content = self.app_instance.last_uploaded_file_content
filename = getattr(self.app_instance, 'last_uploaded_filename', '未知檔案')
if not file_content.strip():
return "檔案內容為空,無法進行分析。"
驗證機制特色:
getattr
避免屬性不存在的錯誤# 數學專家的條件觸發
if "數學專家" in expert_type and ("解題" in analysis_type or "解答" in analysis_type or "求解" in analysis_type):
# 資訊整理專家的條件觸發
elif "信息整理專家" in expert_type and ("總結" in analysis_type or "摘要" in analysis_type or "重點" in analysis_type):
每個專家模式都有專門設計的提示詞結構:
# 轉換為繁體中文
analysis_result = self.opencc_converter.convert(analysis_result)
# 多重狀態保存
self.app_instance.last_analysis_result = analysis_result
self.app_instance.last_analysis_time = time.time()
同步機制特色:
六大專家智能分析系統是智能助手的「專業大腦」,它:
這個系統讓 AI 助手不再是通用的對話機器人,而是能夠提供專業級分析服務的智能專家團隊,真正實現了「一個助手,多種專業」的理想。
下一篇,我們將探討 Word 文件操作系統,看看 AI 如何將分析結果完美整合到文件編輯功能中。