fix webui: switch to created chat session automatically

This commit is contained in:
liunux4odoo 2023-08-12 22:58:13 +08:00
parent 1535059d2c
commit b59b34eaf7
2 changed files with 14 additions and 9 deletions

View File

@ -12,7 +12,7 @@ from webui_pages import *
api = ApiRequest(base_url="http://127.0.0.1:7861", no_remote_api=False) api = ApiRequest(base_url="http://127.0.0.1:7861", no_remote_api=False)
if __name__ == "__main__": if __name__ == "__main__":
st.set_page_config("langchain-chatglm WebUI") st.set_page_config("langchain-chatglm WebUI", initial_sidebar_state="expanded")
if not chat_box.chat_inited: if not chat_box.chat_inited:
st.toast( st.toast(
@ -63,7 +63,10 @@ if __name__ == "__main__":
st.session_state["cur_chat_name"] = new_chat_name st.session_state["cur_chat_name"] = new_chat_name
st.session_state[key] = new_chat_name st.session_state[key] = new_chat_name
elif st.session_state[key] not in ["新建对话", "知识库管理"]: elif st.session_state[key] not in ["新建对话", "知识库管理"]:
st.session_state["cur_chat_name"] = st.session_state[key] if st.session_state.get("prompt"):
st.session_state["cur_chat_name"] = st.session_state.get("prompt")
else:
st.session_state["cur_chat_name"] = st.session_state[key]
with st.sidebar: with st.sidebar:
selected_page = option_menu( selected_page = option_menu(
@ -75,4 +78,5 @@ if __name__ == "__main__":
on_change=on_page_change, on_change=on_page_change,
) )
pages[selected_page]["func"](api) if selected_page == "知识库管理" or selected_page in pages:
pages[selected_page]["func"](api)

View File

@ -82,15 +82,16 @@ def dialogue_page(api: ApiRequest):
chat_box.output_messages() chat_box.output_messages()
if (st.session_state.cur_chat_name == "新建对话" if st.session_state.chat_list.get(st.session_state.cur_chat_name, {}).get("need_rename"):
or st.session_state.chat_list.get(st.session_state.cur_chat_name, {}).get("need_rename")):
chat_input_placeholder = "请输入对话名称" chat_input_placeholder = "请输入对话名称"
else: else:
chat_input_placeholder = "请输入对话内容换行请使用Ctrl+Enter " chat_input_placeholder = "请输入对话内容换行请使用Ctrl+Enter "
if prompt := st.chat_input(chat_input_placeholder, key="prompt"): def on_prompt():
if (st.session_state.cur_chat_name == "新建对话" st.session_state["selected_page"] = prompt
or st.session_state.chat_list.get(st.session_state.cur_chat_name, {}).get("need_rename")):
if prompt := st.chat_input(chat_input_placeholder, key="prompt", on_submit=on_prompt):
if st.session_state.chat_list.get(st.session_state.cur_chat_name, {}).get("need_rename"):
if prompt in st.session_state.chat_list.keys(): if prompt in st.session_state.chat_list.keys():
st.toast("已有同名对话,请重新命名") st.toast("已有同名对话,请重新命名")
else: else: