Spaces:
Sleeping
Sleeping
Update ai_service.py
Browse files- ai_service.py +24 -11
ai_service.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# ai_service.py (
|
| 2 |
import json
|
| 3 |
from datetime import datetime
|
| 4 |
import google.generativeai as genai
|
|
@@ -44,8 +44,13 @@ def call_mcp_pws_search() -> str:
|
|
| 44 |
"""從遠端伺服器查詢最新的 PWS (Public Weather Service) 發布情形。"""
|
| 45 |
try:
|
| 46 |
print(f"--- 正在呼叫遠端 PWS MCP 伺服器 (由 Gemini 觸發) ---")
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
result = client.predict(None, api_name="/get_disaster_warnings")
|
|
|
|
| 49 |
return result[0] if isinstance(result, tuple) and len(result) > 0 else str(result)
|
| 50 |
except Exception as e:
|
| 51 |
return f"工具執行失敗,錯誤訊息: {e}"
|
|
@@ -53,17 +58,25 @@ def call_mcp_pws_search() -> str:
|
|
| 53 |
# --- 3. 向 Gemini 定義工具 (Tool Declarations) ---
|
| 54 |
|
| 55 |
earthquake_search_tool_declaration = {
|
| 56 |
-
"name": "call_earthquake_search_tool",
|
| 57 |
-
"
|
|
|
|
|
|
|
|
|
|
| 58 |
"start_date": {"type": "STRING", "description": "搜尋的開始日期 (格式 'YYYY-MM-DD')。"},
|
| 59 |
"end_date": {"type": "STRING", "description": "搜尋的結束日期 (格式 'YYYY-MM-DD')。"},
|
| 60 |
-
},
|
|
|
|
| 61 |
}
|
| 62 |
}
|
| 63 |
|
| 64 |
pws_search_tool_declaration = {
|
| 65 |
-
"name": "call_mcp_pws_search",
|
| 66 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
}
|
| 68 |
|
| 69 |
available_tools = {
|
|
@@ -75,8 +88,6 @@ available_tools = {
|
|
| 75 |
model = None
|
| 76 |
if GEMINI_API_KEY and "YOUR_GEMINI_API_KEY" not in GEMINI_API_KEY:
|
| 77 |
try:
|
| 78 |
-
# [*** 核心修正 ***]
|
| 79 |
-
# 修改系統指令,要求 AI 優先顯示工具的原始詳細回傳內容
|
| 80 |
system_instruction = (
|
| 81 |
"You are a helpful AI assistant. You must answer in Traditional Chinese."
|
| 82 |
"You have access to tools. When a tool returns data, "
|
|
@@ -94,10 +105,12 @@ if GEMINI_API_KEY and "YOUR_GEMINI_API_KEY" not in GEMINI_API_KEY:
|
|
| 94 |
|
| 95 |
# --- 5. 主要的 AI 文字生成函式 ---
|
| 96 |
def generate_ai_text(user_prompt: str) -> str:
|
| 97 |
-
if not model:
|
|
|
|
| 98 |
try:
|
| 99 |
chat = model.start_chat()
|
| 100 |
response = chat.send_message(user_prompt)
|
|
|
|
| 101 |
for part in response.parts:
|
| 102 |
if part.function_call:
|
| 103 |
function_call = part.function_call
|
|
@@ -113,7 +126,7 @@ def generate_ai_text(user_prompt: str) -> str:
|
|
| 113 |
return final_response.text
|
| 114 |
|
| 115 |
return response.text
|
|
|
|
| 116 |
except Exception as e:
|
| 117 |
print(f"與 Gemini AI 互動時發生錯誤: {e}")
|
| 118 |
return f"🤖 AI 服務發生錯誤: {e}"
|
| 119 |
-
|
|
|
|
| 1 |
+
# ai_service.py (Updated PWS tool to use the new MCP-pws-running space)
|
| 2 |
import json
|
| 3 |
from datetime import datetime
|
| 4 |
import google.generativeai as genai
|
|
|
|
| 44 |
"""從遠端伺服器查詢最新的 PWS (Public Weather Service) 發布情形。"""
|
| 45 |
try:
|
| 46 |
print(f"--- 正在呼叫遠端 PWS MCP 伺服器 (由 Gemini 觸發) ---")
|
| 47 |
+
|
| 48 |
+
# [*** 核心修正 ***]
|
| 49 |
+
# 將 PWS 工具的 Hugging Face Space 位址更新為新的 running 版本
|
| 50 |
+
client = Client("cwadayi/MCP-pws-running")
|
| 51 |
+
|
| 52 |
result = client.predict(None, api_name="/get_disaster_warnings")
|
| 53 |
+
|
| 54 |
return result[0] if isinstance(result, tuple) and len(result) > 0 else str(result)
|
| 55 |
except Exception as e:
|
| 56 |
return f"工具執行失敗,錯誤訊息: {e}"
|
|
|
|
| 58 |
# --- 3. 向 Gemini 定義工具 (Tool Declarations) ---
|
| 59 |
|
| 60 |
earthquake_search_tool_declaration = {
|
| 61 |
+
"name": "call_earthquake_search_tool",
|
| 62 |
+
"description": "從台灣中央氣象署的資料庫中搜尋地震事件。",
|
| 63 |
+
"parameters": {
|
| 64 |
+
"type": "OBJECT",
|
| 65 |
+
"properties": {
|
| 66 |
"start_date": {"type": "STRING", "description": "搜尋的開始日期 (格式 'YYYY-MM-DD')。"},
|
| 67 |
"end_date": {"type": "STRING", "description": "搜尋的結束日期 (格式 'YYYY-MM-DD')。"},
|
| 68 |
+
},
|
| 69 |
+
"required": ["start_date", "end_date"]
|
| 70 |
}
|
| 71 |
}
|
| 72 |
|
| 73 |
pws_search_tool_declaration = {
|
| 74 |
+
"name": "call_mcp_pws_search",
|
| 75 |
+
"description": "查詢最新的 PWS (Public Weather Service) 公共天氣服務發布情形。",
|
| 76 |
+
"parameters": {
|
| 77 |
+
"type": "OBJECT",
|
| 78 |
+
"properties": {}
|
| 79 |
+
}
|
| 80 |
}
|
| 81 |
|
| 82 |
available_tools = {
|
|
|
|
| 88 |
model = None
|
| 89 |
if GEMINI_API_KEY and "YOUR_GEMINI_API_KEY" not in GEMINI_API_KEY:
|
| 90 |
try:
|
|
|
|
|
|
|
| 91 |
system_instruction = (
|
| 92 |
"You are a helpful AI assistant. You must answer in Traditional Chinese."
|
| 93 |
"You have access to tools. When a tool returns data, "
|
|
|
|
| 105 |
|
| 106 |
# --- 5. 主要的 AI 文字生成函式 ---
|
| 107 |
def generate_ai_text(user_prompt: str) -> str:
|
| 108 |
+
if not model:
|
| 109 |
+
return "🤖 AI (Gemini) 服務尚未設定 API 金鑰,或金鑰無效。"
|
| 110 |
try:
|
| 111 |
chat = model.start_chat()
|
| 112 |
response = chat.send_message(user_prompt)
|
| 113 |
+
|
| 114 |
for part in response.parts:
|
| 115 |
if part.function_call:
|
| 116 |
function_call = part.function_call
|
|
|
|
| 126 |
return final_response.text
|
| 127 |
|
| 128 |
return response.text
|
| 129 |
+
|
| 130 |
except Exception as e:
|
| 131 |
print(f"與 Gemini AI 互動時發生錯誤: {e}")
|
| 132 |
return f"🤖 AI 服務發生錯誤: {e}"
|
|
|