Spaces:
Sleeping
Sleeping
Update core/callbacks.py
Browse files- core/callbacks.py +8 -12
core/callbacks.py
CHANGED
|
@@ -53,20 +53,12 @@ def execute_user_code(code_string, source_lab):
|
|
| 53 |
notification_text += f"\n錯誤類型: {error_type}"
|
| 54 |
send_line_notification_in_background(notification_text)
|
| 55 |
|
| 56 |
-
# --- ✨ 重新排序此列表,使其與 UI 上的編號 (#1, #2...) 對應 ---
|
| 57 |
-
# 這個列表同時用於快捷指令和關鍵字查詢
|
| 58 |
LIVE_TOOLS = [
|
| 59 |
-
# #1
|
| 60 |
{"keywords": ["新聞", "今日新聞", "news"], "function": news_service.fetch_today_news, "name": "今日新聞"},
|
| 61 |
-
# #2
|
| 62 |
{"keywords": ["cwa地震", "顯著地震", "有感地震"], "function": cwa_service.fetch_significant_earthquakes, "name": "CWA 顯著有感地震"},
|
| 63 |
-
# #3
|
| 64 |
{"keywords": ["地震預警", "cwa alarm", "eew", "現在有地震預警嗎?"], "function": cwa_service.fetch_cwa_alarm_list, "name": "CWA 地震預警"},
|
| 65 |
-
# #4
|
| 66 |
{"keywords": ["全球地震", "usgs", "最近全球有哪些大地震"], "function": usgs_service.fetch_global_last24h_text, "name": "全球顯著地震"},
|
| 67 |
-
# #5
|
| 68 |
{"keywords": ["pws發布", "pws info"], "function": pws_service.fetch_latest_pws_info, "name": "PWS 發布情形"},
|
| 69 |
-
# #6
|
| 70 |
{"keywords": ["pws地震", "pws alert", "pws", "最新的 pws 地震警報"], "function": pws_service.fetch_cwa_pws_earthquake_info, "name": "PWS 地震警報"},
|
| 71 |
]
|
| 72 |
|
|
@@ -90,7 +82,7 @@ def find_best_match_from_kb(user_input, knowledge_base, threshold=0.6):
|
|
| 90 |
else:
|
| 91 |
return best_answer
|
| 92 |
|
| 93 |
-
# --- ✨
|
| 94 |
def ai_chatbot_with_kb(message, history):
|
| 95 |
"""
|
| 96 |
處理聊天機器人互動的主函式。
|
|
@@ -110,17 +102,21 @@ def ai_chatbot_with_kb(message, history):
|
|
| 110 |
|
| 111 |
user_message = message.strip()
|
| 112 |
|
| 113 |
-
# 步驟 1: 檢查是否為快捷指令
|
| 114 |
if user_message.startswith('#'):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
try:
|
| 116 |
-
# 取得 # 後面的數字,並轉換成索引 (數字1對應索引0)
|
| 117 |
tool_index = int(user_message[1:]) - 1
|
| 118 |
if 0 <= tool_index < len(LIVE_TOOLS):
|
| 119 |
tool = LIVE_TOOLS[tool_index]
|
| 120 |
print(f"🚀 觸發快捷指令:{tool['name']}")
|
| 121 |
return tool["function"]()
|
| 122 |
else:
|
| 123 |
-
return f"⚠️ 無效的快捷指令!請輸入 #1 到 #{len(LIVE_TOOLS)}
|
| 124 |
except (ValueError, IndexError):
|
| 125 |
return "⚠️ 快捷指令格式錯誤!請輸入像 `#1` 這樣的格式。"
|
| 126 |
|
|
|
|
| 53 |
notification_text += f"\n錯誤類型: {error_type}"
|
| 54 |
send_line_notification_in_background(notification_text)
|
| 55 |
|
|
|
|
|
|
|
| 56 |
LIVE_TOOLS = [
|
|
|
|
| 57 |
{"keywords": ["新聞", "今日新聞", "news"], "function": news_service.fetch_today_news, "name": "今日新聞"},
|
|
|
|
| 58 |
{"keywords": ["cwa地震", "顯著地震", "有感地震"], "function": cwa_service.fetch_significant_earthquakes, "name": "CWA 顯著有感地震"},
|
|
|
|
| 59 |
{"keywords": ["地震預警", "cwa alarm", "eew", "現在有地震預警嗎?"], "function": cwa_service.fetch_cwa_alarm_list, "name": "CWA 地震預警"},
|
|
|
|
| 60 |
{"keywords": ["全球地震", "usgs", "最近全球有哪些大地震"], "function": usgs_service.fetch_global_last24h_text, "name": "全球顯著地震"},
|
|
|
|
| 61 |
{"keywords": ["pws發布", "pws info"], "function": pws_service.fetch_latest_pws_info, "name": "PWS 發布情形"},
|
|
|
|
| 62 |
{"keywords": ["pws地震", "pws alert", "pws", "最新的 pws 地震警報"], "function": pws_service.fetch_cwa_pws_earthquake_info, "name": "PWS 地震警報"},
|
| 63 |
]
|
| 64 |
|
|
|
|
| 82 |
else:
|
| 83 |
return best_answer
|
| 84 |
|
| 85 |
+
# --- ✨ 以下是修改後的核心函式 ---
|
| 86 |
def ai_chatbot_with_kb(message, history):
|
| 87 |
"""
|
| 88 |
處理聊天機器人互動的主函式。
|
|
|
|
| 102 |
|
| 103 |
user_message = message.strip()
|
| 104 |
|
| 105 |
+
# 步驟 1: 檢查是否為快捷指令
|
| 106 |
if user_message.startswith('#'):
|
| 107 |
+
# ✨ 新增 elif 區塊來處理 #7
|
| 108 |
+
if user_message == '#7':
|
| 109 |
+
print("💡 觸發進階查詢說明 (#7)")
|
| 110 |
+
return "好的,請依照以下格式提供查詢的日期範圍與規模:\n`查詢 YYYY-MM-DD 到 YYYY-MM-DD 規模 X.X 以上地震`\n\n例如:`查詢 2024-04-01 到 2024-04-07 規模 6.0 以上地震`"
|
| 111 |
+
|
| 112 |
try:
|
|
|
|
| 113 |
tool_index = int(user_message[1:]) - 1
|
| 114 |
if 0 <= tool_index < len(LIVE_TOOLS):
|
| 115 |
tool = LIVE_TOOLS[tool_index]
|
| 116 |
print(f"🚀 觸發快捷指令:{tool['name']}")
|
| 117 |
return tool["function"]()
|
| 118 |
else:
|
| 119 |
+
return f"⚠️ 無效的快捷指令!請輸入 #1 到 #{len(LIVE_TOOLS)} 之間的數字,或 #7 查看進階查詢說明。"
|
| 120 |
except (ValueError, IndexError):
|
| 121 |
return "⚠️ 快捷指令格式錯誤!請輸入像 `#1` 這樣的格式。"
|
| 122 |
|