Spaces:
Sleeping
Sleeping
Update ai_service.py
Browse files- ai_service.py +33 -26
ai_service.py
CHANGED
|
@@ -1,15 +1,36 @@
|
|
| 1 |
-
# ai_service.py (
|
| 2 |
import json
|
|
|
|
|
|
|
| 3 |
from datetime import datetime
|
| 4 |
import google.generativeai as genai
|
| 5 |
from gradio_client import Client
|
| 6 |
|
| 7 |
# 從設定檔匯入金鑰和 URL
|
| 8 |
-
from config import GEMINI_API_KEY
|
| 9 |
|
| 10 |
-
# --- 1. 設定 Gemini API 金鑰 (
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# --- 2. 工具函式 (Tool Functions) ---
|
| 15 |
|
|
@@ -44,13 +65,8 @@ def call_mcp_pws_search() -> str:
|
|
| 44 |
"""從遠端伺服器查詢最新的 PWS (Public Weather Service) 發布情形。"""
|
| 45 |
try:
|
| 46 |
print(f"--- 正在呼叫遠端 PWS MCP 伺服器 (由 Gemini 觸發) ---")
|
| 47 |
-
|
| 48 |
-
# [*** 核心修正 ***]
|
| 49 |
-
# 將 PWS 工具的 Hugging Face Space 位址更新為新的 running 版本
|
| 50 |
client = Client("cwadayi/MCP-pws-running")
|
| 51 |
-
|
| 52 |
result = client.predict(None, api_name="/get_disaster_warnings")
|
| 53 |
-
|
| 54 |
return result[0] if isinstance(result, tuple) and len(result) > 0 else str(result)
|
| 55 |
except Exception as e:
|
| 56 |
return f"工具執行失敗,錯誤訊息: {e}"
|
|
@@ -58,25 +74,17 @@ def call_mcp_pws_search() -> str:
|
|
| 58 |
# --- 3. 向 Gemini 定義工具 (Tool Declarations) ---
|
| 59 |
|
| 60 |
earthquake_search_tool_declaration = {
|
| 61 |
-
"name": "call_earthquake_search_tool",
|
| 62 |
-
"
|
| 63 |
-
"parameters": {
|
| 64 |
-
"type": "OBJECT",
|
| 65 |
-
"properties": {
|
| 66 |
"start_date": {"type": "STRING", "description": "搜尋的開始日期 (格式 'YYYY-MM-DD')。"},
|
| 67 |
"end_date": {"type": "STRING", "description": "搜尋的結束日期 (格式 'YYYY-MM-DD')。"},
|
| 68 |
-
},
|
| 69 |
-
"required": ["start_date", "end_date"]
|
| 70 |
}
|
| 71 |
}
|
| 72 |
|
| 73 |
pws_search_tool_declaration = {
|
| 74 |
-
"name": "call_mcp_pws_search",
|
| 75 |
-
"
|
| 76 |
-
"parameters": {
|
| 77 |
-
"type": "OBJECT",
|
| 78 |
-
"properties": {}
|
| 79 |
-
}
|
| 80 |
}
|
| 81 |
|
| 82 |
available_tools = {
|
|
@@ -86,7 +94,8 @@ available_tools = {
|
|
| 86 |
|
| 87 |
# --- 4. 建立 Gemini 模型 ---
|
| 88 |
model = None
|
| 89 |
-
|
|
|
|
| 90 |
try:
|
| 91 |
system_instruction = (
|
| 92 |
"You are a helpful AI assistant. You must answer in Traditional Chinese."
|
|
@@ -110,7 +119,6 @@ def generate_ai_text(user_prompt: str) -> str:
|
|
| 110 |
try:
|
| 111 |
chat = model.start_chat()
|
| 112 |
response = chat.send_message(user_prompt)
|
| 113 |
-
|
| 114 |
for part in response.parts:
|
| 115 |
if part.function_call:
|
| 116 |
function_call = part.function_call
|
|
@@ -126,7 +134,6 @@ def generate_ai_text(user_prompt: str) -> str:
|
|
| 126 |
return final_response.text
|
| 127 |
|
| 128 |
return response.text
|
| 129 |
-
|
| 130 |
except Exception as e:
|
| 131 |
print(f"與 Gemini AI 互動時發生錯誤: {e}")
|
| 132 |
return f"🤖 AI 服務發生錯誤: {e}"
|
|
|
|
| 1 |
+
# ai_service.py (Implements API Key rotation)
|
| 2 |
import json
|
| 3 |
+
import os
|
| 4 |
+
import random
|
| 5 |
from datetime import datetime
|
| 6 |
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)
|
| 30 |
+
print(f"--- Configuring Gemini. Selected key ending in: ...{selected_key[-4:]} ---")
|
| 31 |
+
genai.configure(api_key=selected_key)
|
| 32 |
+
else:
|
| 33 |
+
print("--- No valid Gemini API Key found. AI Service will be disabled. ---")
|
| 34 |
|
| 35 |
# --- 2. 工具函式 (Tool Functions) ---
|
| 36 |
|
|
|
|
| 65 |
"""從遠端伺服器查詢最新的 PWS (Public Weather Service) 發布情形。"""
|
| 66 |
try:
|
| 67 |
print(f"--- 正在呼叫遠端 PWS MCP 伺服器 (由 Gemini 觸發) ---")
|
|
|
|
|
|
|
|
|
|
| 68 |
client = Client("cwadayi/MCP-pws-running")
|
|
|
|
| 69 |
result = client.predict(None, api_name="/get_disaster_warnings")
|
|
|
|
| 70 |
return result[0] if isinstance(result, tuple) and len(result) > 0 else str(result)
|
| 71 |
except Exception as e:
|
| 72 |
return f"工具執行失敗,錯誤訊息: {e}"
|
|
|
|
| 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 |
|
| 95 |
# --- 4. 建立 Gemini 模型 ---
|
| 96 |
model = None
|
| 97 |
+
# [核心修改] 只有在成功選取到key之後才建立模型
|
| 98 |
+
if selected_key:
|
| 99 |
try:
|
| 100 |
system_instruction = (
|
| 101 |
"You are a helpful AI assistant. You must answer in Traditional Chinese."
|
|
|
|
| 119 |
try:
|
| 120 |
chat = model.start_chat()
|
| 121 |
response = chat.send_message(user_prompt)
|
|
|
|
| 122 |
for part in response.parts:
|
| 123 |
if part.function_call:
|
| 124 |
function_call = part.function_call
|
|
|
|
| 134 |
return final_response.text
|
| 135 |
|
| 136 |
return response.text
|
|
|
|
| 137 |
except Exception as e:
|
| 138 |
print(f"與 Gemini AI 互動時發生錯誤: {e}")
|
| 139 |
return f"🤖 AI 服務發生錯誤: {e}"
|