Spaces:
Sleeping
Sleeping
chen commited on
Update ai_service.py
Browse files123426 unmodified lines313233343536373828 unmodified lines676869707172737413 unmodified lines8889909192939495969798991001015 unmodified lines10710810911011111211311411511611711811912026 unmodified lines147148149# ai_service.py (Improved tool description for date inference)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) -> 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": {}}}available_tools = {5 unmodified linesmodel = Noneif selected_key:try: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.")model = genai.GenerativeModel(model_name="gemini-1.5-flash",tools=[earthquake_search_tool_declaration, pws_search_tool_declaration],system_instruction=system_instruction26 unmodified linesexcept Exception as e:print(f"與 Gemini AI 互動時發生錯誤: {e}")return f"🤖 AI 服務發生錯誤: {e}"123426 unmodified lines313233343536373828 unmodified lines67686970717213 unmodified lines868788899091929394955 unmodified lines10110210310410510610710810911011111211311411511626 unmodified lines143144145146# 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.0, #降低最小規模門檻以獲取更多資料max_magnitude: float = 9.0) -> str:"""根據指定的條件(時間、規模)從遠端伺服器搜尋地震事件。"""try:28 unmodified lines# --- 3. 向 Gemini 定義工具 (Tool Declarations) ---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": {}}}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 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.GenerateContent(model_name="gemini-1.5-flash",tools=[earthquake_search_tool_declaration, pws_search_tool_declaration],system_instruction=system_instruction26 unmodified linesexcept Exception as e:print(f"與 Gemini AI 互動時發生錯誤: {e}")return f"🤖 AI 服務發生錯誤: {e}"
ai_service.py+12-15