Spaces:
Sleeping
Sleeping
Update ai_service.py
Browse files- ai_service.py +34 -75
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
|
|
@@ -13,7 +13,6 @@ if GEMINI_API_KEY and "YOUR_GEMINI_API_KEY" not in GEMINI_API_KEY:
|
|
| 13 |
|
| 14 |
# --- 2. 工具函式 (Tool Functions) ---
|
| 15 |
|
| 16 |
-
# [既有] 地震查詢工具函式
|
| 17 |
def call_mcp_earthquake_search(
|
| 18 |
start_date: str,
|
| 19 |
end_date: str,
|
|
@@ -23,82 +22,48 @@ def call_mcp_earthquake_search(
|
|
| 23 |
"""根據指定的條件(時間、規模)從遠端伺服器搜尋地震事件。"""
|
| 24 |
try:
|
| 25 |
print(f"--- 正在呼叫遠端地震 MCP 伺服器 (由 Gemini 觸發) ---")
|
| 26 |
-
print(f" 查詢條件: {start_date} 到 {end_date}, 規模 {min_magnitude} 以上")
|
| 27 |
-
|
| 28 |
client = Client(src=MCP_SERVER_URL)
|
| 29 |
result = client.predict(
|
| 30 |
param_0=start_date, param_1="00:00:00",
|
| 31 |
param_2=end_date, param_3="23:59:59",
|
| 32 |
-
param_4=21.0, param_5=26.0,
|
| 33 |
-
param_6=119.0, param_7=123.0, # 預設台灣經度
|
| 34 |
param_8=0.0, param_9=100.0,
|
| 35 |
param_10=min_magnitude, param_11=max_magnitude,
|
| 36 |
api_name="/gradio_fetch_and_plot_data"
|
| 37 |
)
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
print("--- MCP 伺服器回傳:未找到符合條件的地震 ---")
|
| 43 |
-
return "查詢完成,但未找到任何符合條件的地震資料。"
|
| 44 |
-
|
| 45 |
-
headers = dataframe_dict.get('headers', [])
|
| 46 |
-
formatted_results = [dict(zip(headers, row)) for row in data]
|
| 47 |
-
print(f"--- MCP 伺服器成功回傳 {len(data)} 筆資料 ---")
|
| 48 |
-
return json.dumps(formatted_results, indent=2, ensure_ascii=False)
|
| 49 |
except Exception as e:
|
| 50 |
-
print(f"呼叫 MCP 伺服器失敗: {e}")
|
| 51 |
return f"工具執行失敗,錯誤訊息: {e}"
|
| 52 |
|
| 53 |
-
# [修改] PWS 查詢工具函式
|
| 54 |
def call_mcp_pws_search() -> str:
|
| 55 |
"""從遠端伺服器查詢最新的 PWS (Public Weather Service) 發布情形。"""
|
| 56 |
try:
|
| 57 |
print(f"--- 正在呼叫遠端 PWS MCP 伺服器 (由 Gemini 觸發) ---")
|
| 58 |
client = Client("cwadayi/MCP-pws")
|
| 59 |
-
|
| 60 |
-
# [*** 核心修正 ***]
|
| 61 |
-
# 明確指定要呼叫的 API 名稱,對應到 MCP_pws_get_disaster_warnings
|
| 62 |
result = client.predict(api_name="/MCP_pws_get_disaster_warnings")
|
| 63 |
-
|
| 64 |
-
if isinstance(result, tuple) and len(result) > 0:
|
| 65 |
-
report = result[0]
|
| 66 |
-
else:
|
| 67 |
-
report = str(result)
|
| 68 |
-
|
| 69 |
-
print(f"--- PWS MCP 伺服器成功回傳 ---")
|
| 70 |
-
return report
|
| 71 |
except Exception as e:
|
| 72 |
-
print(f"呼叫 PWS MCP 伺服器失敗: {e}")
|
| 73 |
return f"工具執行失敗,錯誤訊息: {e}"
|
| 74 |
|
| 75 |
# --- 3. 向 Gemini 定義工具 (Tool Declarations) ---
|
| 76 |
|
| 77 |
-
# [既有] 地震查詢工具定義
|
| 78 |
earthquake_search_tool_declaration = {
|
| 79 |
-
"name": "call_earthquake_search_tool",
|
| 80 |
-
"
|
| 81 |
-
"parameters": {
|
| 82 |
-
"type": "OBJECT", "properties": {
|
| 83 |
"start_date": {"type": "STRING", "description": "搜尋的開始日期 (格式 'YYYY-MM-DD')。"},
|
| 84 |
"end_date": {"type": "STRING", "description": "搜尋的結束日期 (格式 'YYYY-MM-DD')。"},
|
| 85 |
-
"min_magnitude": {"type": "NUMBER", "description": "要搜尋的最小地震規模。"},
|
| 86 |
-
"max_magnitude": {"type": "NUMBER", "description": "要搜尋的最大地震規模。"},
|
| 87 |
}, "required": ["start_date", "end_date"]
|
| 88 |
}
|
| 89 |
}
|
| 90 |
|
| 91 |
-
# [維持不變] PWS 查詢工具定義
|
| 92 |
pws_search_tool_declaration = {
|
| 93 |
-
"name": "call_mcp_pws_search",
|
| 94 |
-
"
|
| 95 |
-
"parameters": {
|
| 96 |
-
"type": "OBJECT",
|
| 97 |
-
"properties": {}
|
| 98 |
-
}
|
| 99 |
}
|
| 100 |
|
| 101 |
-
# [維持不變] 註冊所有工具
|
| 102 |
available_tools = {
|
| 103 |
"call_earthquake_search_tool": call_mcp_earthquake_search,
|
| 104 |
"call_mcp_pws_search": call_mcp_pws_search
|
|
@@ -108,11 +73,7 @@ available_tools = {
|
|
| 108 |
model = None
|
| 109 |
if GEMINI_API_KEY and "YOUR_GEMINI_API_KEY" not in GEMINI_API_KEY:
|
| 110 |
try:
|
| 111 |
-
system_instruction =
|
| 112 |
-
"You are a helpful AI assistant. You must answer in Traditional Chinese."
|
| 113 |
-
"You have access to tools. When a tool returns data, "
|
| 114 |
-
"you must analyze the data to fully answer the user's question."
|
| 115 |
-
)
|
| 116 |
model = genai.GenerativeModel(
|
| 117 |
model_name="gemini-1.5-flash",
|
| 118 |
tools=[earthquake_search_tool_declaration, pws_search_tool_declaration],
|
|
@@ -121,36 +82,34 @@ if GEMINI_API_KEY and "YOUR_GEMINI_API_KEY" not in GEMINI_API_KEY:
|
|
| 121 |
except Exception as e:
|
| 122 |
print(f"建立 Gemini 模型失敗: {e}")
|
| 123 |
|
| 124 |
-
# --- 5. 主要的 AI 文字生成函式
|
| 125 |
def generate_ai_text(user_prompt: str) -> str:
|
| 126 |
-
if not model:
|
| 127 |
-
return "🤖 AI (Gemini) 服務尚未設定 API 金鑰,或金鑰無效。"
|
| 128 |
try:
|
| 129 |
-
print(f"--- 開始 Gemini 對話,使用者輸入: '{user_prompt}' ---")
|
| 130 |
chat = model.start_chat()
|
| 131 |
response = chat.send_message(user_prompt)
|
| 132 |
-
try:
|
| 133 |
-
function_call = response.candidates[0].content.parts[0].function_call
|
| 134 |
-
except (IndexError, AttributeError):
|
| 135 |
-
function_call = None
|
| 136 |
-
if not function_call:
|
| 137 |
-
print("--- Gemini 直接回覆文字 ---")
|
| 138 |
-
return response.text
|
| 139 |
-
|
| 140 |
-
print(f"--- Gemini 要求呼叫工具: {function_call.name} ---")
|
| 141 |
-
tool_function = available_tools.get(function_call.name)
|
| 142 |
-
if not tool_function:
|
| 143 |
-
return f"錯誤:模型嘗試呼叫一個不存在的工具 '{function_call.name}'。"
|
| 144 |
-
|
| 145 |
-
tool_result = tool_function(**dict(function_call.args))
|
| 146 |
-
print("--- 將工具結果回傳給 Gemini ---")
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
-
|
| 153 |
return response.text
|
|
|
|
| 154 |
except Exception as e:
|
| 155 |
print(f"與 Gemini AI 互動時發生錯誤: {e}")
|
| 156 |
-
return f"🤖 AI 服務發生錯誤: {e}"
|
|
|
|
| 1 |
+
# ai_service.py (已修正 function_call 處理邏輯)
|
| 2 |
import json
|
| 3 |
from datetime import datetime
|
| 4 |
import google.generativeai as genai
|
|
|
|
| 13 |
|
| 14 |
# --- 2. 工具函式 (Tool Functions) ---
|
| 15 |
|
|
|
|
| 16 |
def call_mcp_earthquake_search(
|
| 17 |
start_date: str,
|
| 18 |
end_date: str,
|
|
|
|
| 22 |
"""根據指定的條件(時間、規模)從遠端伺服器搜尋地震事件。"""
|
| 23 |
try:
|
| 24 |
print(f"--- 正在呼叫遠端地震 MCP 伺服器 (由 Gemini 觸發) ---")
|
|
|
|
|
|
|
| 25 |
client = Client(src=MCP_SERVER_URL)
|
| 26 |
result = client.predict(
|
| 27 |
param_0=start_date, param_1="00:00:00",
|
| 28 |
param_2=end_date, param_3="23:59:59",
|
| 29 |
+
param_4=21.0, param_5=26.0, param_6=119.0, param_7=123.0,
|
|
|
|
| 30 |
param_8=0.0, param_9=100.0,
|
| 31 |
param_10=min_magnitude, param_11=max_magnitude,
|
| 32 |
api_name="/gradio_fetch_and_plot_data"
|
| 33 |
)
|
| 34 |
+
data = result[0].get('data', [])
|
| 35 |
+
if not data: return "查詢完成,但未找到任何符合條件的地震資料。"
|
| 36 |
+
headers = result[0].get('headers', [])
|
| 37 |
+
return json.dumps([dict(zip(headers, row)) for row in data], indent=2, ensure_ascii=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
except Exception as e:
|
|
|
|
| 39 |
return f"工具執行失敗,錯誤訊息: {e}"
|
| 40 |
|
|
|
|
| 41 |
def call_mcp_pws_search() -> str:
|
| 42 |
"""從遠端伺服器查詢最新的 PWS (Public Weather Service) 發布情形。"""
|
| 43 |
try:
|
| 44 |
print(f"--- 正在呼叫遠端 PWS MCP 伺服器 (由 Gemini 觸發) ---")
|
| 45 |
client = Client("cwadayi/MCP-pws")
|
|
|
|
|
|
|
|
|
|
| 46 |
result = client.predict(api_name="/MCP_pws_get_disaster_warnings")
|
| 47 |
+
return result[0] if isinstance(result, tuple) and len(result) > 0 else str(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
except Exception as e:
|
|
|
|
| 49 |
return f"工具執行失敗,錯誤訊息: {e}"
|
| 50 |
|
| 51 |
# --- 3. 向 Gemini 定義工具 (Tool Declarations) ---
|
| 52 |
|
|
|
|
| 53 |
earthquake_search_tool_declaration = {
|
| 54 |
+
"name": "call_earthquake_search_tool", "description": "從台灣中央氣象署的資料庫中搜尋地震事件。",
|
| 55 |
+
"parameters": { "type": "OBJECT", "properties": {
|
|
|
|
|
|
|
| 56 |
"start_date": {"type": "STRING", "description": "搜尋的開始日期 (格式 'YYYY-MM-DD')。"},
|
| 57 |
"end_date": {"type": "STRING", "description": "搜尋的結束日期 (格式 'YYYY-MM-DD')。"},
|
|
|
|
|
|
|
| 58 |
}, "required": ["start_date", "end_date"]
|
| 59 |
}
|
| 60 |
}
|
| 61 |
|
|
|
|
| 62 |
pws_search_tool_declaration = {
|
| 63 |
+
"name": "call_mcp_pws_search", "description": "查詢最新的 PWS (Public Weather Service) 公共天氣服務發布情形。",
|
| 64 |
+
"parameters": {"type": "OBJECT", "properties": {}}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
}
|
| 66 |
|
|
|
|
| 67 |
available_tools = {
|
| 68 |
"call_earthquake_search_tool": call_mcp_earthquake_search,
|
| 69 |
"call_mcp_pws_search": call_mcp_pws_search
|
|
|
|
| 73 |
model = None
|
| 74 |
if GEMINI_API_KEY and "YOUR_GEMINI_API_KEY" not in GEMINI_API_KEY:
|
| 75 |
try:
|
| 76 |
+
system_instruction = "You are a helpful AI assistant. You must answer in Traditional Chinese. You have access to tools. When a tool returns data, you must analyze the data to fully answer the user's question."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
model = genai.GenerativeModel(
|
| 78 |
model_name="gemini-1.5-flash",
|
| 79 |
tools=[earthquake_search_tool_declaration, pws_search_tool_declaration],
|
|
|
|
| 82 |
except Exception as e:
|
| 83 |
print(f"建立 Gemini 模型失敗: {e}")
|
| 84 |
|
| 85 |
+
# --- 5. 主要的 AI 文字生成函式 ---
|
| 86 |
def generate_ai_text(user_prompt: str) -> str:
|
| 87 |
+
if not model: return "🤖 AI (Gemini) 服務尚未設定 API 金鑰,或金鑰無效。"
|
|
|
|
| 88 |
try:
|
|
|
|
| 89 |
chat = model.start_chat()
|
| 90 |
response = chat.send_message(user_prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
# [*** 核心修正 ***]
|
| 93 |
+
# 迭代檢查模型的回應部分,而不是直接存取
|
| 94 |
+
for part in response.parts:
|
| 95 |
+
# 如果回應部分是函式呼叫,則執行它
|
| 96 |
+
if part.function_call:
|
| 97 |
+
function_call = part.function_call
|
| 98 |
+
tool_function = available_tools.get(function_call.name)
|
| 99 |
+
if not tool_function:
|
| 100 |
+
return f"錯誤:模型嘗試呼叫一個不存在的工具 '{function_call.name}'。"
|
| 101 |
+
|
| 102 |
+
tool_result = tool_function(**dict(function_call.args))
|
| 103 |
+
|
| 104 |
+
# 將工具執行的結果送回給模型
|
| 105 |
+
final_response = chat.send_message(
|
| 106 |
+
{"function_response": {"name": function_call.name, "response": {"result": tool_result}}}
|
| 107 |
+
)
|
| 108 |
+
return final_response.text
|
| 109 |
|
| 110 |
+
# 如果沒有函式呼叫,直接回傳文字結果
|
| 111 |
return response.text
|
| 112 |
+
|
| 113 |
except Exception as e:
|
| 114 |
print(f"與 Gemini AI 互動時發生錯誤: {e}")
|
| 115 |
+
return f"🤖 AI 服務發生錯誤: {e}"
|