|
@@ -14,19 +14,12 @@ VECTOR_SEARCH_TOP_K = 6
|
|
LLM_HISTORY_LEN = 3
|
|
LLM_HISTORY_LEN = 3
|
|
|
|
|
|
|
|
|
|
-def get_file_list():
|
|
|
|
- if not os.path.exists("content"):
|
|
|
|
- return []
|
|
|
|
- return [f for f in os.listdir("content")]
|
|
|
|
-
|
|
|
|
-
|
|
|
|
def get_vs_list():
|
|
def get_vs_list():
|
|
- if not os.path.exists("vector_store"):
|
|
|
|
|
|
+ if not os.path.exists(VS_ROOT_PATH):
|
|
return []
|
|
return []
|
|
- return [f for f in os.listdir("vector_store")]
|
|
|
|
|
|
+ return ["新建知识库"] + os.listdir(VS_ROOT_PATH)
|
|
|
|
|
|
|
|
|
|
-file_list = get_file_list()
|
|
|
|
vs_list = get_vs_list()
|
|
vs_list = get_vs_list()
|
|
|
|
|
|
embedding_model_dict_list = list(embedding_model_dict.keys())
|
|
embedding_model_dict_list = list(embedding_model_dict.keys())
|
|
@@ -36,19 +29,8 @@ llm_model_dict_list = list(llm_model_dict.keys())
|
|
local_doc_qa = LocalDocQA()
|
|
local_doc_qa = LocalDocQA()
|
|
|
|
|
|
|
|
|
|
-def upload_file(file, chatbot):
|
|
|
|
- if not os.path.exists("content"):
|
|
|
|
- os.mkdir("content")
|
|
|
|
- filename = os.path.basename(file.name)
|
|
|
|
- shutil.move(file.name, "content/" + filename)
|
|
|
|
- # file_list首位插入新上传的文件
|
|
|
|
- file_list.insert(0, filename)
|
|
|
|
- status = "已将xx上传至xxx"
|
|
|
|
- return chatbot + [None, status]
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-def get_answer(query, vs_path, history):
|
|
|
|
- if vs_path:
|
|
|
|
|
|
+def get_answer(query, vs_path, history, mode):
|
|
|
|
+ if vs_path and mode == "知识库问答":
|
|
resp, history = local_doc_qa.get_knowledge_based_answer(
|
|
resp, history = local_doc_qa.get_knowledge_based_answer(
|
|
query=query, vs_path=vs_path, chat_history=history)
|
|
query=query, vs_path=vs_path, chat_history=history)
|
|
source = "".join([f"""<details> <summary>出处 {i + 1}</summary>
|
|
source = "".join([f"""<details> <summary>出处 {i + 1}</summary>
|
|
@@ -59,7 +41,7 @@ def get_answer(query, vs_path, history):
|
|
history[-1][-1] += source
|
|
history[-1][-1] += source
|
|
else:
|
|
else:
|
|
resp = local_doc_qa.llm._call(query)
|
|
resp = local_doc_qa.llm._call(query)
|
|
- history = history + [[None, resp + "\n如需基于知识库进行问答,请先加载知识库后,再进行提问。"]]
|
|
|
|
|
|
+ history = history + [[query, resp + ("\n\n当前知识库为空,如需基于知识库进行问答,请先加载知识库后,再进行提问。" if mode == "知识库问答" else "")]]
|
|
return history, ""
|
|
return history, ""
|
|
|
|
|
|
|
|
|
|
@@ -73,10 +55,10 @@ def init_model():
|
|
try:
|
|
try:
|
|
local_doc_qa.init_cfg()
|
|
local_doc_qa.init_cfg()
|
|
local_doc_qa.llm._call("你好")
|
|
local_doc_qa.llm._call("你好")
|
|
- return """模型已成功加载,请选择文件后点击"加载文件"按钮"""
|
|
|
|
|
|
+ return """模型已成功加载,可以开始对话,或从右侧选择模式后开始对话"""
|
|
except Exception as e:
|
|
except Exception as e:
|
|
print(e)
|
|
print(e)
|
|
- return """模型未成功加载,请重新选择后点击"加载模型"按钮"""
|
|
|
|
|
|
+ return """模型未成功加载,请到页面左上角"模型配置"选项卡中重新选择后点击"加载模型"按钮"""
|
|
|
|
|
|
|
|
|
|
def reinit_model(llm_model, embedding_model, llm_history_len, use_ptuning_v2, top_k, history):
|
|
def reinit_model(llm_model, embedding_model, llm_history_len, use_ptuning_v2, top_k, history):
|
|
@@ -86,24 +68,54 @@ def reinit_model(llm_model, embedding_model, llm_history_len, use_ptuning_v2, to
|
|
llm_history_len=llm_history_len,
|
|
llm_history_len=llm_history_len,
|
|
use_ptuning_v2=use_ptuning_v2,
|
|
use_ptuning_v2=use_ptuning_v2,
|
|
top_k=top_k)
|
|
top_k=top_k)
|
|
- model_status = """模型已成功重新加载,请选择文件后点击"加载文件"按钮"""
|
|
|
|
|
|
+ model_status = """模型已成功重新加载,可以开始对话,或从右侧选择模式后开始对话"""
|
|
except Exception as e:
|
|
except Exception as e:
|
|
print(e)
|
|
print(e)
|
|
- model_status = """模型未成功重新加载,请重新选择后点击"加载模型"按钮"""
|
|
|
|
|
|
+ model_status = """模型未成功重新加载,请到页面左上角"模型配置"选项卡中重新选择后点击"加载模型"按钮"""
|
|
return history + [[None, model_status]]
|
|
return history + [[None, model_status]]
|
|
|
|
|
|
|
|
|
|
-def get_vector_store(filepath, history):
|
|
|
|
|
|
+def get_vector_store(vs_id, files, history):
|
|
|
|
+ vs_path = VS_ROOT_PATH + vs_id
|
|
|
|
+ filelist = []
|
|
|
|
+ for file in files:
|
|
|
|
+ filename = os.path.split(file.name)[-1]
|
|
|
|
+ shutil.move(file.name, UPLOAD_ROOT_PATH + filename)
|
|
|
|
+ filelist.append(UPLOAD_ROOT_PATH + filename)
|
|
if local_doc_qa.llm and local_doc_qa.embeddings:
|
|
if local_doc_qa.llm and local_doc_qa.embeddings:
|
|
- vs_path = local_doc_qa.init_knowledge_vector_store(["content/" + filepath])
|
|
|
|
- if vs_path:
|
|
|
|
- file_status = "文件已成功加载,请开始提问"
|
|
|
|
|
|
+ vs_path, loaded_files = local_doc_qa.init_knowledge_vector_store(filelist, vs_path)
|
|
|
|
+ if len(loaded_files):
|
|
|
|
+ file_status = f"已上传 {'、'.join([os.path.split(i)[-1] for i in loaded_files])} 至知识库,并已加载知识库,请开始提问"
|
|
else:
|
|
else:
|
|
file_status = "文件未成功加载,请重新上传文件"
|
|
file_status = "文件未成功加载,请重新上传文件"
|
|
else:
|
|
else:
|
|
file_status = "模型未完成加载,请先在加载模型后再导入文件"
|
|
file_status = "模型未完成加载,请先在加载模型后再导入文件"
|
|
vs_path = None
|
|
vs_path = None
|
|
- return vs_path, history + [[None, file_status]]
|
|
|
|
|
|
+ return vs_path, None, history + [[None, file_status]]
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def change_vs_name_input(vs_id):
|
|
|
|
+ if vs_id == "新建知识库":
|
|
|
|
+ return gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), None
|
|
|
|
+ else:
|
|
|
|
+ return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), VS_ROOT_PATH + vs_id
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def change_mode(mode):
|
|
|
|
+ if mode == "知识库问答":
|
|
|
|
+ return gr.update(visible=True)
|
|
|
|
+ else:
|
|
|
|
+ return gr.update(visible=False)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def add_vs_name(vs_name, vs_list, chatbot):
|
|
|
|
+ if vs_name in vs_list:
|
|
|
|
+ chatbot = chatbot + [[None, "与已有知识库名称冲突,请重新选择其他名称后提交"]]
|
|
|
|
+ return gr.update(visible=True), vs_list, chatbot
|
|
|
|
+ else:
|
|
|
|
+ chatbot = chatbot + [
|
|
|
|
+ [None, f"""已新增知识库"{vs_name}",将在上传文件并载入成功后进行存储。请在开始对话前,先完成文件上传。 """]]
|
|
|
|
+ return gr.update(visible=True, choices=vs_list + [vs_name], value=vs_name), vs_list + [vs_name], chatbot
|
|
|
|
|
|
|
|
|
|
block_css = """.importantButton {
|
|
block_css = """.importantButton {
|
|
@@ -123,46 +135,88 @@ webui_title = """
|
|
|
|
|
|
"""
|
|
"""
|
|
|
|
|
|
-init_message = """欢迎使用 langchain-ChatGLM Web UI,开始提问前,请依次如下 3 个步骤:
|
|
|
|
-1. 选择语言模型、Embedding 模型及相关参数,如果使用 ptuning-v2 方式微调过模型,将 PrefixEncoder 模型放在 ptuning-v2 文件夹里并勾选相关选项,然后点击"重新加载模型",并等待加载完成提示
|
|
|
|
-2. 上传或选择已有文件作为本地知识文档输入后点击"重新加载文档",并等待加载完成提示
|
|
|
|
-3. 输入要提交的问题后,点击回车提交 """
|
|
|
|
|
|
+init_message = """欢迎使用 langchain-ChatGLM Web UI!
|
|
|
|
+
|
|
|
|
+请在右侧切换模式,目前支持直接与 LLM 模型对话或基于本地知识库问答。
|
|
|
|
+
|
|
|
|
+知识库问答模式中,选择知识库名称后,即可开始问答,如有需要可以在选择知识库名称后上传文件/文件夹至知识库。
|
|
|
|
+
|
|
|
|
+知识库暂不支持文件删除,该功能将在后续版本中推出。
|
|
|
|
+"""
|
|
|
|
|
|
model_status = init_model()
|
|
model_status = init_model()
|
|
|
|
|
|
with gr.Blocks(css=block_css) as demo:
|
|
with gr.Blocks(css=block_css) as demo:
|
|
- vs_path, file_status, model_status = gr.State(""), gr.State(""), gr.State(model_status)
|
|
|
|
|
|
+ vs_path, file_status, model_status, vs_list = gr.State(""), gr.State(""), gr.State(model_status), gr.State(vs_list)
|
|
gr.Markdown(webui_title)
|
|
gr.Markdown(webui_title)
|
|
- with gr.Tab("聊天"):
|
|
|
|
|
|
+ with gr.Tab("对话"):
|
|
with gr.Row():
|
|
with gr.Row():
|
|
- with gr.Column(scale=2):
|
|
|
|
|
|
+ with gr.Column(scale=10):
|
|
chatbot = gr.Chatbot([[None, init_message], [None, model_status.value]],
|
|
chatbot = gr.Chatbot([[None, init_message], [None, model_status.value]],
|
|
elem_id="chat-box",
|
|
elem_id="chat-box",
|
|
show_label=False).style(height=750)
|
|
show_label=False).style(height=750)
|
|
query = gr.Textbox(show_label=False,
|
|
query = gr.Textbox(show_label=False,
|
|
placeholder="请输入提问内容,按回车进行提交",
|
|
placeholder="请输入提问内容,按回车进行提交",
|
|
).style(container=False)
|
|
).style(container=False)
|
|
-
|
|
|
|
- with gr.Column(scale=1):
|
|
|
|
- # with gr.Column():
|
|
|
|
- # with gr.Tab("select"):
|
|
|
|
- selectFile = gr.Dropdown(vs_list,
|
|
|
|
- label="请选择要加载的知识库",
|
|
|
|
- interactive=True,
|
|
|
|
- value=vs_list[0] if len(vs_list) > 0 else None)
|
|
|
|
- #
|
|
|
|
- gr.Markdown("向知识库中添加文件")
|
|
|
|
- with gr.Tab("上传文件"):
|
|
|
|
- files = gr.File(label="向知识库中添加文件",
|
|
|
|
- file_types=['.txt', '.md', '.docx', '.pdf'],
|
|
|
|
- file_count="multiple"
|
|
|
|
- ) # .style(height=100)
|
|
|
|
- with gr.Tab("上传文件夹"):
|
|
|
|
- files = gr.File(label="向知识库中添加文件",
|
|
|
|
- file_types=['.txt', '.md', '.docx', '.pdf'],
|
|
|
|
- file_count="directory"
|
|
|
|
- ) # .style(height=100)
|
|
|
|
- load_file_button = gr.Button("加载知识库")
|
|
|
|
|
|
+ with gr.Column(scale=5):
|
|
|
|
+ mode = gr.Radio(["LLM 对话", "知识库问答"],
|
|
|
|
+ label="请选择使用模式",
|
|
|
|
+ value="知识库问答", )
|
|
|
|
+ vs_setting = gr.Accordion("配置知识库")
|
|
|
|
+ mode.change(fn=change_mode,
|
|
|
|
+ inputs=mode,
|
|
|
|
+ outputs=vs_setting)
|
|
|
|
+ with vs_setting:
|
|
|
|
+ select_vs = gr.Dropdown(vs_list.value,
|
|
|
|
+ label="请选择要加载的知识库",
|
|
|
|
+ interactive=True,
|
|
|
|
+ value=vs_list.value[0] if len(vs_list.value) > 0 else None
|
|
|
|
+ )
|
|
|
|
+ vs_name = gr.Textbox(label="请输入新建知识库名称",
|
|
|
|
+ lines=1,
|
|
|
|
+ interactive=True)
|
|
|
|
+ vs_add = gr.Button(value="添加至知识库选项")
|
|
|
|
+ vs_add.click(fn=add_vs_name,
|
|
|
|
+ inputs=[vs_name, vs_list, chatbot],
|
|
|
|
+ outputs=[select_vs, vs_list, chatbot])
|
|
|
|
+
|
|
|
|
+ file2vs = gr.Column(visible=False)
|
|
|
|
+ with file2vs:
|
|
|
|
+ # load_vs = gr.Button("加载知识库")
|
|
|
|
+ gr.Markdown("向知识库中添加文件")
|
|
|
|
+ with gr.Tab("上传文件"):
|
|
|
|
+ files = gr.File(label="添加文件",
|
|
|
|
+ file_types=['.txt', '.md', '.docx', '.pdf'],
|
|
|
|
+ file_count="multiple",
|
|
|
|
+ show_label=False
|
|
|
|
+ )
|
|
|
|
+ load_file_button = gr.Button("上传文件并加载知识库")
|
|
|
|
+ with gr.Tab("上传文件夹"):
|
|
|
|
+ folder_files = gr.File(label="添加文件",
|
|
|
|
+ # file_types=['.txt', '.md', '.docx', '.pdf'],
|
|
|
|
+ file_count="directory",
|
|
|
|
+ show_label=False
|
|
|
|
+ )
|
|
|
|
+ load_folder_button = gr.Button("上传文件夹并加载知识库")
|
|
|
|
+ # load_vs.click(fn=)
|
|
|
|
+ select_vs.change(fn=change_vs_name_input,
|
|
|
|
+ inputs=select_vs,
|
|
|
|
+ outputs=[vs_name, vs_add, file2vs, vs_path])
|
|
|
|
+ # 将上传的文件保存到content文件夹下,并更新下拉框
|
|
|
|
+ load_file_button.click(get_vector_store,
|
|
|
|
+ show_progress=True,
|
|
|
|
+ inputs=[select_vs, files, chatbot],
|
|
|
|
+ outputs=[vs_path, files, chatbot],
|
|
|
|
+ )
|
|
|
|
+ load_folder_button.click(get_vector_store,
|
|
|
|
+ show_progress=True,
|
|
|
|
+ inputs=[select_vs, folder_files, chatbot],
|
|
|
|
+ outputs=[vs_path, folder_files, chatbot],
|
|
|
|
+ )
|
|
|
|
+ query.submit(get_answer,
|
|
|
|
+ [query, vs_path, chatbot, mode],
|
|
|
|
+ [chatbot, query],
|
|
|
|
+ )
|
|
with gr.Tab("模型配置"):
|
|
with gr.Tab("模型配置"):
|
|
llm_model = gr.Radio(llm_model_dict_list,
|
|
llm_model = gr.Radio(llm_model_dict_list,
|
|
label="LLM 模型",
|
|
label="LLM 模型",
|
|
@@ -172,7 +226,7 @@ with gr.Blocks(css=block_css) as demo:
|
|
10,
|
|
10,
|
|
value=LLM_HISTORY_LEN,
|
|
value=LLM_HISTORY_LEN,
|
|
step=1,
|
|
step=1,
|
|
- label="LLM history len",
|
|
|
|
|
|
+ label="LLM 对话轮数",
|
|
interactive=True)
|
|
interactive=True)
|
|
use_ptuning_v2 = gr.Checkbox(USE_PTUNING_V2,
|
|
use_ptuning_v2 = gr.Checkbox(USE_PTUNING_V2,
|
|
label="使用p-tuning-v2微调过的模型",
|
|
label="使用p-tuning-v2微调过的模型",
|
|
@@ -193,19 +247,6 @@ with gr.Blocks(css=block_css) as demo:
|
|
inputs=[llm_model, embedding_model, llm_history_len, use_ptuning_v2, top_k, chatbot],
|
|
inputs=[llm_model, embedding_model, llm_history_len, use_ptuning_v2, top_k, chatbot],
|
|
outputs=chatbot
|
|
outputs=chatbot
|
|
)
|
|
)
|
|
- # 将上传的文件保存到content文件夹下,并更新下拉框
|
|
|
|
- files.upload(upload_file,
|
|
|
|
- inputs=[files, chatbot],
|
|
|
|
- outputs=chatbot)
|
|
|
|
- load_file_button.click(get_vector_store,
|
|
|
|
- show_progress=True,
|
|
|
|
- inputs=[selectFile, chatbot],
|
|
|
|
- outputs=[vs_path, chatbot],
|
|
|
|
- )
|
|
|
|
- query.submit(get_answer,
|
|
|
|
- [query, vs_path, chatbot],
|
|
|
|
- [chatbot, query],
|
|
|
|
- )
|
|
|
|
|
|
|
|
demo.queue(concurrency_count=3
|
|
demo.queue(concurrency_count=3
|
|
).launch(server_name='0.0.0.0',
|
|
).launch(server_name='0.0.0.0',
|