Update app.py
Browse files190 unmodified lines1911921931941951961974 unmodified lines2022032042052062072082092102112122139 unmodified lines2232242252262272282292 unmodified lines2322332342352362372382397 unmodified lines247248249250251252253254255256257258259260261262263264265266267268269270271190 unmodified linesreturn "\n\n".join(relevant)return "未找到相關信息。"def chat(self, message):"""完整的問答流程"""if not message.strip():return "請輸入您的問題。"4 unmodified linescontext = self._retrieve_relevant_info(message)print(f"📚 檢索到 {len(context)} 字符的相關信息")# 2. 構建提示詞(不包含歷史記錄)prompt = f"""{self.system_prompt}相關大學信息:{context}用戶: {message}助手: """9 unmodified lineschatbot = UniversityChatBot()# ==================== Gradio 界面 ====================# 修正版本:使用正確的聊天歷史格式with gr.Blocks(title="🎓 大學信息智能助手", theme=gr.themes.Soft()) as demo:gr.Markdown(f"""# 🎓 大學信息智能助手 (開源模型版)2 unmodified lines**使用模型**:TinyLlama-1.1B-Chat""")# 使用 type="messages" 指定使用新的消息格式chatbot_ui = gr.Chatbot(label="對話歷史", height=500, type="messages")with gr.Row():msg = gr.Textbox(7 unmodified linesclear_btn = gr.Button("清除對話")def respond(message, chat_history):"""回應函數 - 使用 messages 格式"""if not message:return "", chat_history# 將用戶消息添加到歷史chat_history.append({"role": "user", "content": message})# 獲取回答answer = chatbot.chat(message)# 將助手回答添加到歷史chat_history.append({"role": "assistant", "content": answer})return "", chat_history# 綁定事件send_btn.click(respond, [msg, chatbot_ui], [msg, chatbot_ui])msg.submit(respond, [msg, chatbot_ui], [msg, chatbot_ui])clear_btn.click(lambda: [], None, chatbot_ui, queue=False)# ==================== 啟動應用 ====================if __name__ == "__main__":190 unmodified lines1911921931941951961974 unmodified lines2022032042052062072082092102112122132142152162172182192209 unmodified lines2302312322332342352362 unmodified lines2392402412422432442452467 unmodified lines254255256257258259260261262263264265266267268269270271272273274190 unmodified linesreturn "\n\n".join(relevant)return "未找到相關信息。"def chat(self, message, history=None):"""完整的問答流程"""if not message.strip():return "請輸入您的問題。"4 unmodified linescontext = self._retrieve_relevant_info(message)print(f"📚 檢索到 {len(context)} 字符的相關信息")# 2. 構建提示詞history_text = ""if history:# 使用傳統的 tuple 格式for user_msg, assistant_msg in history[-3:]:history_text += f"用戶: {user_msg}\n助手: {assistant_msg}\n"prompt = f"""{self.system_prompt}相關大學信息:{context}{history_text}用戶: {message}助手: """9 unmodified lineschatbot = UniversityChatBot()# ==================== Gradio 界面 ====================# 兼容舊版 Gradio 的版本with gr.Blocks(title="🎓 大學信息智能助手", theme=gr.themes.Soft()) as demo:gr.Markdown(f"""# 🎓 大學信息智能助手 (開源模型版)2 unmodified lines**使用模型**:TinyLlama-1.1B-Chat""")# 移除 type 參數,使用傳統格式chatbot_ui = gr.Chatbot(label="對話歷史", height=500)with gr.Row():msg = gr.Textbox(7 unmodified linesclear_btn = gr.Button("清除對話")def respond(message, chat_history):"""回應函數 - 使用傳統 tuple 格式"""if not message:return "", chat_history# 獲取回答answer = chatbot.chat(message, chat_history)# 使用傳統的 tuple 格式chat_history.append((message, answer))return "", chat_history# 綁定事件send_btn.click(respond, [msg, chatbot_ui], [msg, chatbot_ui])msg.submit(respond, [msg, chatbot_ui], [msg, chatbot_ui])clear_btn.click(lambda: None, None, chatbot_ui, queue=False)# ==================== 啟動應用 ====================if __name__ == "__main__":
app.py+17-14