cwadayi commited on
Commit
80b3402
·
verified ·
1 Parent(s): ef43cc9

Update ai_service.py

Browse files
Files changed (1) hide show
  1. ai_service.py +10 -11
ai_service.py CHANGED
@@ -1,4 +1,4 @@
1
- # ai_service.py (已整合 PWS 查詢工具)
2
  import json
3
  from datetime import datetime
4
  import google.generativeai as genai
@@ -50,17 +50,17 @@ def call_mcp_earthquake_search(
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
- # 連線到使用者指定的 Gradio 應用程式
59
  client = Client("cwadayi/MCP-pws")
60
- # 假設 Gradio API 不需要參數,直接呼叫 predict
61
- result = client.predict(api_name="/predict")
62
 
63
- # Gradio client 回傳的結果通常是元組 (tuple),取出第一個元素
 
 
 
64
  if isinstance(result, tuple) and len(result) > 0:
65
  report = result[0]
66
  else:
@@ -88,17 +88,17 @@ earthquake_search_tool_declaration = {
88
  }
89
  }
90
 
91
- # [新增] PWS 查詢工具定義
92
  pws_search_tool_declaration = {
93
  "name": "call_mcp_pws_search",
94
  "description": "查詢最新的 PWS (Public Weather Service) 公共天氣服務發布情形。",
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
@@ -113,7 +113,6 @@ if GEMINI_API_KEY and "YOUR_GEMINI_API_KEY" not in GEMINI_API_KEY:
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
- # [修改] 初始化模型時,提供所有工具的定義
117
  model = genai.GenerativeModel(
118
  model_name="gemini-1.5-flash",
119
  tools=[earthquake_search_tool_declaration, pws_search_tool_declaration],
@@ -122,7 +121,7 @@ if GEMINI_API_KEY and "YOUR_GEMINI_API_KEY" not in GEMINI_API_KEY:
122
  except Exception as e:
123
  print(f"建立 Gemini 模型失敗: {e}")
124
 
125
- # --- 5. 主要的 AI 文字生成函式 (此部分維持不變) ---
126
  def generate_ai_text(user_prompt: str) -> str:
127
  if not model:
128
  return "🤖 AI (Gemini) 服務尚未設定 API 金鑰,或金鑰無效。"
 
1
+ # ai_service.py (已修正 PWS 工具呼叫目標)
2
  import json
3
  from datetime import datetime
4
  import google.generativeai as genai
 
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:
 
88
  }
89
  }
90
 
91
+ # [維持不變] PWS 查詢工具定義
92
  pws_search_tool_declaration = {
93
  "name": "call_mcp_pws_search",
94
  "description": "查詢最新的 PWS (Public Weather Service) 公共天氣服務發布情形。",
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
 
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
  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 金鑰,或金鑰無效。"