添加自定义命令: (#2229)
/new [conv_name] 新建会话 /del [conv_name] 删除会话 /clear [conv_name] 清空会话 /help 命令帮助 新增依赖:streamlit-modal
This commit is contained in:
parent
b7a50daa0f
commit
c4fe3393b3
|
|
@ -58,6 +58,7 @@ streamlit~=1.28.2 # # on win, make sure write its path in environment variable
|
|||
streamlit-option-menu>=0.3.6
|
||||
streamlit-antd-components>=0.2.3
|
||||
streamlit-chatbox>=1.1.11
|
||||
streamlit-modal==0.1.0
|
||||
streamlit-aggrid>=0.3.4.post3
|
||||
httpx[brotli,http2,socks]~=0.24.1
|
||||
watchdog
|
||||
|
|
@ -4,6 +4,7 @@ streamlit~=1.28.2
|
|||
streamlit-option-menu>=0.3.6
|
||||
streamlit-antd-components>=0.2.3
|
||||
streamlit-chatbox>=1.1.11
|
||||
streamlit-modal==0.1.0
|
||||
streamlit-aggrid>=0.3.4.post3
|
||||
httpx[brotli,http2,socks]~=0.24.1
|
||||
watchdog
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
import streamlit as st
|
||||
from webui_pages.utils import *
|
||||
from streamlit_chatbox import *
|
||||
from streamlit_modal import Modal
|
||||
from datetime import datetime
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
from configs import (TEMPERATURE, HISTORY_LEN, PROMPT_TEMPLATES,
|
||||
DEFAULT_KNOWLEDGE_BASE, DEFAULT_SEARCH_ENGINE, SUPPORT_AGENT_MODEL)
|
||||
from server.knowledge_base.utils import LOADER_DICT
|
||||
|
|
@ -47,6 +50,53 @@ def upload_temp_docs(files, _api: ApiRequest) -> str:
|
|||
return _api.upload_temp_docs(files).get("data", {}).get("id")
|
||||
|
||||
|
||||
def parse_command(text: str, modal: Modal) -> bool:
|
||||
'''
|
||||
检查用户是否输入了自定义命令,当前支持:
|
||||
/new {session_name}。如果未提供名称,默认为“会话X”
|
||||
/del {session_name}。如果未提供名称,在会话数量>1的情况下,删除当前会话。
|
||||
/clear {session_name}。如果未提供名称,默认清除当前会话
|
||||
/help。查看命令帮助
|
||||
返回值:输入的是命令返回True,否则返回False
|
||||
'''
|
||||
if m := re.match(r"/([^\s]+)\s*(.*)", text):
|
||||
cmd, name = m.groups()
|
||||
name = name.strip()
|
||||
conv_names = chat_box.get_chat_names()
|
||||
if cmd == "help":
|
||||
modal.open()
|
||||
elif cmd == "new":
|
||||
if not name:
|
||||
i = 1
|
||||
while True:
|
||||
name = f"会话{i}"
|
||||
if name not in conv_names:
|
||||
break
|
||||
i += 1
|
||||
if name in st.session_state["conversation_ids"]:
|
||||
st.error(f"该会话名称 “{name}” 已存在")
|
||||
time.sleep(1)
|
||||
else:
|
||||
st.session_state["conversation_ids"][name] = uuid.uuid4().hex
|
||||
st.session_state["cur_conv_name"] = name
|
||||
elif cmd == "del":
|
||||
name = name or st.session_state.get("cur_conv_name")
|
||||
if len(conv_names) == 1:
|
||||
st.error("这是最后一个会话,无法删除")
|
||||
time.sleep(1)
|
||||
elif not name or name not in st.session_state["conversation_ids"]:
|
||||
st.error(f"无效的会话名称:“{name}”")
|
||||
time.sleep(1)
|
||||
else:
|
||||
st.session_state["conversation_ids"].pop(name, None)
|
||||
chat_box.del_chat_name(name)
|
||||
st.session_state["cur_conv_name"] = ""
|
||||
elif cmd == "clear":
|
||||
chat_box.reset_history(name=name or None)
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def dialogue_page(api: ApiRequest, is_lite: bool = False):
|
||||
st.session_state.setdefault("conversation_ids", {})
|
||||
st.session_state["conversation_ids"].setdefault(chat_box.cur_chat_name, uuid.uuid4().hex)
|
||||
|
|
@ -60,25 +110,15 @@ def dialogue_page(api: ApiRequest, is_lite: bool = False):
|
|||
)
|
||||
chat_box.init_session()
|
||||
|
||||
# 弹出自定义命令帮助信息
|
||||
modal = Modal("自定义命令", key="cmd_help", max_width="500")
|
||||
if modal.is_open():
|
||||
with modal.container():
|
||||
cmds = [x for x in parse_command.__doc__.split("\n") if x.strip().startswith("/")]
|
||||
st.write("\n\n".join(cmds))
|
||||
|
||||
with st.sidebar:
|
||||
# 多会话
|
||||
cols = st.columns([3, 1])
|
||||
conv_name = cols[0].text_input("会话名称")
|
||||
with cols[1]:
|
||||
if st.button("添加"):
|
||||
if not conv_name or conv_name in st.session_state["conversation_ids"]:
|
||||
st.error("请指定有效的会话名称")
|
||||
else:
|
||||
st.session_state["conversation_ids"][conv_name] = uuid.uuid4().hex
|
||||
st.session_state["cur_conv_name"] = conv_name
|
||||
st.session_state["conv_name"] = ""
|
||||
if st.button("删除"):
|
||||
if not conv_name or conv_name not in st.session_state["conversation_ids"]:
|
||||
st.error("请指定有效的会话名称")
|
||||
else:
|
||||
st.session_state["conversation_ids"].pop(conv_name, None)
|
||||
st.session_state["cur_conv_name"] = ""
|
||||
|
||||
conv_names = list(st.session_state["conversation_ids"].keys())
|
||||
index = 0
|
||||
if st.session_state.get("cur_conv_name") in conv_names:
|
||||
|
|
@ -236,7 +276,7 @@ def dialogue_page(api: ApiRequest, is_lite: bool = False):
|
|||
# Display chat messages from history on app rerun
|
||||
chat_box.output_messages()
|
||||
|
||||
chat_input_placeholder = "请输入对话内容,换行请使用Shift+Enter "
|
||||
chat_input_placeholder = "请输入对话内容,换行请使用Shift+Enter。输入/help查看自定义命令 "
|
||||
|
||||
def on_feedback(
|
||||
feedback,
|
||||
|
|
@ -256,6 +296,9 @@ def dialogue_page(api: ApiRequest, is_lite: bool = False):
|
|||
}
|
||||
|
||||
if prompt := st.chat_input(chat_input_placeholder, key="prompt"):
|
||||
if parse_command(text=prompt, modal=modal): # 用户输入自定义命令
|
||||
st.rerun()
|
||||
else:
|
||||
history = get_messages_history(history_len)
|
||||
chat_box.user_say(prompt)
|
||||
if dialogue_mode == "LLM 对话":
|
||||
|
|
|
|||
Loading…
Reference in New Issue