Spaces:
Sleeping
Sleeping
chen commited on
Update ai_service.py
Browse files123445 unmodified lines505152535455565758596061626364656621 unmodified lines8889909192939495969798991001011021031048 unmodified lines1131141151161171181192 unmodified lines122123124125126127128# ai_service.py (已整合 PWS 查詢工具)import jsonfrom datetime import datetimeimport google.generativeai as genai45 unmodified linesprint(f"呼叫 MCP 伺服器失敗: {e}")return f"工具執行失敗,錯誤訊息: {e}"# [新增] PWS 查詢工具函式def call_mcp_pws_search() -> str:"""從遠端伺服器查詢最新的 PWS (Public Weather Service) 發布情形。"""try:print(f"--- 正在呼叫遠端 PWS MCP 伺服器 (由 Gemini 觸發) ---")# 連線到使用者指定的 Gradio 應用程式client = Client("cwadayi/MCP-pws")# 假設 Gradio API 不需要參數,直接呼叫 predictresult = client.predict(api_name="/predict")# Gradio client 回傳的結果通常是元組 (tuple),取出第一個元素if isinstance(result, tuple) and len(result) > 0:report = result[0]else:21 unmodified lines}}# [新增] PWS 查詢工具定義pws_search_tool_declaration = {"name": "call_mcp_pws_search","description": "查詢最新的 PWS (Public Weather Service) 公共天氣服務發布情形。","parameters": {"type": "OBJECT","properties": {} # 此工具不需要參數}}# [修改] 將所有可用的工具函式註冊在此available_tools = {"call_earthquake_search_tool": call_mcp_earthquake_search,"call_mcp_pws_search": call_mcp_pws_search8 unmodified lines"You have access to tools. When a tool returns data, ""you must analyze the data to fully answer the user's question.")# [修改] 初始化模型時,提供所有工具的定義model = genai.GenerativeModel(model_name="gemini-1.5-flash",tools=[earthquake_search_tool_declaration, pws_search_tool_declaration],2 unmodified linesexcept Exception as e:print(f"建立 Gemini 模型失敗: {e}")# --- 5. 主要的 AI 文字生成函式 (此部分維持不變) ---def generate_ai_text(user_prompt: str) -> str:if not model:return "🤖 AI (Gemini) 服務尚未設定 API 金鑰,或金鑰無效。"123445 unmodified lines505152535455565758596061626364656621 unmodified lines8889909192939495969798991001011021031048 unmodified lines1131141151161171182 unmodified lines121122123124125126127# ai_service.py (已修正 PWS 工具呼叫目標)import jsonfrom datetime import datetimeimport google.generativeai as genai45 unmodified linesprint(f"呼叫 MCP 伺服器失敗: {e}")return f"工具執行失敗,錯誤訊息: {e}"# [修改] PWS 查詢工具函式def call_mcp_pws_search() -> str:"""從遠端伺服器查詢最新的 PWS (Public Weather Service) 發布情形。"""try:print(f"--- 正在呼叫遠端 PWS MCP 伺服器 (由 Gemini 觸發) ---")client = Client("cwadayi/MCP-pws")# [*** 核心修正 ***]# 明確指定要呼叫的 API 名稱,對應到 MCP_pws_get_disaster_warningsresult = client.predict(api_name="/MCP_pws_get_disaster_warnings")if isinstance(result, tuple) and len(result) > 0:report = result[0]else:21 unmodified lines}}# [維持不變] PWS 查詢工具定義pws_search_tool_declaration = {"name": "call_mcp_pws_search","description": "查詢最新的 PWS (Public Weather Service) 公共天氣服務發布情形。","parameters": {"type": "OBJECT","properties": {}}}# [維持不變] 註冊所有工具available_tools = {"call_earthquake_search_tool": call_mcp_earthquake_search,"call_mcp_pws_search": call_mcp_pws_search8 unmodified lines"You have access to tools. When a tool returns data, ""you must analyze the data to fully answer the user's question.")model = genai.GenerativeModel(model_name="gemini-1.5-flash",tools=[earthquake_search_tool_declaration, pws_search_tool_declaration],2 unmodified linesexcept Exception as e:print(f"建立 Gemini 模型失敗: {e}")# --- 5. 主要的 AI 文字生成函式 (維持不變) ---def generate_ai_text(user_prompt: str) -> str:if not model:return "🤖 AI (Gemini) 服務尚未設定 API 金鑰,或金鑰無效。"
ai_service.py+10-11