Langchain-Chatchat/webui.py

72 lines
2.4 KiB
Python
Raw Normal View History

2023-07-27 23:22:07 +08:00
import streamlit as st
2023-08-01 14:18:30 +08:00
from streamlit_chatbox import *
from webui_utils import *
2023-07-28 06:58:34 +08:00
from streamlit_option_menu import option_menu
2023-08-01 14:18:30 +08:00
api = ApiRequest()
2023-07-28 06:58:34 +08:00
def dialogue_page():
with st.sidebar:
dialogue_mode = st.radio("请选择对话模式",
["LLM 对话",
"知识库问答",
"Bing 搜索问答"])
2023-08-01 14:18:30 +08:00
history_len = st.slider("历史对话轮数:", 1, 10, 1)
2023-07-28 06:58:34 +08:00
if dialogue_mode == "知识库问答":
2023-08-01 14:18:30 +08:00
selected_kb = st.selectbox("请选择知识库:", get_kb_list())
2023-07-28 06:58:34 +08:00
with st.expander(f"{selected_kb} 中已存储文件"):
2023-08-01 14:18:30 +08:00
st.write(get_kb_files(selected_kb))
2023-07-28 06:58:34 +08:00
# Display chat messages from history on app rerun
2023-08-01 14:18:30 +08:00
chat_box.output_messages()
2023-07-28 06:58:34 +08:00
if prompt := st.chat_input("What is up?"):
2023-08-01 14:18:30 +08:00
chat_box.user_say(prompt)
chat_box.ai_say("正在思考...")
# with api.chat_fastchat([{"role": "user", "content": "prompt"}], stream=streaming) as r: # todo: support history len
text = ""
r = api.chat_chat(prompt, no_remote_api=True)
for t in r:
text += t
chat_box.update_msg(text)
chat_box.update_msg(text, streaming=False)
# with api.chat_chat(prompt) as r:
# for t in r.iter_text(None):
# text += t
# chat_box.update_msg(text)
# chat_box.update_msg(text, streaming=False)
2023-07-28 06:58:34 +08:00
def knowledge_base_edit_page():
pass
def config_page():
pass
if __name__ == "__main__":
st.set_page_config("langchain-chatglm WebUI")
2023-08-01 14:18:30 +08:00
chat_box = ChatBox()
2023-07-28 06:58:34 +08:00
pages = {"对话": {"icon": "chat",
"func": dialogue_page,
},
"知识库管理": {"icon": "database-fill-gear",
"func": knowledge_base_edit_page,
},
"模型配置": {"icon": "gear",
"func": config_page,
}
}
with st.sidebar:
selected_page = option_menu("langchain-chatglm",
options=list(pages.keys()),
icons=[i["icon"] for i in pages.values()],
menu_icon="chat-quote",
default_index=0)
pages[selected_page]["func"]()