2023-08-01 14:47:38 +08:00
|
|
|
|
import streamlit as st
|
|
|
|
|
|
from webui_pages.utils import *
|
2023-08-07 21:00:55 +08:00
|
|
|
|
from st_aggrid import AgGrid
|
|
|
|
|
|
from st_aggrid.grid_options_builder import GridOptionsBuilder
|
|
|
|
|
|
import pandas as pd
|
2023-08-10 11:37:14 +08:00
|
|
|
|
from server.knowledge_base.utils import get_file_path
|
2023-08-11 08:37:07 +08:00
|
|
|
|
from server.knowledge_base.kb_service.base import get_kb_details, get_kb_doc_details
|
2023-08-10 11:37:14 +08:00
|
|
|
|
from typing import Literal, Dict, Tuple
|
2023-08-07 21:00:55 +08:00
|
|
|
|
|
|
|
|
|
|
SENTENCE_SIZE = 100
|
|
|
|
|
|
|
2023-08-01 14:47:38 +08:00
|
|
|
|
|
2023-08-08 23:58:44 +08:00
|
|
|
|
def config_aggrid(
|
2023-08-10 23:51:10 +08:00
|
|
|
|
df: pd.DataFrame,
|
|
|
|
|
|
columns: Dict[Tuple[str, str], Dict] = {},
|
|
|
|
|
|
selection_mode: Literal["single", "multiple", "disabled"] = "single",
|
|
|
|
|
|
use_checkbox: bool = False,
|
2023-08-08 23:58:44 +08:00
|
|
|
|
) -> GridOptionsBuilder:
|
|
|
|
|
|
gb = GridOptionsBuilder.from_dataframe(df)
|
2023-08-10 11:37:14 +08:00
|
|
|
|
gb.configure_column("No", width=40)
|
|
|
|
|
|
for (col, header), kw in columns.items():
|
|
|
|
|
|
gb.configure_column(col, header, wrapHeaderText=True, **kw)
|
|
|
|
|
|
gb.configure_selection(
|
|
|
|
|
|
selection_mode,
|
|
|
|
|
|
use_checkbox,
|
|
|
|
|
|
# pre_selected_rows=st.session_state.get("selected_rows", [0]),
|
|
|
|
|
|
)
|
2023-08-08 23:58:44 +08:00
|
|
|
|
return gb
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# kb_box = ChatBox(session_key="kb_messages")
|
|
|
|
|
|
|
2023-08-01 15:08:19 +08:00
|
|
|
|
def knowledge_base_page(api: ApiRequest):
|
2023-08-10 23:51:10 +08:00
|
|
|
|
# api = ApiRequest(base_url="http://127.0.0.1:7861", no_remote_api=True)
|
2023-08-11 08:37:07 +08:00
|
|
|
|
kb_details = get_kb_details()
|
2023-08-09 22:00:33 +08:00
|
|
|
|
kb_list = list(kb_details.kb_name)
|
2023-08-07 21:00:55 +08:00
|
|
|
|
|
2023-08-10 23:51:10 +08:00
|
|
|
|
cols = st.columns([3, 1, 1])
|
2023-08-08 23:58:44 +08:00
|
|
|
|
new_kb_name = cols[0].text_input(
|
|
|
|
|
|
"新知识库名称",
|
2023-08-10 23:51:10 +08:00
|
|
|
|
placeholder="新知识库名称,暂不支持中文命名",
|
2023-08-08 23:58:44 +08:00
|
|
|
|
label_visibility="collapsed",
|
|
|
|
|
|
key="new_kb_name",
|
|
|
|
|
|
)
|
2023-08-09 22:00:33 +08:00
|
|
|
|
|
2023-08-10 23:51:10 +08:00
|
|
|
|
if cols[1].button(
|
|
|
|
|
|
"新建",
|
|
|
|
|
|
disabled=not bool(new_kb_name),
|
|
|
|
|
|
use_container_width=True,
|
|
|
|
|
|
) and new_kb_name:
|
2023-08-09 22:00:33 +08:00
|
|
|
|
if new_kb_name in kb_list:
|
|
|
|
|
|
st.error(f"名为 {new_kb_name} 的知识库已经存在!")
|
|
|
|
|
|
else:
|
|
|
|
|
|
ret = api.create_knowledge_base(new_kb_name)
|
|
|
|
|
|
st.toast(ret["msg"])
|
|
|
|
|
|
st.experimental_rerun()
|
|
|
|
|
|
|
2023-08-10 23:51:10 +08:00
|
|
|
|
if cols[2].button(
|
|
|
|
|
|
"删除",
|
|
|
|
|
|
disabled=not bool(new_kb_name),
|
|
|
|
|
|
use_container_width=True,
|
|
|
|
|
|
) and new_kb_name:
|
2023-08-09 22:00:33 +08:00
|
|
|
|
if new_kb_name in kb_list:
|
|
|
|
|
|
ret = api.delete_knowledge_base(new_kb_name)
|
|
|
|
|
|
st.toast(ret["msg"])
|
|
|
|
|
|
st.experimental_rerun()
|
|
|
|
|
|
else:
|
|
|
|
|
|
st.error(f"名为 {new_kb_name} 的知识库不存在!")
|
2023-08-07 21:00:55 +08:00
|
|
|
|
|
2023-08-10 11:37:14 +08:00
|
|
|
|
st.write("知识库列表:")
|
2023-08-10 23:51:10 +08:00
|
|
|
|
st.info("请选择知识库")
|
2023-08-07 21:00:55 +08:00
|
|
|
|
if kb_list:
|
2023-08-08 23:58:44 +08:00
|
|
|
|
gb = config_aggrid(
|
|
|
|
|
|
kb_details,
|
|
|
|
|
|
{
|
2023-08-10 23:51:10 +08:00
|
|
|
|
("kb_name", "知识库名称"): {},
|
|
|
|
|
|
("vs_type", "知识库类型"): {},
|
|
|
|
|
|
("embed_model", "嵌入模型"): {},
|
|
|
|
|
|
("file_count", "文档数量"): {},
|
|
|
|
|
|
("create_time", "创建时间"): {},
|
|
|
|
|
|
("in_folder", "文件夹"): {},
|
|
|
|
|
|
("in_db", "数据库"): {},
|
2023-08-08 23:58:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
)
|
2023-08-10 23:51:10 +08:00
|
|
|
|
kb_grid = AgGrid(
|
|
|
|
|
|
kb_details,
|
|
|
|
|
|
gb.build(),
|
|
|
|
|
|
columns_auto_size_mode="FIT_CONTENTS",
|
|
|
|
|
|
theme="alpine",
|
|
|
|
|
|
)
|
2023-08-10 11:37:14 +08:00
|
|
|
|
# st.write(kb_grid)
|
2023-08-08 23:58:44 +08:00
|
|
|
|
if kb_grid.selected_rows:
|
2023-08-10 11:37:14 +08:00
|
|
|
|
# st.session_state.selected_rows = [x["nIndex"] for x in kb_grid.selected_rows]
|
2023-08-08 23:58:44 +08:00
|
|
|
|
kb = kb_grid.selected_rows[0]["kb_name"]
|
2023-08-07 21:00:55 +08:00
|
|
|
|
|
2023-08-08 23:58:44 +08:00
|
|
|
|
with st.sidebar:
|
2023-08-10 23:51:10 +08:00
|
|
|
|
# sentence_size = st.slider("文本入库分句长度限制", 1, 1000, SENTENCE_SIZE, disabled=True)
|
2023-08-08 23:58:44 +08:00
|
|
|
|
files = st.file_uploader("上传知识文件",
|
2023-08-10 23:51:10 +08:00
|
|
|
|
["docx", "txt", "md", "csv", "xlsx", "pdf"],
|
|
|
|
|
|
accept_multiple_files=True,
|
|
|
|
|
|
)
|
2023-08-08 23:58:44 +08:00
|
|
|
|
if st.button(
|
2023-08-10 23:51:10 +08:00
|
|
|
|
"添加文件到知识库",
|
|
|
|
|
|
help="请先上传文件,再点击添加",
|
|
|
|
|
|
use_container_width=True,
|
|
|
|
|
|
disabled=len(files) == 0,
|
2023-08-08 23:58:44 +08:00
|
|
|
|
):
|
|
|
|
|
|
for f in files:
|
|
|
|
|
|
ret = api.upload_kb_doc(f, kb)
|
|
|
|
|
|
if ret["code"] == 200:
|
|
|
|
|
|
st.toast(ret["msg"], icon="✔")
|
|
|
|
|
|
else:
|
|
|
|
|
|
st.toast(ret["msg"], icon="❌")
|
|
|
|
|
|
st.session_state.files = []
|
2023-08-07 21:00:55 +08:00
|
|
|
|
|
2023-08-10 23:51:10 +08:00
|
|
|
|
# if st.button(
|
|
|
|
|
|
# "重建知识库",
|
|
|
|
|
|
# help="无需上传文件,通过其它方式将文档拷贝到对应知识库content目录下,点击本按钮即可重建知识库。",
|
|
|
|
|
|
# use_container_width=True,
|
|
|
|
|
|
# disabled=True,
|
|
|
|
|
|
# ):
|
|
|
|
|
|
# progress = st.progress(0.0, "")
|
|
|
|
|
|
# for d in api.recreate_vector_store(kb):
|
|
|
|
|
|
# progress.progress(d["finished"] / d["total"], f"正在处理: {d['doc']}")
|
2023-08-08 23:58:44 +08:00
|
|
|
|
|
|
|
|
|
|
# 知识库详情
|
2023-08-10 23:51:10 +08:00
|
|
|
|
st.write(f"知识库 `{kb}` 详情:")
|
|
|
|
|
|
st.info("请选择文件")
|
2023-08-11 08:37:07 +08:00
|
|
|
|
doc_details = get_kb_doc_details(kb)
|
2023-08-09 22:00:33 +08:00
|
|
|
|
doc_details.drop(columns=["kb_name"], inplace=True)
|
|
|
|
|
|
|
2023-08-08 23:58:44 +08:00
|
|
|
|
gb = config_aggrid(
|
|
|
|
|
|
doc_details,
|
|
|
|
|
|
{
|
2023-08-10 23:51:10 +08:00
|
|
|
|
("file_name", "文档名称"): {},
|
|
|
|
|
|
("file_ext", "文档类型"): {},
|
|
|
|
|
|
("file_version", "文档版本"): {},
|
|
|
|
|
|
("document_loader", "文档加载器"): {},
|
|
|
|
|
|
("text_splitter", "分词器"): {},
|
|
|
|
|
|
("create_time", "创建时间"): {},
|
|
|
|
|
|
("in_folder", "文件夹"): {},
|
|
|
|
|
|
("in_db", "数据库"): {},
|
2023-08-08 23:58:44 +08:00
|
|
|
|
},
|
|
|
|
|
|
"multiple",
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2023-08-10 23:51:10 +08:00
|
|
|
|
doc_grid = AgGrid(
|
|
|
|
|
|
doc_details,
|
|
|
|
|
|
gb.build(),
|
|
|
|
|
|
columns_auto_size_mode="FIT_CONTENTS",
|
|
|
|
|
|
theme="alpine",
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2023-08-07 21:00:55 +08:00
|
|
|
|
cols = st.columns(3)
|
2023-08-08 23:58:44 +08:00
|
|
|
|
selected_rows = doc_grid.get("selected_rows", [])
|
2023-08-07 21:00:55 +08:00
|
|
|
|
|
2023-08-09 16:52:04 +08:00
|
|
|
|
cols = st.columns(4)
|
2023-08-07 21:00:55 +08:00
|
|
|
|
if selected_rows:
|
2023-08-08 23:58:44 +08:00
|
|
|
|
file_name = selected_rows[0]["file_name"]
|
2023-08-07 21:00:55 +08:00
|
|
|
|
file_path = get_file_path(kb, file_name)
|
|
|
|
|
|
with open(file_path, "rb") as fp:
|
2023-08-10 23:51:10 +08:00
|
|
|
|
cols[0].download_button(
|
|
|
|
|
|
"下载选中文档",
|
|
|
|
|
|
fp,
|
|
|
|
|
|
file_name=file_name,
|
|
|
|
|
|
use_container_width=True,)
|
2023-08-07 21:00:55 +08:00
|
|
|
|
else:
|
2023-08-10 23:51:10 +08:00
|
|
|
|
cols[0].download_button(
|
|
|
|
|
|
"下载选中文档",
|
|
|
|
|
|
"",
|
|
|
|
|
|
disabled=True,
|
|
|
|
|
|
use_container_width=True,)
|
|
|
|
|
|
|
|
|
|
|
|
if cols[1].button(
|
|
|
|
|
|
"入库",
|
|
|
|
|
|
disabled=len(selected_rows) == 0,
|
|
|
|
|
|
use_container_width=True,
|
|
|
|
|
|
):
|
2023-08-09 16:52:04 +08:00
|
|
|
|
for row in selected_rows:
|
|
|
|
|
|
api.update_kb_doc(kb, row["file_name"])
|
|
|
|
|
|
st.experimental_rerun()
|
|
|
|
|
|
|
2023-08-10 23:51:10 +08:00
|
|
|
|
if cols[2].button(
|
|
|
|
|
|
"出库",
|
|
|
|
|
|
disabled=len(selected_rows) == 0,
|
|
|
|
|
|
use_container_width=True,
|
|
|
|
|
|
):
|
2023-08-09 16:52:04 +08:00
|
|
|
|
for row in selected_rows:
|
|
|
|
|
|
api.delete_kb_doc(kb, row["file_name"])
|
|
|
|
|
|
st.experimental_rerun()
|
|
|
|
|
|
|
2023-08-10 23:51:10 +08:00
|
|
|
|
if cols[3].button(
|
|
|
|
|
|
"删除选中文档!",
|
|
|
|
|
|
type="primary",
|
|
|
|
|
|
use_container_width=True,
|
|
|
|
|
|
):
|
2023-08-07 21:00:55 +08:00
|
|
|
|
for row in selected_rows:
|
2023-08-09 16:52:04 +08:00
|
|
|
|
ret = api.delete_kb_doc(kb, row["file_name"], True)
|
2023-08-07 21:00:55 +08:00
|
|
|
|
st.toast(ret["msg"])
|
|
|
|
|
|
st.experimental_rerun()
|