cwadayi commited on
Commit
b5a3d27
·
verified ·
1 Parent(s): a7dcc8d

Update ai_service.py

Browse files
Files changed (1) hide show
  1. ai_service.py +24 -10
ai_service.py CHANGED
@@ -1,4 +1,4 @@
1
- # ai_service.py (Final Version with System Prompt)
2
  import json
3
  from datetime import datetime
4
  import google.generativeai as genai
@@ -48,27 +48,41 @@ def call_mcp_earthquake_search(
48
  print(f"呼叫 MCP 伺服器失敗: {e}")
49
  return f"工具執行失敗,錯誤訊息: {e}"
50
 
51
- # --- 3. 向 Gemini 定義工具 ---
52
  earthquake_search_tool_declaration = {
53
  "name": "call_earthquake_search_tool",
54
  "description": "根據指定的條件(時間、地點、規模等)從台灣中央氣象署的資料庫中搜尋地震事件。預設搜尋台灣周邊地區。",
55
  "parameters": {
56
- "type": "OBJECT", "properties": {
57
- "start_date": {"type": "STRING", "description": "搜尋的開始日期,格式為 'YYYY-MM-DD'。"},
58
- "end_date": {"type": "STRING", "description": f"搜尋的結束日期,格式為 'YYYY-MM-DD'。預設為今天: {datetime.now().strftime('%Y-%m-%d')}。"},
59
- "min_magnitude": {"type": "NUMBER", "description": "要搜尋的最小地震規模。預設為 4.5。"},
60
- "max_magnitude": {"type": "NUMBER", "description": "要搜尋的最大地震規模。預設為 8.0。"},
61
- }, "required": ["start_date", "end_date", "min_magnitude"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  }
63
  }
64
 
65
  available_tools = {"call_earthquake_search_tool": call_mcp_earthquake_search}
66
 
67
- # --- 4. 建立 Gemini 模型 (Singleton 模式) ---
68
  model = None
69
  if GEMINI_API_KEY and "YOUR_GEMINI_API_KEY" not in GEMINI_API_KEY:
70
  try:
71
- # [修正] 加入 system_instruction 賦予 AI 分析能力和指定語言
72
  system_instruction = (
73
  "You are a helpful AI assistant. You must answer in Traditional Chinese."
74
  "You have access to tools. When a tool returns data in JSON format, "
 
1
+ # ai_service.py (Final Version with Improved Tool Definition)
2
  import json
3
  from datetime import datetime
4
  import google.generativeai as genai
 
48
  print(f"呼叫 MCP 伺服器失敗: {e}")
49
  return f"工具執行失敗,錯誤訊息: {e}"
50
 
51
+ # --- 3. [修正] 向 Gemini 定義更清晰的工具 ---
52
  earthquake_search_tool_declaration = {
53
  "name": "call_earthquake_search_tool",
54
  "description": "根據指定的條件(時間、地點、規模等)從台灣中央氣象署的資料庫中搜尋地震事件。預設搜尋台灣周邊地區。",
55
  "parameters": {
56
+ "type": "OBJECT",
57
+ "properties": {
58
+ "start_date": {
59
+ "type": "STRING",
60
+ "description": "搜尋的開始日期 (格式 'YYYY-MM-DD')。模型應根據使用者問題推斷此日期,例如從『去年』或『2024年』推斷出 '2024-01-01'。"
61
+ },
62
+ "end_date": {
63
+ "type": "STRING",
64
+ "description": "搜尋的結束日期 (格式 'YYYY-MM-DD')。模型應根據使用者問題推斷此日期,例如從『昨天』或『2024年』推斷出 '2024-12-31'。"
65
+ },
66
+ "min_magnitude": {
67
+ "type": "NUMBER",
68
+ "description": "要搜尋的最小地震規模。如果使用者未指定,請使用預設值 4.5。"
69
+ },
70
+ "max_magnitude": {
71
+ "type": "NUMBER",
72
+ "description": "要搜尋的最大地震規模。預設為 8.0。"
73
+ },
74
+ },
75
+ # 將 min_magnitude 改為非必填,讓 AI 更靈活
76
+ "required": ["start_date", "end_date"]
77
  }
78
  }
79
 
80
  available_tools = {"call_earthquake_search_tool": call_mcp_earthquake_search}
81
 
82
+ # --- 4. 建立 Gemini 模型 ---
83
  model = None
84
  if GEMINI_API_KEY and "YOUR_GEMINI_API_KEY" not in GEMINI_API_KEY:
85
  try:
 
86
  system_instruction = (
87
  "You are a helpful AI assistant. You must answer in Traditional Chinese."
88
  "You have access to tools. When a tool returns data in JSON format, "