Spaces:
Sleeping
Sleeping
chen commited on
Update ai_service.py
Browse files1123426 unmodified lines3132333435343536373828 unmodified lines676869707170717213 unmodified lines8687889189909195969798929394955 unmodified lines10110210310410510610711211311411510810911011111211711311411511626 unmodified lines143144145146# ai_service.py (Improved tool description for date inference)# ai_service.py (Instructs AI to assume 'largest' means by magnitude)import jsonimport osimport random26 unmodified linesdef call_mcp_earthquake_search( start_date: str, end_date: str, min_magnitude: float = 4.5, max_magnitude: float = 8.0 min_magnitude: float = 4.0, #降低最小規模門檻以獲取更多資料 max_magnitude: float = 9.0) -> str: """根據指定的條件(時間、規模)從遠端伺服器搜尋地震事件。""" try:28 unmodified lines
# --- 3. 向 Gemini 定義工具 (Tool Declarations) ---
# [*** 核心修正 ***]# 強化參數描述,引導 AI 自動從對話中推斷日期earthquake_search_tool_declaration = { "name": "call_earthquake_search_tool", "description": "從台灣中央氣象署的資料庫中搜尋地震事件。",13 unmodified lines }}
pws_search_tool_declaration = { "name": "call_mcp_pws_search", "description": "查詢最新的 PWS (Public Weather Service) 公共天氣服務發布情形。", "parameters": { "type": "OBJECT", "properties": {} } "parameters": {"type": "OBJECT", "properties": {}}}
available_tools = {5 unmodified linesmodel = Noneif selected_key: try: # [*** 核心修正 ***] # 新增指令,要求 AI 在遇到「最大」等模糊詞彙時,主動以「規模」作為判斷標準 system_instruction = ( "You are a helpful AI assistant. You must answer in Traditional Chinese." "You have access to tools. When a tool returns data, " "you MUST prioritize showing the detailed, raw data returned from the tool. " "Do not summarize unless the user explicitly asks for a summary. " "Format the tool's output clearly for the user." "You have access to tools. When the user asks for the 'largest' or 'strongest' earthquake, " "you MUST assume they mean by magnitude. You should then analyze the JSON data returned by the tool, " "find the single entry with the highest 'magnitude' value, and present that specific earthquake's details as the answer." "Do not simply show all the data; find the maximum and answer the question directly." ) model = genai.GenerativeModel( model = genai.GenerateContent( model_name="gemini-1.5-flash", tools=[earthquake_search_tool_declaration, pws_search_tool_declaration], system_instruction=system_instruction26 unmodified lines except Exception as e: print(f"與 Gemini AI 互動時發生錯誤: {e}") return f"🤖 AI 服務發生錯誤: {e}"
ai_service.py+12-15