cwadayi commited on
Commit
b84a794
·
verified ·
1 Parent(s): 5b7bb17

Update ai_service.py

Browse files
Files changed (1) hide show
  1. ai_service.py +12 -15
ai_service.py CHANGED
@@ -1,4 +1,4 @@
1
- # ai_service.py (Improved tool description for date inference)
2
  import json
3
  import os
4
  import random
@@ -31,8 +31,8 @@ else:
31
  def call_mcp_earthquake_search(
32
  start_date: str,
33
  end_date: str,
34
- min_magnitude: float = 4.5,
35
- max_magnitude: float = 8.0
36
  ) -> str:
37
  """根據指定的條件(時間、規模)從遠端伺服器搜尋地震事件。"""
38
  try:
@@ -67,8 +67,6 @@ def call_mcp_pws_search() -> str:
67
 
68
  # --- 3. 向 Gemini 定義工具 (Tool Declarations) ---
69
 
70
- # [*** 核心修正 ***]
71
- # 強化參數描述,引導 AI 自動從對話中推斷日期
72
  earthquake_search_tool_declaration = {
73
  "name": "call_earthquake_search_tool",
74
  "description": "從台灣中央氣象署的資料庫中搜尋地震事件。",
@@ -88,14 +86,10 @@ earthquake_search_tool_declaration = {
88
  }
89
  }
90
 
91
-
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
  available_tools = {
@@ -107,14 +101,16 @@ available_tools = {
107
  model = None
108
  if selected_key:
109
  try:
 
 
110
  system_instruction = (
111
  "You are a helpful AI assistant. You must answer in Traditional Chinese."
112
- "You have access to tools. When a tool returns data, "
113
- "you MUST prioritize showing the detailed, raw data returned from the tool. "
114
- "Do not summarize unless the user explicitly asks for a summary. "
115
- "Format the tool's output clearly for the user."
116
  )
117
- model = genai.GenerativeModel(
118
  model_name="gemini-1.5-flash",
119
  tools=[earthquake_search_tool_declaration, pws_search_tool_declaration],
120
  system_instruction=system_instruction
@@ -147,3 +143,4 @@ def generate_ai_text(user_prompt: str) -> str:
147
  except Exception as e:
148
  print(f"與 Gemini AI 互動時發生錯誤: {e}")
149
  return f"🤖 AI 服務發生錯誤: {e}"
 
 
1
+ # ai_service.py (Instructs AI to assume 'largest' means by magnitude)
2
  import json
3
  import os
4
  import random
 
31
  def call_mcp_earthquake_search(
32
  start_date: str,
33
  end_date: str,
34
+ min_magnitude: float = 4.0, #降低最小規模門檻以獲取更多資料
35
+ max_magnitude: float = 9.0
36
  ) -> str:
37
  """根據指定的條件(時間、規模)從遠端伺服器搜尋地震事件。"""
38
  try:
 
67
 
68
  # --- 3. 向 Gemini 定義工具 (Tool Declarations) ---
69
 
 
 
70
  earthquake_search_tool_declaration = {
71
  "name": "call_earthquake_search_tool",
72
  "description": "從台灣中央氣象署的資料庫中搜尋地震事件。",
 
86
  }
87
  }
88
 
 
89
  pws_search_tool_declaration = {
90
  "name": "call_mcp_pws_search",
91
  "description": "查詢最新的 PWS (Public Weather Service) 公共天氣服務發布情形。",
92
+ "parameters": {"type": "OBJECT", "properties": {}}
 
 
 
93
  }
94
 
95
  available_tools = {
 
101
  model = None
102
  if selected_key:
103
  try:
104
+ # [*** 核心修正 ***]
105
+ # 新增指令,要求 AI 在遇到「最大」等模糊詞彙時,主動以「規模」作為判斷標準
106
  system_instruction = (
107
  "You are a helpful AI assistant. You must answer in Traditional Chinese."
108
+ "You have access to tools. When the user asks for the 'largest' or 'strongest' earthquake, "
109
+ "you MUST assume they mean by magnitude. You should then analyze the JSON data returned by the tool, "
110
+ "find the single entry with the highest 'magnitude' value, and present that specific earthquake's details as the answer."
111
+ "Do not simply show all the data; find the maximum and answer the question directly."
112
  )
113
+ model = genai.GenerateContent(
114
  model_name="gemini-1.5-flash",
115
  tools=[earthquake_search_tool_declaration, pws_search_tool_declaration],
116
  system_instruction=system_instruction
 
143
  except Exception as e:
144
  print(f"與 Gemini AI 互動時發生錯誤: {e}")
145
  return f"🤖 AI 服務發生錯誤: {e}"
146
+