Spaces:
Sleeping
Sleeping
Update ai_service.py
Browse files- ai_service.py +11 -10
ai_service.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# ai_service.py (
|
| 2 |
import json
|
| 3 |
from datetime import datetime
|
| 4 |
import google.generativeai as genai
|
|
@@ -68,39 +68,41 @@ available_tools = {"call_earthquake_search_tool": call_mcp_earthquake_search}
|
|
| 68 |
model = None
|
| 69 |
if GEMINI_API_KEY and "YOUR_GEMINI_API_KEY" not in GEMINI_API_KEY:
|
| 70 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
model = genai.GenerativeModel(
|
| 72 |
model_name="gemini-1.5-flash",
|
| 73 |
-
tools=[earthquake_search_tool_declaration]
|
|
|
|
| 74 |
)
|
| 75 |
except Exception as e:
|
| 76 |
print(f"建立 Gemini 模型失敗: {e}")
|
| 77 |
|
| 78 |
# --- 5. 主要的 AI 文字生成函式 ---
|
| 79 |
def generate_ai_text(user_prompt: str) -> str:
|
| 80 |
-
"""使用 Gemini 模型生成回應,並在需要時觸發工具呼叫。"""
|
| 81 |
if not model:
|
| 82 |
return "🤖 AI (Gemini) 服務尚未設定 API 金鑰,或金鑰無效。"
|
| 83 |
try:
|
| 84 |
print(f"--- 開始 Gemini 對話,使用者輸入: '{user_prompt}' ---")
|
| 85 |
chat = model.start_chat()
|
| 86 |
response = chat.send_message(user_prompt)
|
| 87 |
-
|
| 88 |
-
# 檢查模型是否要求呼叫工具
|
| 89 |
try:
|
| 90 |
function_call = response.candidates[0].content.parts[0].function_call
|
| 91 |
except (IndexError, AttributeError):
|
| 92 |
function_call = None
|
| 93 |
-
|
| 94 |
if not function_call:
|
| 95 |
print("--- Gemini 直接回覆文字 ---")
|
| 96 |
return response.text
|
| 97 |
-
|
| 98 |
-
# 處理工具呼叫
|
| 99 |
print(f"--- Gemini 要求呼叫工具: {function_call.name} ---")
|
| 100 |
tool_function = available_tools.get(function_call.name)
|
| 101 |
if not tool_function:
|
| 102 |
return f"錯誤:模型嘗試呼叫一個不存在的工具 '{function_call.name}'。"
|
| 103 |
-
|
| 104 |
tool_result = tool_function(**dict(function_call.args))
|
| 105 |
print("--- 將工具結果回傳給 Gemini ---")
|
| 106 |
response = chat.send_message(
|
|
@@ -108,7 +110,6 @@ def generate_ai_text(user_prompt: str) -> str:
|
|
| 108 |
)
|
| 109 |
print("--- Gemini 根據工具結果生成最終回覆 ---")
|
| 110 |
return response.text
|
| 111 |
-
|
| 112 |
except Exception as e:
|
| 113 |
print(f"與 Gemini AI 互動時發生錯誤: {e}")
|
| 114 |
return f"🤖 AI 服務發生錯誤: {e}"
|
|
|
|
| 1 |
+
# ai_service.py (Final Version with System Prompt)
|
| 2 |
import json
|
| 3 |
from datetime import datetime
|
| 4 |
import google.generativeai as genai
|
|
|
|
| 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, "
|
| 75 |
+
"you must analyze the JSON data to fully answer the user's question. "
|
| 76 |
+
"For example, if the user asks for the largest earthquake, use the search tool for the relevant date range "
|
| 77 |
+
"and then find the entry with the highest magnitude from the JSON results before answering."
|
| 78 |
+
)
|
| 79 |
model = genai.GenerativeModel(
|
| 80 |
model_name="gemini-1.5-flash",
|
| 81 |
+
tools=[earthquake_search_tool_declaration],
|
| 82 |
+
system_instruction=system_instruction
|
| 83 |
)
|
| 84 |
except Exception as e:
|
| 85 |
print(f"建立 Gemini 模型失敗: {e}")
|
| 86 |
|
| 87 |
# --- 5. 主要的 AI 文字生成函式 ---
|
| 88 |
def generate_ai_text(user_prompt: str) -> str:
|
|
|
|
| 89 |
if not model:
|
| 90 |
return "🤖 AI (Gemini) 服務尚未設定 API 金鑰,或金鑰無效。"
|
| 91 |
try:
|
| 92 |
print(f"--- 開始 Gemini 對話,使用者輸入: '{user_prompt}' ---")
|
| 93 |
chat = model.start_chat()
|
| 94 |
response = chat.send_message(user_prompt)
|
|
|
|
|
|
|
| 95 |
try:
|
| 96 |
function_call = response.candidates[0].content.parts[0].function_call
|
| 97 |
except (IndexError, AttributeError):
|
| 98 |
function_call = None
|
|
|
|
| 99 |
if not function_call:
|
| 100 |
print("--- Gemini 直接回覆文字 ---")
|
| 101 |
return response.text
|
|
|
|
|
|
|
| 102 |
print(f"--- Gemini 要求呼叫工具: {function_call.name} ---")
|
| 103 |
tool_function = available_tools.get(function_call.name)
|
| 104 |
if not tool_function:
|
| 105 |
return f"錯誤:模型嘗試呼叫一個不存在的工具 '{function_call.name}'。"
|
|
|
|
| 106 |
tool_result = tool_function(**dict(function_call.args))
|
| 107 |
print("--- 將工具結果回傳給 Gemini ---")
|
| 108 |
response = chat.send_message(
|
|
|
|
| 110 |
)
|
| 111 |
print("--- Gemini 根據工具結果生成最終回覆 ---")
|
| 112 |
return response.text
|
|
|
|
| 113 |
except Exception as e:
|
| 114 |
print(f"與 Gemini AI 互動時發生錯誤: {e}")
|
| 115 |
return f"🤖 AI 服務發生錯誤: {e}"
|