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

Update ai_service.py

Browse files
Files changed (1) hide show
  1. ai_service.py +26 -16
ai_service.py CHANGED
@@ -1,4 +1,4 @@
1
- # ai_service.py (Implements API Key rotation)
2
  import json
3
  import os
4
  import random
@@ -7,23 +7,17 @@ import google.generativeai as genai
7
  from gradio_client import Client
8
 
9
  # 從設定檔匯入金鑰和 URL
10
- from config import MCP_SERVER_URL #不再直接從config匯入GEMINI_API_KEY
11
 
12
  # --- 1. 設定 Gemini API 金鑰 (金鑰輪替邏輯) ---
13
-
14
- # [核心修改] 讀取並輪流使用兩組 API 金鑰
15
- # 從環境變數讀取您設定的兩組金鑰
16
  key1 = os.getenv("GEMINI_API_KEY")
17
  key2 = os.getenv("GEMINI_API_KEY2")
18
-
19
- # 建立一個包含所有有效金鑰的列表
20
  valid_keys = []
21
  if key1 and "YOUR_GEMINI_API_KEY" not in key1:
22
  valid_keys.append(key1)
23
  if key2 and "YOUR_GEMINI_API_KEY" not in key2:
24
  valid_keys.append(key2)
25
 
26
- # 如果列表中有可用的金鑰,就隨機選取一個來設定
27
  selected_key = None
28
  if valid_keys:
29
  selected_key = random.choice(valid_keys)
@@ -73,18 +67,35 @@ def call_mcp_pws_search() -> str:
73
 
74
  # --- 3. 向 Gemini 定義工具 (Tool Declarations) ---
75
 
 
 
76
  earthquake_search_tool_declaration = {
77
- "name": "call_earthquake_search_tool", "description": "從台灣中央氣象署的資料庫中搜尋地震事件。",
78
- "parameters": { "type": "OBJECT", "properties": {
79
- "start_date": {"type": "STRING", "description": "搜尋的開始日期 (格式 'YYYY-MM-DD')。"},
80
- "end_date": {"type": "STRING", "description": "搜尋的結束日期 (格式 'YYYY-MM-DD')。"},
81
- }, "required": ["start_date", "end_date"]
 
 
 
 
 
 
 
 
 
 
82
  }
83
  }
84
 
 
85
  pws_search_tool_declaration = {
86
- "name": "call_mcp_pws_search", "description": "查詢最新的 PWS (Public Weather Service) 公共天氣服務發布情形。",
87
- "parameters": {"type": "OBJECT", "properties": {}}
 
 
 
 
88
  }
89
 
90
  available_tools = {
@@ -94,7 +105,6 @@ available_tools = {
94
 
95
  # --- 4. 建立 Gemini 模型 ---
96
  model = None
97
- # [核心修改] 只有在成功選取到key之後才建立模型
98
  if selected_key:
99
  try:
100
  system_instruction = (
 
1
+ # ai_service.py (Improved tool description for date inference)
2
  import json
3
  import os
4
  import random
 
7
  from gradio_client import Client
8
 
9
  # 從設定檔匯入金鑰和 URL
10
+ from config import MCP_SERVER_URL
11
 
12
  # --- 1. 設定 Gemini API 金鑰 (金鑰輪替邏輯) ---
 
 
 
13
  key1 = os.getenv("GEMINI_API_KEY")
14
  key2 = os.getenv("GEMINI_API_KEY2")
 
 
15
  valid_keys = []
16
  if key1 and "YOUR_GEMINI_API_KEY" not in key1:
17
  valid_keys.append(key1)
18
  if key2 and "YOUR_GEMINI_API_KEY" not in key2:
19
  valid_keys.append(key2)
20
 
 
21
  selected_key = None
22
  if valid_keys:
23
  selected_key = random.choice(valid_keys)
 
67
 
68
  # --- 3. 向 Gemini 定義工具 (Tool Declarations) ---
69
 
70
+ # [*** 核心修正 ***]
71
+ # 強化參數描述,引導 AI 自動從對話中推斷日期
72
  earthquake_search_tool_declaration = {
73
+ "name": "call_earthquake_search_tool",
74
+ "description": "從台灣中央氣象署的資料庫中搜尋地震事件。",
75
+ "parameters": {
76
+ "type": "OBJECT",
77
+ "properties": {
78
+ "start_date": {
79
+ "type": "STRING",
80
+ "description": "搜尋的開始日期 (格式 'YYYY-MM-DD')。模型應根據使用者問題中的相對時間(例如:昨天、上個月、去年)或絕對時間(例如:2024年)來主動推斷此日期。"
81
+ },
82
+ "end_date": {
83
+ "type": "STRING",
84
+ "description": "搜尋的結束日期 (格式 'YYYY-MM-DD')。模型應根據使用者問題中的相對時間或絕對時間來主動推斷此日期。"
85
+ },
86
+ },
87
+ "required": ["start_date", "end_date"]
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 = {
 
105
 
106
  # --- 4. 建立 Gemini 模型 ---
107
  model = None
 
108
  if selected_key:
109
  try:
110
  system_instruction = (