Spaces:
Sleeping
Sleeping
Update core/callbacks.py
Browse files- core/callbacks.py +29 -10
core/callbacks.py
CHANGED
|
@@ -5,7 +5,7 @@ import traceback
|
|
| 5 |
from datetime import datetime
|
| 6 |
import pytz
|
| 7 |
import difflib
|
| 8 |
-
import re
|
| 9 |
|
| 10 |
from services import cwa_service, news_service, pws_service, usgs_service
|
| 11 |
from core.visits import get_current_visit_count
|
|
@@ -53,13 +53,20 @@ 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 |
LIVE_TOOLS = [
|
|
|
|
| 57 |
{"keywords": ["新聞", "今日新聞", "news"], "function": news_service.fetch_today_news, "name": "今日新聞"},
|
|
|
|
| 58 |
{"keywords": ["cwa地震", "顯著地震", "有感地震"], "function": cwa_service.fetch_significant_earthquakes, "name": "CWA 顯著有感地震"},
|
| 59 |
-
#
|
| 60 |
{"keywords": ["地震預警", "cwa alarm", "eew", "現在有地震預警嗎?"], "function": cwa_service.fetch_cwa_alarm_list, "name": "CWA 地震預警"},
|
|
|
|
| 61 |
{"keywords": ["全球地震", "usgs", "最近全球有哪些大地震"], "function": usgs_service.fetch_global_last24h_text, "name": "全球顯著地震"},
|
|
|
|
| 62 |
{"keywords": ["pws發布", "pws info"], "function": pws_service.fetch_latest_pws_info, "name": "PWS 發布情形"},
|
|
|
|
| 63 |
{"keywords": ["pws地震", "pws alert", "pws", "最新的 pws 地震警報"], "function": pws_service.fetch_cwa_pws_earthquake_info, "name": "PWS 地震警報"},
|
| 64 |
]
|
| 65 |
|
|
@@ -83,11 +90,11 @@ def find_best_match_from_kb(user_input, knowledge_base, threshold=0.6):
|
|
| 83 |
else:
|
| 84 |
return best_answer
|
| 85 |
|
| 86 |
-
# --- ✨
|
| 87 |
def ai_chatbot_with_kb(message, history):
|
| 88 |
"""
|
| 89 |
處理聊天機器人互動的主函式。
|
| 90 |
-
|
| 91 |
"""
|
| 92 |
# (通知邏輯維持不變)
|
| 93 |
tz = pytz.timezone('Asia/Taipei')
|
|
@@ -101,10 +108,23 @@ def ai_chatbot_with_kb(message, history):
|
|
| 101 |
)
|
| 102 |
send_line_notification_in_background(notification_text)
|
| 103 |
|
| 104 |
-
user_message = message.strip()
|
| 105 |
|
| 106 |
-
# 步驟 1:
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
pattern = r"查詢\s*(\d{4}-\d{2}-\d{2})\s*到\s*(\d{4}-\d{2}-\d{2})\s*規模\s*(\d+(?:\.\d+)?)\s*以上地震"
|
| 109 |
match = re.search(pattern, user_message)
|
| 110 |
|
|
@@ -112,12 +132,11 @@ def ai_chatbot_with_kb(message, history):
|
|
| 112 |
start_date, end_date, magnitude = match.groups()
|
| 113 |
print(f"🔍 觸發進階地震查詢:{start_date} 到 {end_date}, M≥{magnitude}")
|
| 114 |
try:
|
| 115 |
-
# 呼叫 usgs_service 中的新函式
|
| 116 |
return usgs_service.fetch_usgs_earthquakes_by_date(start_date, end_date, float(magnitude))
|
| 117 |
except Exception as e:
|
| 118 |
return f"❌ 執行進階查詢時發生錯誤:{e}"
|
| 119 |
|
| 120 |
-
# 步驟
|
| 121 |
user_message_lower = user_message.lower()
|
| 122 |
for tool in LIVE_TOOLS:
|
| 123 |
for keyword in tool["keywords"]:
|
|
@@ -128,5 +147,5 @@ def ai_chatbot_with_kb(message, history):
|
|
| 128 |
except Exception as e:
|
| 129 |
return f"❌ 執行「{tool['name']}」工具時發生錯誤:{e}"
|
| 130 |
|
| 131 |
-
# 步驟
|
| 132 |
return find_best_match_from_kb(user_message_lower, KNOWLEDGE_BASE)
|
|
|
|
| 5 |
from datetime import datetime
|
| 6 |
import pytz
|
| 7 |
import difflib
|
| 8 |
+
import re
|
| 9 |
|
| 10 |
from services import cwa_service, news_service, pws_service, usgs_service
|
| 11 |
from core.visits import get_current_visit_count
|
|
|
|
| 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 |
else:
|
| 91 |
return best_answer
|
| 92 |
|
| 93 |
+
# --- ✨ 這是修改後的核心函式 ---
|
| 94 |
def ai_chatbot_with_kb(message, history):
|
| 95 |
"""
|
| 96 |
處理聊天機器人互動的主函式。
|
| 97 |
+
優先處理快捷指令,其次是進階查詢,再其次是關鍵字工具,最後查詢靜態知識庫。
|
| 98 |
"""
|
| 99 |
# (通知邏輯維持不變)
|
| 100 |
tz = pytz.timezone('Asia/Taipei')
|
|
|
|
| 108 |
)
|
| 109 |
send_line_notification_in_background(notification_text)
|
| 110 |
|
| 111 |
+
user_message = message.strip()
|
| 112 |
|
| 113 |
+
# 步驟 1: 檢查是否為快捷指令 (例如 #1, #2)
|
| 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 |
+
|
| 127 |
+
# 步驟 2: 檢查是否為進階地震查詢格式
|
| 128 |
pattern = r"查詢\s*(\d{4}-\d{2}-\d{2})\s*到\s*(\d{4}-\d{2}-\d{2})\s*規模\s*(\d+(?:\.\d+)?)\s*以上地震"
|
| 129 |
match = re.search(pattern, user_message)
|
| 130 |
|
|
|
|
| 132 |
start_date, end_date, magnitude = match.groups()
|
| 133 |
print(f"🔍 觸發進階地震查詢:{start_date} 到 {end_date}, M≥{magnitude}")
|
| 134 |
try:
|
|
|
|
| 135 |
return usgs_service.fetch_usgs_earthquakes_by_date(start_date, end_date, float(magnitude))
|
| 136 |
except Exception as e:
|
| 137 |
return f"❌ 執行進階查詢時發生錯誤:{e}"
|
| 138 |
|
| 139 |
+
# 步驟 3: 如果都不是,則檢查是否觸發即時工具 (關鍵字比對)
|
| 140 |
user_message_lower = user_message.lower()
|
| 141 |
for tool in LIVE_TOOLS:
|
| 142 |
for keyword in tool["keywords"]:
|
|
|
|
| 147 |
except Exception as e:
|
| 148 |
return f"❌ 執行「{tool['name']}」工具時發生錯誤:{e}"
|
| 149 |
|
| 150 |
+
# 步驟 4: 如果都沒有觸發,則查詢靜態知識庫
|
| 151 |
return find_best_match_from_kb(user_message_lower, KNOWLEDGE_BASE)
|