cwadayi commited on
Commit
996f7e2
·
verified ·
1 Parent(s): d541e2c

Update ai_service.py

Browse files
Files changed (1) hide show
  1. ai_service.py +17 -22
ai_service.py CHANGED
@@ -1,4 +1,4 @@
1
- # ai_service.py (Final version, added None placeholder for Gradio button input)
2
  import json
3
  from datetime import datetime
4
  import google.generativeai as genai
@@ -45,10 +45,7 @@ def call_mcp_pws_search() -> str:
45
  try:
46
  print(f"--- 正在呼叫遠端 PWS MCP 伺服器 (由 Gemini 觸發) ---")
47
  client = Client("cwadayi/MCP-pws")
48
-
49
- # [核心修正] 傳入一個 None 作為伺服器端按鈕事件所需的佔位符輸入
50
  result = client.predict(None, api_name="/get_disaster_warnings")
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,25 +53,17 @@ def call_mcp_pws_search() -> str:
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 = {
@@ -86,7 +75,15 @@ available_tools = {
86
  model = None
87
  if GEMINI_API_KEY and "YOUR_GEMINI_API_KEY" not in GEMINI_API_KEY:
88
  try:
89
- 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."
 
 
 
 
 
 
 
 
90
  model = genai.GenerativeModel(
91
  model_name="gemini-1.5-flash",
92
  tools=[earthquake_search_tool_declaration, pws_search_tool_declaration],
@@ -97,12 +94,10 @@ if GEMINI_API_KEY and "YOUR_GEMINI_API_KEY" not in GEMINI_API_KEY:
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
@@ -118,7 +113,7 @@ def generate_ai_text(user_prompt: str) -> str:
118
  return final_response.text
119
 
120
  return response.text
121
-
122
  except Exception as e:
123
  print(f"與 Gemini AI 互動時發生錯誤: {e}")
124
  return f"🤖 AI 服務發生錯誤: {e}"
 
 
1
+ # ai_service.py (Modified system_instruction to prefer detailed tool output)
2
  import json
3
  from datetime import datetime
4
  import google.generativeai as genai
 
45
  try:
46
  print(f"--- 正在呼叫遠端 PWS MCP 伺服器 (由 Gemini 觸發) ---")
47
  client = Client("cwadayi/MCP-pws")
 
 
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
  # --- 3. 向 Gemini 定義工具 (Tool Declarations) ---
54
 
55
  earthquake_search_tool_declaration = {
56
+ "name": "call_earthquake_search_tool", "description": "從台灣中央氣象署的資料庫中搜尋地震事件。",
57
+ "parameters": { "type": "OBJECT", "properties": {
 
 
 
58
  "start_date": {"type": "STRING", "description": "搜尋的開始日期 (格式 'YYYY-MM-DD')。"},
59
  "end_date": {"type": "STRING", "description": "搜尋的結束日期 (格式 'YYYY-MM-DD')。"},
60
+ }, "required": ["start_date", "end_date"]
 
61
  }
62
  }
63
 
64
  pws_search_tool_declaration = {
65
+ "name": "call_mcp_pws_search", "description": "查詢最新的 PWS (Public Weather Service) 公共天氣服務發布情形。",
66
+ "parameters": {"type": "OBJECT", "properties": {}}
 
 
 
 
67
  }
68
 
69
  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, "
83
+ "you MUST prioritize showing the detailed, raw data returned from the tool. "
84
+ "Do not summarize unless the user explicitly asks for a summary. "
85
+ "Format the tool's output clearly for the user."
86
+ )
87
  model = genai.GenerativeModel(
88
  model_name="gemini-1.5-flash",
89
  tools=[earthquake_search_tool_declaration, pws_search_tool_declaration],
 
94
 
95
  # --- 5. 主要的 AI 文字生成函式 ---
96
  def generate_ai_text(user_prompt: str) -> str:
97
+ if not model: return "🤖 AI (Gemini) 服務尚未設定 API 金鑰,或金鑰無效。"
 
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
  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
+