Update app.py
Browse files
app.py
CHANGED
|
@@ -191,7 +191,7 @@ class UniversityChatBot:
|
|
| 191 |
return "\n\n".join(relevant)
|
| 192 |
return "未找到相關信息。"
|
| 193 |
|
| 194 |
-
def chat(self, message):
|
| 195 |
"""完整的問答流程"""
|
| 196 |
if not message.strip():
|
| 197 |
return "請輸入您的問題。"
|
|
@@ -202,12 +202,19 @@ class UniversityChatBot:
|
|
| 202 |
context = self._retrieve_relevant_info(message)
|
| 203 |
print(f"📚 檢索到 {len(context)} 字符的相關信息")
|
| 204 |
|
| 205 |
-
# 2. 構建提示詞
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
prompt = f"""{self.system_prompt}
|
| 207 |
|
| 208 |
相關大學信息:
|
| 209 |
{context}
|
| 210 |
|
|
|
|
| 211 |
用戶: {message}
|
| 212 |
助手: """
|
| 213 |
|
|
@@ -223,7 +230,7 @@ class UniversityChatBot:
|
|
| 223 |
chatbot = UniversityChatBot()
|
| 224 |
|
| 225 |
# ==================== Gradio 界面 ====================
|
| 226 |
-
#
|
| 227 |
with gr.Blocks(title="🎓 大學信息智能助手", theme=gr.themes.Soft()) as demo:
|
| 228 |
gr.Markdown(f"""
|
| 229 |
# 🎓 大學信息智能助手 (開源模型版)
|
|
@@ -232,8 +239,8 @@ with gr.Blocks(title="🎓 大學信息智能助手", theme=gr.themes.Soft()) as
|
|
| 232 |
**使用模型**:TinyLlama-1.1B-Chat
|
| 233 |
""")
|
| 234 |
|
| 235 |
-
#
|
| 236 |
-
chatbot_ui = gr.Chatbot(label="對話歷史", height=500
|
| 237 |
|
| 238 |
with gr.Row():
|
| 239 |
msg = gr.Textbox(
|
|
@@ -247,25 +254,21 @@ with gr.Blocks(title="🎓 大學信息智能助手", theme=gr.themes.Soft()) as
|
|
| 247 |
clear_btn = gr.Button("清除對話")
|
| 248 |
|
| 249 |
def respond(message, chat_history):
|
| 250 |
-
"""回應函數 - 使用
|
| 251 |
if not message:
|
| 252 |
return "", chat_history
|
| 253 |
|
| 254 |
-
# 將用戶消息添加到歷史
|
| 255 |
-
chat_history.append({"role": "user", "content": message})
|
| 256 |
-
|
| 257 |
# 獲取回答
|
| 258 |
-
answer = chatbot.chat(message)
|
| 259 |
-
|
| 260 |
-
# 將助手回答添加到歷史
|
| 261 |
-
chat_history.append({"role": "assistant", "content": answer})
|
| 262 |
|
|
|
|
|
|
|
| 263 |
return "", chat_history
|
| 264 |
|
| 265 |
# 綁定事件
|
| 266 |
send_btn.click(respond, [msg, chatbot_ui], [msg, chatbot_ui])
|
| 267 |
msg.submit(respond, [msg, chatbot_ui], [msg, chatbot_ui])
|
| 268 |
-
clear_btn.click(lambda:
|
| 269 |
|
| 270 |
# ==================== 啟動應用 ====================
|
| 271 |
if __name__ == "__main__":
|
|
|
|
| 191 |
return "\n\n".join(relevant)
|
| 192 |
return "未找到相關信息。"
|
| 193 |
|
| 194 |
+
def chat(self, message, history=None):
|
| 195 |
"""完整的問答流程"""
|
| 196 |
if not message.strip():
|
| 197 |
return "請輸入您的問題。"
|
|
|
|
| 202 |
context = self._retrieve_relevant_info(message)
|
| 203 |
print(f"📚 檢索到 {len(context)} 字符的相關信息")
|
| 204 |
|
| 205 |
+
# 2. 構建提示詞
|
| 206 |
+
history_text = ""
|
| 207 |
+
if history:
|
| 208 |
+
# 使用傳統的 tuple 格式
|
| 209 |
+
for user_msg, assistant_msg in history[-3:]:
|
| 210 |
+
history_text += f"用戶: {user_msg}\n助手: {assistant_msg}\n"
|
| 211 |
+
|
| 212 |
prompt = f"""{self.system_prompt}
|
| 213 |
|
| 214 |
相關大學信息:
|
| 215 |
{context}
|
| 216 |
|
| 217 |
+
{history_text}
|
| 218 |
用戶: {message}
|
| 219 |
助手: """
|
| 220 |
|
|
|
|
| 230 |
chatbot = UniversityChatBot()
|
| 231 |
|
| 232 |
# ==================== Gradio 界面 ====================
|
| 233 |
+
# 兼容舊版 Gradio 的版本
|
| 234 |
with gr.Blocks(title="🎓 大學信息智能助手", theme=gr.themes.Soft()) as demo:
|
| 235 |
gr.Markdown(f"""
|
| 236 |
# 🎓 大學信息智能助手 (開源模型版)
|
|
|
|
| 239 |
**使用模型**:TinyLlama-1.1B-Chat
|
| 240 |
""")
|
| 241 |
|
| 242 |
+
# 移除 type 參數,使用傳統格式
|
| 243 |
+
chatbot_ui = gr.Chatbot(label="對話歷史", height=500)
|
| 244 |
|
| 245 |
with gr.Row():
|
| 246 |
msg = gr.Textbox(
|
|
|
|
| 254 |
clear_btn = gr.Button("清除對話")
|
| 255 |
|
| 256 |
def respond(message, chat_history):
|
| 257 |
+
"""回應函數 - 使用傳統 tuple 格式"""
|
| 258 |
if not message:
|
| 259 |
return "", chat_history
|
| 260 |
|
|
|
|
|
|
|
|
|
|
| 261 |
# 獲取回答
|
| 262 |
+
answer = chatbot.chat(message, chat_history)
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
+
# 使用傳統的 tuple 格式
|
| 265 |
+
chat_history.append((message, answer))
|
| 266 |
return "", chat_history
|
| 267 |
|
| 268 |
# 綁定事件
|
| 269 |
send_btn.click(respond, [msg, chatbot_ui], [msg, chatbot_ui])
|
| 270 |
msg.submit(respond, [msg, chatbot_ui], [msg, chatbot_ui])
|
| 271 |
+
clear_btn.click(lambda: None, None, chatbot_ui, queue=False)
|
| 272 |
|
| 273 |
# ==================== 啟動應用 ====================
|
| 274 |
if __name__ == "__main__":
|