Spaces:
Sleeping
Sleeping
Update ai_service.py
Browse files- ai_service.py +24 -15
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
|
|
@@ -26,13 +26,15 @@ def call_mcp_earthquake_search(
|
|
| 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,
|
|
|
|
| 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:
|
|
|
|
| 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:
|
|
@@ -43,7 +45,10 @@ def call_mcp_pws_search() -> str:
|
|
| 43 |
try:
|
| 44 |
print(f"--- 正在呼叫遠端 PWS MCP 伺服器 (由 Gemini 觸發) ---")
|
| 45 |
client = Client("cwadayi/MCP-pws")
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
return result[0] if isinstance(result, tuple) and len(result) > 0 else str(result)
|
| 48 |
except Exception as e:
|
| 49 |
return f"工具執行失敗,錯誤訊息: {e}"
|
|
@@ -51,17 +56,25 @@ def call_mcp_pws_search() -> str:
|
|
| 51 |
# --- 3. 向 Gemini 定義工具 (Tool Declarations) ---
|
| 52 |
|
| 53 |
earthquake_search_tool_declaration = {
|
| 54 |
-
"name": "call_earthquake_search_tool",
|
| 55 |
-
"
|
|
|
|
|
|
|
|
|
|
| 56 |
"start_date": {"type": "STRING", "description": "搜尋的開始日期 (格式 'YYYY-MM-DD')。"},
|
| 57 |
"end_date": {"type": "STRING", "description": "搜尋的結束日期 (格式 'YYYY-MM-DD')。"},
|
| 58 |
-
},
|
|
|
|
| 59 |
}
|
| 60 |
}
|
| 61 |
|
| 62 |
pws_search_tool_declaration = {
|
| 63 |
-
"name": "call_mcp_pws_search",
|
| 64 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
}
|
| 66 |
|
| 67 |
available_tools = {
|
|
@@ -84,15 +97,13 @@ if GEMINI_API_KEY and "YOUR_GEMINI_API_KEY" not in GEMINI_API_KEY:
|
|
| 84 |
|
| 85 |
# --- 5. 主要的 AI 文字生成函式 ---
|
| 86 |
def generate_ai_text(user_prompt: str) -> str:
|
| 87 |
-
if not model:
|
|
|
|
| 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)
|
|
@@ -101,13 +112,11 @@ def generate_ai_text(user_prompt: str) -> str:
|
|
| 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:
|
|
|
|
| 1 |
+
# ai_service.py (Final version, removed api_name for PWS tool)
|
| 2 |
import json
|
| 3 |
from datetime import datetime
|
| 4 |
import google.generativeai as genai
|
|
|
|
| 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,
|
| 30 |
+
param_6=119.0, param_7=123.0,
|
| 31 |
param_8=0.0, param_9=100.0,
|
| 32 |
param_10=min_magnitude, param_11=max_magnitude,
|
| 33 |
api_name="/gradio_fetch_and_plot_data"
|
| 34 |
)
|
| 35 |
data = result[0].get('data', [])
|
| 36 |
+
if not data:
|
| 37 |
+
return "查詢完成,但未找到任何符合條件的地震資料。"
|
| 38 |
headers = result[0].get('headers', [])
|
| 39 |
return json.dumps([dict(zip(headers, row)) for row in data], indent=2, ensure_ascii=False)
|
| 40 |
except Exception as e:
|
|
|
|
| 45 |
try:
|
| 46 |
print(f"--- 正在呼叫遠端 PWS MCP 伺服器 (由 Gemini 觸發) ---")
|
| 47 |
client = Client("cwadayi/MCP-pws")
|
| 48 |
+
|
| 49 |
+
# [核心修正] 移除 api_name 參數,讓 gradio_client 自動尋找預設的 API 端點。
|
| 50 |
+
result = client.predict()
|
| 51 |
+
|
| 52 |
return result[0] if isinstance(result, tuple) and len(result) > 0 else str(result)
|
| 53 |
except Exception as e:
|
| 54 |
return f"工具執行失敗,錯誤訊息: {e}"
|
|
|
|
| 56 |
# --- 3. 向 Gemini 定義工具 (Tool Declarations) ---
|
| 57 |
|
| 58 |
earthquake_search_tool_declaration = {
|
| 59 |
+
"name": "call_earthquake_search_tool",
|
| 60 |
+
"description": "從台灣中央氣象署的資料庫中搜尋地震事件。",
|
| 61 |
+
"parameters": {
|
| 62 |
+
"type": "OBJECT",
|
| 63 |
+
"properties": {
|
| 64 |
"start_date": {"type": "STRING", "description": "搜尋的開始日期 (格式 'YYYY-MM-DD')。"},
|
| 65 |
"end_date": {"type": "STRING", "description": "搜尋的結束日期 (格式 'YYYY-MM-DD')。"},
|
| 66 |
+
},
|
| 67 |
+
"required": ["start_date", "end_date"]
|
| 68 |
}
|
| 69 |
}
|
| 70 |
|
| 71 |
pws_search_tool_declaration = {
|
| 72 |
+
"name": "call_mcp_pws_search",
|
| 73 |
+
"description": "查詢最新的 PWS (Public Weather Service) 公共天氣服務發布情形。",
|
| 74 |
+
"parameters": {
|
| 75 |
+
"type": "OBJECT",
|
| 76 |
+
"properties": {}
|
| 77 |
+
}
|
| 78 |
}
|
| 79 |
|
| 80 |
available_tools = {
|
|
|
|
| 97 |
|
| 98 |
# --- 5. 主要的 AI 文字生成函式 ---
|
| 99 |
def generate_ai_text(user_prompt: str) -> str:
|
| 100 |
+
if not model:
|
| 101 |
+
return "🤖 AI (Gemini) 服務尚未設定 API 金鑰,或金鑰無效。"
|
| 102 |
try:
|
| 103 |
chat = model.start_chat()
|
| 104 |
response = chat.send_message(user_prompt)
|
| 105 |
|
|
|
|
|
|
|
| 106 |
for part in response.parts:
|
|
|
|
| 107 |
if part.function_call:
|
| 108 |
function_call = part.function_call
|
| 109 |
tool_function = available_tools.get(function_call.name)
|
|
|
|
| 112 |
|
| 113 |
tool_result = tool_function(**dict(function_call.args))
|
| 114 |
|
|
|
|
| 115 |
final_response = chat.send_message(
|
| 116 |
{"function_response": {"name": function_call.name, "response": {"result": tool_result}}}
|
| 117 |
)
|
| 118 |
return final_response.text
|
| 119 |
|
|
|
|
| 120 |
return response.text
|
| 121 |
|
| 122 |
except Exception as e:
|