提交 57c3cac3 作者: liang tongtong

Webui更新说明

1、自动读取knowledge_based_chatglm.py中LLM及embedding模型枚举,选择后点击setting进行模型加载,可随时切换模型进行测试
2、可手动调节保留对话历史长度,可根据显存大小自行调节
3、添加上传文件功能,通过下拉框选择已上传的文件,点击loading加载文件,过程中可随时更换加载的文件
4、底部添加use via API可对接到自己系统

TODO:
1、添加模型加载进度条
2、添加输出内容及错误提示
3、国家化语言切换
4、引用标注
5、添加插件系统(可基础lora训练等)
上级 f7f40406
......@@ -28,23 +28,32 @@ embedding_model_dict = {
llm_model_dict = {
"chatglm-6b": "THUDM/chatglm-6b",
"chatglm-6b-int4": "THUDM/chatglm-6b-int4",
"chatglm-6b-int4-qe": "THUDM/chatglm-6b-int4-qe",
# "chatglm-6b-int4-qe": "THUDM/chatglm-6b-int4-qe",
}
chatglm = ChatGLM()
chatglm.load_model(model_name_or_path=llm_model_dict[LLM_MODEL])
chatglm.history_len = LLM_HISTORY_LEN
chatglm = None
embeddings = None
def init_cfg(LLM_MODEL,EMBEDDING_MODEL, LLM_HISTORY_LEN,V_SEARCH_TOP_K=6):
global chatglm,embeddings,VECTOR_SEARCH_TOP_K
VECTOR_SEARCH_TOP_K=V_SEARCH_TOP_K
chatglm = ChatGLM()
chatglm.load_model(model_name_or_path=llm_model_dict[LLM_MODEL])
chatglm.history_len = LLM_HISTORY_LEN
def init_knowledge_vector_store(filepath):
embeddings = HuggingFaceEmbeddings(model_name=embedding_model_dict[EMBEDDING_MODEL], )
def init_knowledge_vector_store(filepath):
loader = UnstructuredFileLoader(filepath, mode="elements")
docs = loader.load()
vector_store = FAISS.from_documents(docs, embeddings)
return vector_store
def get_knowledge_based_answer(query, vector_store, chat_history=[]):
global chatglm,embeddings
system_template = """基于以下内容,简洁和专业的来回答用户的问题。
如果无法从中得到答案,请说 "不知道" 或 "没有足够的相关信息",不要试图编造答案。答案请使用中文。
----------------
......@@ -72,6 +81,7 @@ def get_knowledge_based_answer(query, vector_store, chat_history=[]):
if __name__ == "__main__":
init_cfg(LLM_MODEL,EMBEDDING_MODEL, LLM_HISTORY_LEN)
filepath = input("Input your local knowledge file path 请输入本地知识文件路径:")
vector_store = init_knowledge_vector_store(filepath)
history = []
......
import gradio as gr
import os
import shutil
import knowledge_based_chatglm as kb
# class kb:
# def __init__(self):
# pass
# def init_knowledge_vector_store(filepath):
# return filepath
# def get_knowledge_based_answer(*args):
# return args
def get_file_list():
if not os.path.exists("content"):
return []
return [f for f in os.listdir("content")]
file_list = get_file_list()
embedding_model_dict_list = list(kb.embedding_model_dict.keys())
llm_model_dict_list = list(kb.llm_model_dict.keys())
def upload_file(file):
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)
return gr.Dropdown.update(choices=file_list, value=filename)
def getAnswer(q, v, h):
resp, history = kb.get_knowledge_based_answer(
query=q, vector_store=v, chat_history=h)
return history, history
with gr.Blocks(css="""
.importantButton {
background: linear-gradient(45deg, #7e0570,#5d1c99, #6e00ff) !important;
border: none !important;
}
.importantButton:hover {
background: linear-gradient(45deg, #ff00e0,#8500ff, #6e00ff) !important;
border: none !important;
}
""") as demo:
gr.Markdown(
f"""
# 🎉langchain-ChatGLM WebUI🎉
👍 [https://github.com/imClumsyPanda/langchain-ChatGLM](https://github.com/imClumsyPanda/langchain-ChatGLM)
""")
with gr.Row():
with gr.Column(scale=2):
chatbot = gr.Chatbot(elem_id="chat-box",
show_label=False).style(height=600)
with gr.Column(scale=1):
with gr.Column():
llm_model = gr.Radio(
llm_model_dict_list, label="llm model",
value="chatglm-6b", interactive=True)
LLM_HISTORY_LEN = gr.Slider(
1, 10, value=3, step=1, label="LLM history len", interactive=True)
embedding_model = gr.Radio(
embedding_model_dict_list, label="embedding model",
value="text2vec", interactive=True)
VECTOR_SEARCH_TOP_K = gr.Slider(
1, 20, value=6, step=1, label="vector search top k", interactive=True)
load_model_button = gr.Button("step.1:setting")
load_model_button.click(lambda *args:
kb.init_cfg(args[0], args[1], args[2], args[3]),
show_progress=True,
api_name="init_cfg",
inputs=[llm_model, embedding_model, VECTOR_SEARCH_TOP_K, LLM_HISTORY_LEN])
with gr.Column():
with gr.Tab("select"):
selectFile = gr.Dropdown(
file_list, label="content file", interactive=True, value=file_list[0] if len(file_list) > 0 else None)
with gr.Tab("upload"):
file = gr.File(label="content file", file_types=[
'.txt', '.md', '.docx']).style(height=100)
# 将上传的文件保存到content文件夹下,并更新下拉框
file.upload(upload_file, inputs=file, outputs=selectFile)
history = gr.State([])
vector_store = gr.State()
load_button = gr.Button("step.2:loading")
load_button.click(lambda fileName:
kb.init_knowledge_vector_store(
"content/"+fileName),
show_progress=True,
api_name="init_knowledge_vector_store",
inputs=selectFile, outputs=vector_store)
with gr.Row():
with gr.Column(scale=2):
query = gr.Textbox(show_label=False, placeholder="Prompts", lines=1, value="用200字总结一下").style(
container=False)
with gr.Column(scale=1):
generate_button = gr.Button(
"step.3:asking", elem_classes="importantButton")
generate_button.click(getAnswer, [query, vector_store, history],
[chatbot, history],api_name="get_knowledge_based_answer")
demo.queue(concurrency_count=3).launch(
server_name='0.0.0.0', share=False, inbrowser=False)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论