Spaces:
Sleeping
Sleeping
chen commited on
Update command_handler.py
Browse files123438 unmodified lines4344454647484927 unmodified lines777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312416 unmodified lines141142143144# command_handler.py (Improved date keyword pre-processing for AI queries)import pandas as pdimport refrom datetime import datetime, timedelta38 unmodified linestext = ("🤖 關於我\n\n""我是一個多功能助理機器人,提供地震查詢與 AI 對話功能。\n\n""• 版本: 5.4 (Gemini Pre-processor Edition)\n""• 資料來源: CWA, USGS, Google Gemini\n""• 開發者: dayichen")27 unmodified linesexcept Exception as e: return [TextMessage(text=f"❌ 查詢最新地震失敗:{e}")]def preprocess_ai_prompt(prompt: str) -> str:"""[核心修正]強化預處理:偵測多種時間關鍵字,並將問題改寫為更明確的指令。"""today = datetime.now()year_match = re.search(r"(\d{4})年", prompt)# 建立時間關鍵字與對應日期的字典time_keywords = {"昨天": (today - timedelta(days=1)).strftime("%Y-%m-%d"),"今天": today.strftime("%Y-%m-%d"),"今年": str(today.year),"去年": str(today.year - 1),}# 如果找到 "YYYY年" 的格式,也加入字典if year_match:time_keywords[year_match.group(0)] = year_match.group(1)# 如果訊息中包含 "地震"if "地震" in prompt:for keyword, date_val in time_keywords.items():if keyword in prompt:# 判斷是查詢特定一天還是一整年if len(date_val) == 10: # YYYY-MM-DD 格式start_date = end_date = date_valdate_range_str = date_valelse: # YYYY 格式start_date = f"{date_val}-01-01"end_date = f"{date_val}-12-31"date_range_str = f"{date_val}年"# 根據是否包含"最大"來決定指令內容if "最大" in prompt:new_prompt = f"請幫我呼叫地震搜尋工具,找出從 {start_date} 到 {end_date} 之間,規模最大的地震是哪一個。"else:new_prompt = f"請幫我呼叫地震搜尋工具,查詢從 {start_date} 到 {end_date} 之間發生的所有地震。"print(f"--- AI Prompt Rewritten ---\nOriginal: {prompt}\nNew: {new_prompt}\n---------------------------")return new_promptreturn prompt # 如果沒有匹配,回傳原始問題def process_message(user_message_raw: str, request_base_url: str) -> list:user_message = (user_message_raw or "").strip()16 unmodified linesprocessed_prompt = preprocess_ai_prompt(arg)return [TextMessage(text=generate_ai_text(processed_prompt))]return [get_help_message()]123438 unmodified lines4344454647484927 unmodified lines777879808182838485868788899091929394959616 unmodified lines113114115116117118# command_handler.py (Silent on non-command messages)import pandas as pdimport refrom datetime import datetime, timedelta38 unmodified linestext = ("🤖 關於我\n\n""我是一個多功能助理機器人,提供地震查詢與 AI 對話功能。\n\n""• 版本: 5.5 (Silent Mode Edition)\n""• 資料來源: CWA, USGS, Google Gemini\n""• 開發者: dayichen")27 unmodified linesexcept Exception as e: return [TextMessage(text=f"❌ 查詢最新地震失敗:{e}")]def preprocess_ai_prompt(prompt: str) -> str:today = datetime.now()year_match = re.search(r"(\d{4})年", prompt)time_keywords = { "昨天": (today - timedelta(days=1)).strftime("%Y-%m-%d"), "今天": today.strftime("%Y-%m-%d"), "今年": str(today.year), "去年": str(today.year - 1), }if year_match: time_keywords[year_match.group(0)] = year_match.group(1)if "地震" in prompt:for keyword, date_val in time_keywords.items():if keyword in prompt:if len(date_val) == 10: start_date = end_date = date_valelse: start_date, end_date = f"{date_val}-01-01", f"{date_val}-12-31"if "最大" in prompt: new_prompt = f"請幫我呼叫地震搜尋工具,找出從 {start_date} 到 {end_date} 之間,規模最大的地震是哪一個。"else: new_prompt = f"請幫我呼叫地震搜尋工具,查詢從 {start_date} 到 {end_date} 之間發生的所有地震。"print(f"--- AI Prompt Rewritten ---\nOriginal: {prompt}\nNew: {new_prompt}\n---------------------------")return new_promptreturn promptdef process_message(user_message_raw: str, request_base_url: str) -> list:user_message = (user_message_raw or "").strip()16 unmodified linesprocessed_prompt = preprocess_ai_prompt(arg)return [TextMessage(text=generate_ai_text(processed_prompt))]# [核心修改]# 對於所有其他無法識別的訊息 (一般對話),回傳一個空列表,代表「不回應」。return []
command_handler.py+12-38