2023-08-03 13:41:31 +08:00
|
|
|
|
# 运行方式:
|
2023-08-04 12:52:58 +08:00
|
|
|
|
# 1. 安装必要的包:pip install streamlit-option-menu streamlit-chatbox>=1.1.6
|
2023-08-03 13:41:31 +08:00
|
|
|
|
# 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
|
|
|
|
|
|
|
2023-07-27 23:22:07 +08:00
|
|
|
|
import streamlit as st
|
2023-08-01 14:47:38 +08:00
|
|
|
|
from webui_pages.utils import *
|
2023-07-28 06:58:34 +08:00
|
|
|
|
from streamlit_option_menu import option_menu
|
2023-08-01 14:47:38 +08:00
|
|
|
|
from webui_pages import *
|
2023-08-01 14:18:30 +08:00
|
|
|
|
|
2023-08-04 12:52:58 +08:00
|
|
|
|
api = ApiRequest(base_url="http://127.0.0.1:7861", no_remote_api=False)
|
2023-05-18 22:54:41 +08:00
|
|
|
|
|
2023-07-28 06:58:34 +08:00
|
|
|
|
if __name__ == "__main__":
|
2023-08-12 22:58:13 +08:00
|
|
|
|
st.set_page_config("langchain-chatglm WebUI", initial_sidebar_state="expanded")
|
2023-07-28 06:58:34 +08:00
|
|
|
|
|
2023-08-04 12:52:58 +08:00
|
|
|
|
if not chat_box.chat_inited:
|
2023-08-10 22:32:56 +08:00
|
|
|
|
st.toast(
|
|
|
|
|
|
f"欢迎使用 [`Langchain-Chatglm`](https://github.com/chatchat-space/langchain-chatglm) ! \n\n"
|
|
|
|
|
|
f"当前使用模型`{LLM_MODEL}`, 您可以开始提问了."
|
|
|
|
|
|
)
|
2023-08-04 12:52:58 +08:00
|
|
|
|
|
2023-08-12 02:30:50 +08:00
|
|
|
|
if "chat_list" not in st.session_state:
|
2023-08-13 14:58:14 +08:00
|
|
|
|
st.session_state["chat_list"] = {"对话1": {"need_rename": True, "chat_no": 1}}
|
2023-08-12 02:30:50 +08:00
|
|
|
|
if "cur_chat_name" not in st.session_state:
|
2023-08-12 14:08:21 +08:00
|
|
|
|
st.session_state["cur_chat_name"] = list(st.session_state["chat_list"].keys())[0]
|
2023-08-12 02:30:50 +08:00
|
|
|
|
if "need_chat_name" not in st.session_state:
|
|
|
|
|
|
st.session_state["need_chat_name"] = True
|
2023-08-13 14:58:14 +08:00
|
|
|
|
if "chat_count" not in st.session_state:
|
|
|
|
|
|
st.session_state["chat_count"] = 1
|
2023-08-12 02:30:50 +08:00
|
|
|
|
|
2023-08-13 13:13:30 +08:00
|
|
|
|
chat_list = [{"name": k, "chat_no": v.get("chat_no", 0)} for k, v in st.session_state.chat_list.items()]
|
2023-08-13 14:58:14 +08:00
|
|
|
|
chat_list = [x["name"] for x in sorted(chat_list, key=lambda x: x["chat_no"])]
|
|
|
|
|
|
pages1 = {i: {
|
2023-08-12 14:08:21 +08:00
|
|
|
|
"icon": "chat",
|
|
|
|
|
|
"func": dialogue_page,
|
2023-08-13 14:58:14 +08:00
|
|
|
|
} for i in chat_list}
|
2023-08-12 14:08:21 +08:00
|
|
|
|
|
2023-08-12 02:30:50 +08:00
|
|
|
|
pages2 = {
|
2023-08-12 14:08:21 +08:00
|
|
|
|
"新建对话": {
|
|
|
|
|
|
"icon": "plus-circle",
|
|
|
|
|
|
"func": dialogue_page,
|
|
|
|
|
|
},
|
|
|
|
|
|
"---": {
|
|
|
|
|
|
"icon": None,
|
|
|
|
|
|
"func": None
|
|
|
|
|
|
},
|
|
|
|
|
|
"知识库管理": {
|
|
|
|
|
|
"icon": "hdd-stack",
|
|
|
|
|
|
"func": knowledge_base_page,
|
|
|
|
|
|
},
|
2023-08-12 02:30:50 +08:00
|
|
|
|
}
|
2023-08-13 14:58:14 +08:00
|
|
|
|
pages = {**pages1, **pages2}
|
2023-08-12 22:58:13 +08:00
|
|
|
|
|
2023-07-28 06:58:34 +08:00
|
|
|
|
with st.sidebar:
|
2023-08-13 14:58:14 +08:00
|
|
|
|
options = chat_list + list(pages2)
|
|
|
|
|
|
icons = ["chat"] * len(chat_list) + [x["icon"] for x in pages2.values()]
|
|
|
|
|
|
|
2023-08-13 12:59:06 +08:00
|
|
|
|
default_index = list(pages).index(st.session_state["cur_chat_name"])
|
2023-08-12 15:02:14 +08:00
|
|
|
|
selected_page = option_menu(
|
|
|
|
|
|
"langchain-chatglm",
|
2023-08-13 14:58:14 +08:00
|
|
|
|
options=options,
|
|
|
|
|
|
icons=icons,
|
2023-08-12 15:02:14 +08:00
|
|
|
|
menu_icon="chat-quote",
|
2023-08-13 12:59:06 +08:00
|
|
|
|
default_index=default_index,
|
2023-08-12 15:02:14 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2023-08-13 12:59:06 +08:00
|
|
|
|
if selected_page == "新建对话":
|
2023-08-13 14:58:14 +08:00
|
|
|
|
cur_chat_name = st.session_state.get("cur_chat_name")
|
2023-08-13 12:59:06 +08:00
|
|
|
|
if (not st.session_state.get("create_chat")
|
|
|
|
|
|
and not st.session_state.get("renamde_chat")
|
|
|
|
|
|
and not st.session_state.get("delete_chat")):
|
2023-08-13 14:58:14 +08:00
|
|
|
|
st.session_state.chat_count += 1
|
|
|
|
|
|
chat_no = st.session_state.chat_count
|
2023-08-13 13:13:30 +08:00
|
|
|
|
new_chat_name = f"对话{chat_no}"
|
|
|
|
|
|
st.session_state.chat_list[new_chat_name] = {"need_rename": True, "chat_no": chat_no}
|
2023-08-13 12:59:06 +08:00
|
|
|
|
st.session_state["cur_chat_name"] = new_chat_name
|
|
|
|
|
|
st.experimental_rerun()
|
2023-08-13 14:58:14 +08:00
|
|
|
|
else:
|
|
|
|
|
|
if st.session_state.get("create_chat"):
|
|
|
|
|
|
st.session_state.create_chat = False
|
|
|
|
|
|
if st.session_state.get("renamde_chat"):
|
|
|
|
|
|
st.session_state.renamde_chat = False
|
|
|
|
|
|
st.experimental_rerun()
|
|
|
|
|
|
if st.session_state.get("delete_chat"):
|
|
|
|
|
|
st.session_state.delete_chat = False
|
|
|
|
|
|
st.experimental_rerun()
|
2023-08-13 12:59:06 +08:00
|
|
|
|
elif selected_page in st.session_state.chat_list:
|
2023-08-13 14:58:14 +08:00
|
|
|
|
st.session_state["selected_page"] = selected_page
|
2023-08-13 12:59:06 +08:00
|
|
|
|
|
|
|
|
|
|
if selected_page in pages:
|
2023-08-12 22:58:13 +08:00
|
|
|
|
pages[selected_page]["func"](api)
|