From e1698ce12e3b7ca9831e6843a8b9a8dbe96b90c0 Mon Sep 17 00:00:00 2001 From: liunux4odoo Date: Thu, 3 Aug 2023 15:06:10 +0800 Subject: [PATCH] =?UTF-8?q?webui=E4=B8=AD=E5=A2=9E=E5=8A=A0=E5=A4=9A?= =?UTF-8?q?=E4=BC=9A=E8=AF=9D=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webui.py | 4 +- webui_pages/dialogue/dialogue.py | 67 +++++++++++++++++++++++--------- 2 files changed, 50 insertions(+), 21 deletions(-) diff --git a/webui.py b/webui.py index fe58240..3b89998 100644 --- a/webui.py +++ b/webui.py @@ -1,5 +1,5 @@ # 运行方式: -# 1. 安装必要的包:pip install streamlit-option-menu streamlit-chatbox>=1.1.3 +# 1. 安装必要的包:pip install streamlit-option-menu streamlit-chatbox>=1.1.4 # 2. 运行本机fastchat服务:python server\llm_api.py 或者 运行对应的sh文件 # 3. 运行API服务器:python server/api.py。如果使用api = ApiRequest(no_remote_api=True),该步可以跳过。 # 4. 运行WEB UI:streamlit run webui.py --server.port 7860 @@ -12,7 +12,7 @@ from webui_pages import * api = ApiRequest() if __name__ == "__main__": - st.set_page_config("langchain-chatglm WebUI") + st.set_page_config("langchain-chatglm WebUI", layout="wide") pages = {"对话": {"icon": "chat", "func": dialogue_page, diff --git a/webui_pages/dialogue/dialogue.py b/webui_pages/dialogue/dialogue.py index 5f7faf9..06e37c7 100644 --- a/webui_pages/dialogue/dialogue.py +++ b/webui_pages/dialogue/dialogue.py @@ -3,13 +3,13 @@ from webui_pages.utils import * from streamlit_chatbox import * -def dialogue_page(api: ApiRequest): - chat_box = ChatBox( - greetings=[ - f"欢迎使用 [`Langchain-Chatglm`](https://github.com/chatchat-space/langchain-chatglm) ! 当前使用模型`{LLM_MODEL}`, 您可以开始提问了.", - ] - ) +chat_box = ChatBox( + greetings=[ + f"欢迎使用 [`Langchain-Chatglm`](https://github.com/chatchat-space/langchain-chatglm) ! 当前使用模型`{LLM_MODEL}`, 您可以开始提问了.", + ] +) +def dialogue_page(api: ApiRequest): with st.sidebar: def on_mode_change(): mode = st.session_state.dialogue_mode @@ -31,24 +31,53 @@ def dialogue_page(api: ApiRequest): ) history_len = st.slider("历史对话轮数:", 1, 10, 1, disabled=True) # todo: support history len - if st.button("清除历史对话"): - chat_box.reset_history() + with st.expander("会话管理", True): + if st.button("清除历史对话内容"): + chat_box.reset_history() + + col_input, col_btn = st.columns(2) + new_chat_name = col_input.text_input( + "新会话名称", + placeholder="新会话名称", + label_visibility="collapsed", + key="new_chat_name", + ) + + def on_btn_new_chat(): + new_chat_name = st.session_state.new_chat_name + if new_chat_name: + chat_box.use_chat_name(new_chat_name) + st.session_state.new_chat_name = "" + col_btn.button("新建会话", on_click=on_btn_new_chat) + + cols = st.columns(2) + chat_list = chat_box.get_chat_names() + print(chat_list, chat_box.cur_chat_name) + try: + index = chat_list.index(chat_box.cur_chat_name) + except: + index = 0 + cur_chat_name = cols[0].selectbox("当前会话:", chat_list, index, label_visibility="collapsed") + chat_box.use_chat_name(cur_chat_name) + if cols[1].button("清除会话"): + chat_box.del_chat_name(cur_chat_name) def on_kb_change(): chat_box.ai_say(f"已加载知识库: {st.session_state.selected_kb}", not_render=True) if dialogue_mode == "知识库问答": - kb_list = api.list_knowledge_bases() - selected_kb = st.selectbox( - "请选择知识库:", - kb_list, - on_change=on_kb_change, - key="selected_kb", - ) - top_k = st.slider("匹配知识条数:", 1, 20, 3, disabled=True) - score_threshold = st.slider("知识匹配分数阈值:", 0, 1000, 0, disabled=True) - chunk_content = st.checkbox("关联上下文", False, disabled=True) - chunk_size = st.slider("关联长度:", 0, 500, 250, disabled=True) + with st.expander("知识库配置", True): + kb_list = api.list_knowledge_bases() + selected_kb = st.selectbox( + "请选择知识库:", + kb_list, + on_change=on_kb_change, + key="selected_kb", + ) + top_k = st.slider("匹配知识条数:", 1, 20, 3, disabled=True) + score_threshold = st.slider("知识匹配分数阈值:", 0, 1000, 0, disabled=True) + chunk_content = st.checkbox("关联上下文", False, disabled=True) + chunk_size = st.slider("关联长度:", 0, 500, 250, disabled=True) # Display chat messages from history on app rerun chat_box.output_messages()