Spaces:
Sleeping
Sleeping
Update ai_service.py
Browse files- ai_service.py +17 -24
ai_service.py
CHANGED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
-
# ai_service.py (Final Version with
|
| 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, MCP_SERVER_URL
|
|
@@ -48,32 +50,17 @@ def call_mcp_earthquake_search(
|
|
| 48 |
print(f"呼叫 MCP 伺服器失敗: {e}")
|
| 49 |
return f"工具執行失敗,錯誤訊息: {e}"
|
| 50 |
|
| 51 |
-
# --- 3.
|
| 52 |
earthquake_search_tool_declaration = {
|
| 53 |
"name": "call_earthquake_search_tool",
|
| 54 |
"description": "根據指定的條件(時間、地點、規模等)從台灣中央氣象署的資料庫中搜尋地震事件。預設搜尋台灣周邊地區。",
|
| 55 |
"parameters": {
|
| 56 |
-
"type": "OBJECT",
|
| 57 |
-
|
| 58 |
-
"
|
| 59 |
-
|
| 60 |
-
|
| 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 |
|
|
@@ -113,17 +100,23 @@ def generate_ai_text(user_prompt: str) -> str:
|
|
| 113 |
if not function_call:
|
| 114 |
print("--- Gemini 直接回覆文字 ---")
|
| 115 |
return response.text
|
|
|
|
| 116 |
print(f"--- Gemini 要求呼叫工具: {function_call.name} ---")
|
| 117 |
tool_function = available_tools.get(function_call.name)
|
| 118 |
if not tool_function:
|
| 119 |
return f"錯誤:模型嘗試呼叫一個不存在的工具 '{function_call.name}'。"
|
|
|
|
| 120 |
tool_result = tool_function(**dict(function_call.args))
|
| 121 |
print("--- 將工具結果回傳給 Gemini ---")
|
|
|
|
|
|
|
| 122 |
response = chat.send_message(
|
| 123 |
-
|
| 124 |
)
|
|
|
|
| 125 |
print("--- Gemini 根據工具結果生成最終回覆 ---")
|
| 126 |
return response.text
|
| 127 |
except Exception as e:
|
| 128 |
print(f"與 Gemini AI 互動時發生錯誤: {e}")
|
| 129 |
return f"🤖 AI 服務發生錯誤: {e}"
|
|
|
|
|
|
| 1 |
+
# ai_service.py (Final Version with 'Part' import fix)
|
| 2 |
import json
|
| 3 |
from datetime import datetime
|
| 4 |
import google.generativeai as genai
|
| 5 |
from gradio_client import Client
|
| 6 |
+
# [修正] 從 google.generativeai.types 匯入 Part 類別
|
| 7 |
+
from google.generativeai.types import Part
|
| 8 |
|
| 9 |
# 從設定檔匯入金鑰和 URL
|
| 10 |
from config import GEMINI_API_KEY, MCP_SERVER_URL
|
|
|
|
| 50 |
print(f"呼叫 MCP 伺服器失敗: {e}")
|
| 51 |
return f"工具執行失敗,錯誤訊息: {e}"
|
| 52 |
|
| 53 |
+
# --- 3. 向 Gemini 定義工具 ---
|
| 54 |
earthquake_search_tool_declaration = {
|
| 55 |
"name": "call_earthquake_search_tool",
|
| 56 |
"description": "根據指定的條件(時間、地點、規模等)從台灣中央氣象署的資料庫中搜尋地震事件。預設搜尋台灣周邊地區。",
|
| 57 |
"parameters": {
|
| 58 |
+
"type": "OBJECT", "properties": {
|
| 59 |
+
"start_date": {"type": "STRING", "description": "搜尋的開始日期 (格式 'YYYY-MM-DD')。模型應根據使用者問題推斷此日期,例如從『去年』或『2024年』推斷出 '2024-01-01'。"},
|
| 60 |
+
"end_date": {"type": "STRING", "description": "搜尋的結束日期 (格式 'YYYY-MM-DD')。模型應根據使用者問題推斷此日期,例如從『昨天』或『2024年』推斷出 '2024-12-31'。"},
|
| 61 |
+
"min_magnitude": {"type": "NUMBER", "description": "要搜尋的最小地震規模。如果使用者未指定,請使用預設值 4.5。"},
|
| 62 |
+
"max_magnitude": {"type": "NUMBER", "description": "要搜尋的最大地震規模。預設為 8.0。"},
|
| 63 |
+
}, "required": ["start_date", "end_date"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
}
|
| 65 |
}
|
| 66 |
|
|
|
|
| 100 |
if not function_call:
|
| 101 |
print("--- Gemini 直接回覆文字 ---")
|
| 102 |
return response.text
|
| 103 |
+
|
| 104 |
print(f"--- Gemini 要求呼叫工具: {function_call.name} ---")
|
| 105 |
tool_function = available_tools.get(function_call.name)
|
| 106 |
if not tool_function:
|
| 107 |
return f"錯誤:模型嘗試呼叫一個不存在的工具 '{function_call.name}'。"
|
| 108 |
+
|
| 109 |
tool_result = tool_function(**dict(function_call.args))
|
| 110 |
print("--- 將工具結果回傳給 Gemini ---")
|
| 111 |
+
|
| 112 |
+
# [修正] 將 genai.Part 改為 Part (因為我們已經從 types 模組匯入)
|
| 113 |
response = chat.send_message(
|
| 114 |
+
Part(function_response={"name": function_call.name, "response": {"result": tool_result}}),
|
| 115 |
)
|
| 116 |
+
|
| 117 |
print("--- Gemini 根據工具結果生成最終回覆 ---")
|
| 118 |
return response.text
|
| 119 |
except Exception as e:
|
| 120 |
print(f"與 Gemini AI 互動時發生錯誤: {e}")
|
| 121 |
return f"🤖 AI 服務發生錯誤: {e}"
|
| 122 |
+
|