logging: 格式日志输出 (#268)
* logging: 统一日志格式输出 --------- Co-authored-by: Bob Chang <bob.chang@amway.com> Co-authored-by: imClumsyPanda <littlepanda0716@gmail.com>
This commit is contained in:
parent
7f25c9fa8e
commit
47e9bdb122
|
|
@ -31,13 +31,13 @@ if __name__ == "__main__":
|
||||||
chat_history=history,
|
chat_history=history,
|
||||||
streaming=STREAMING):
|
streaming=STREAMING):
|
||||||
if STREAMING:
|
if STREAMING:
|
||||||
print(resp["result"][last_print_len:], end="", flush=True)
|
logger.info(resp["result"][last_print_len:], end="", flush=True)
|
||||||
last_print_len = len(resp["result"])
|
last_print_len = len(resp["result"])
|
||||||
else:
|
else:
|
||||||
print(resp["result"])
|
logger.info(resp["result"])
|
||||||
if REPLY_WITH_SOURCE:
|
if REPLY_WITH_SOURCE:
|
||||||
source_text = [f"""出处 [{inum + 1}] {os.path.split(doc.metadata['source'])[-1]}:\n\n{doc.page_content}\n\n"""
|
source_text = [f"""出处 [{inum + 1}] {os.path.split(doc.metadata['source'])[-1]}:\n\n{doc.page_content}\n\n"""
|
||||||
# f"""相关度:{doc.metadata['score']}\n\n"""
|
# f"""相关度:{doc.metadata['score']}\n\n"""
|
||||||
for inum, doc in
|
for inum, doc in
|
||||||
enumerate(resp["source_documents"])]
|
enumerate(resp["source_documents"])]
|
||||||
print("\n\n" + "\n\n".join(source_text))
|
logger.info("\n\n" + "\n\n".join(source_text))
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,13 @@
|
||||||
import torch.cuda
|
import torch.cuda
|
||||||
import torch.backends
|
import torch.backends
|
||||||
import os
|
import os
|
||||||
|
import logging
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
LOG_FORMAT = "%(levelname) -5s %(asctime)s" "-1d: %(message)s"
|
||||||
|
logger = logging.getLogger()
|
||||||
|
logger.setLevel(logging.INFO)
|
||||||
|
logging.basicConfig(format=LOG_FORMAT)
|
||||||
|
|
||||||
embedding_model_dict = {
|
embedding_model_dict = {
|
||||||
"ernie-tiny": "nghuyong/ernie-3.0-nano-zh",
|
"ernie-tiny": "nghuyong/ernie-3.0-nano-zh",
|
||||||
|
|
@ -63,3 +70,13 @@ LLM_HISTORY_LEN = 3
|
||||||
VECTOR_SEARCH_TOP_K = 5
|
VECTOR_SEARCH_TOP_K = 5
|
||||||
|
|
||||||
NLTK_DATA_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)), "nltk_data")
|
NLTK_DATA_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)), "nltk_data")
|
||||||
|
|
||||||
|
FLAG_USER_NAME = uuid.uuid4().hex
|
||||||
|
|
||||||
|
logger.info(f"""
|
||||||
|
loading model config
|
||||||
|
llm device: {LLM_DEVICE}
|
||||||
|
embedding device: {EMBEDDING_DEVICE}
|
||||||
|
dir: {os.path.dirname(os.path.dirname(__file__))}
|
||||||
|
flagging username: {FLAG_USER_NAME}
|
||||||
|
""")
|
||||||
|
|
@ -126,8 +126,7 @@ class ChatGLM(LLM):
|
||||||
model_config.pre_seq_len = prefix_encoder_config['pre_seq_len']
|
model_config.pre_seq_len = prefix_encoder_config['pre_seq_len']
|
||||||
model_config.prefix_projection = prefix_encoder_config['prefix_projection']
|
model_config.prefix_projection = prefix_encoder_config['prefix_projection']
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
logger.error(f"加载PrefixEncoder config.json失败: {e}")
|
||||||
print("加载PrefixEncoder config.json失败")
|
|
||||||
self.model = AutoModel.from_pretrained(model_name_or_path, config=model_config, trust_remote_code=True,
|
self.model = AutoModel.from_pretrained(model_name_or_path, config=model_config, trust_remote_code=True,
|
||||||
**kwargs)
|
**kwargs)
|
||||||
if LLM_LORA_PATH and use_lora:
|
if LLM_LORA_PATH and use_lora:
|
||||||
|
|
@ -165,8 +164,7 @@ class ChatGLM(LLM):
|
||||||
self.model.transformer.prefix_encoder.load_state_dict(new_prefix_state_dict)
|
self.model.transformer.prefix_encoder.load_state_dict(new_prefix_state_dict)
|
||||||
self.model.transformer.prefix_encoder.float()
|
self.model.transformer.prefix_encoder.float()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
logger.error(f"加载PrefixEncoder模型参数失败:{e}")
|
||||||
print("加载PrefixEncoder模型参数失败")
|
|
||||||
|
|
||||||
self.model = self.model.eval()
|
self.model = self.model.eval()
|
||||||
|
|
||||||
|
|
@ -177,8 +175,8 @@ if __name__ == "__main__":
|
||||||
llm_device=LLM_DEVICE, )
|
llm_device=LLM_DEVICE, )
|
||||||
last_print_len = 0
|
last_print_len = 0
|
||||||
for resp, history in llm._call("你好", streaming=True):
|
for resp, history in llm._call("你好", streaming=True):
|
||||||
print(resp[last_print_len:], end="", flush=True)
|
logger.info(resp[last_print_len:], end="", flush=True)
|
||||||
last_print_len = len(resp)
|
last_print_len = len(resp)
|
||||||
for resp, history in llm._call("你好", streaming=False):
|
for resp, history in llm._call("你好", streaming=False):
|
||||||
print(resp)
|
logger.info(resp)
|
||||||
pass
|
pass
|
||||||
|
|
|
||||||
26
webui.py
26
webui.py
|
|
@ -4,7 +4,6 @@ import shutil
|
||||||
from chains.local_doc_qa import LocalDocQA
|
from chains.local_doc_qa import LocalDocQA
|
||||||
from configs.model_config import *
|
from configs.model_config import *
|
||||||
import nltk
|
import nltk
|
||||||
import uuid
|
|
||||||
|
|
||||||
nltk.data.path = [NLTK_DATA_PATH] + nltk.data.path
|
nltk.data.path = [NLTK_DATA_PATH] + nltk.data.path
|
||||||
|
|
||||||
|
|
@ -27,9 +26,7 @@ llm_model_dict_list = list(llm_model_dict.keys())
|
||||||
|
|
||||||
local_doc_qa = LocalDocQA()
|
local_doc_qa = LocalDocQA()
|
||||||
|
|
||||||
logger = gr.CSVLogger()
|
flag_csv_logger = gr.CSVLogger()
|
||||||
username = uuid.uuid4().hex
|
|
||||||
|
|
||||||
|
|
||||||
def get_answer(query, vs_path, history, mode,
|
def get_answer(query, vs_path, history, mode,
|
||||||
streaming: bool = STREAMING):
|
streaming: bool = STREAMING):
|
||||||
|
|
@ -54,23 +51,24 @@ def get_answer(query, vs_path, history, mode,
|
||||||
history[-1][-1] = resp + (
|
history[-1][-1] = resp + (
|
||||||
"\n\n当前知识库为空,如需基于知识库进行问答,请先加载知识库后,再进行提问。" if mode == "知识库问答" else "")
|
"\n\n当前知识库为空,如需基于知识库进行问答,请先加载知识库后,再进行提问。" if mode == "知识库问答" else "")
|
||||||
yield history, ""
|
yield history, ""
|
||||||
logger.flag([query, vs_path, history, mode], username=username)
|
logger.info(f"flagging: username={FLAG_USER_NAME},query={query},vs_path={vs_path},mode={mode},history={history}")
|
||||||
|
flag_csv_logger.flag([query, vs_path, history, mode], username=FLAG_USER_NAME)
|
||||||
|
|
||||||
def init_model():
|
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("你好")
|
||||||
reply = """模型已成功加载,可以开始对话,或从右侧选择模式后开始对话"""
|
reply = """模型已成功加载,可以开始对话,或从右侧选择模式后开始对话"""
|
||||||
print(reply)
|
logger.info(reply)
|
||||||
return reply
|
return reply
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
logger.error(e)
|
||||||
reply = """模型未成功加载,请到页面左上角"模型配置"选项卡中重新选择后点击"加载模型"按钮"""
|
reply = """模型未成功加载,请到页面左上角"模型配置"选项卡中重新选择后点击"加载模型"按钮"""
|
||||||
if str(e) == "Unknown platform: darwin":
|
if str(e) == "Unknown platform: darwin":
|
||||||
print("该报错可能因为您使用的是 macOS 操作系统,需先下载模型至本地后执行 Web UI,具体方法请参考项目 README 中本地部署方法及常见问题:"
|
logger.info("该报错可能因为您使用的是 macOS 操作系统,需先下载模型至本地后执行 Web UI,具体方法请参考项目 README 中本地部署方法及常见问题:"
|
||||||
" https://github.com/imClumsyPanda/langchain-ChatGLM")
|
" https://github.com/imClumsyPanda/langchain-ChatGLM")
|
||||||
else:
|
else:
|
||||||
print(reply)
|
logger.info(reply)
|
||||||
return reply
|
return reply
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -83,11 +81,11 @@ def reinit_model(llm_model, embedding_model, llm_history_len, use_ptuning_v2, us
|
||||||
use_lora=use_lora,
|
use_lora=use_lora,
|
||||||
top_k=top_k, )
|
top_k=top_k, )
|
||||||
model_status = """模型已成功重新加载,可以开始对话,或从右侧选择模式后开始对话"""
|
model_status = """模型已成功重新加载,可以开始对话,或从右侧选择模式后开始对话"""
|
||||||
print(model_status)
|
logger.info(model_status)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
logger.error(e)
|
||||||
model_status = """模型未成功重新加载,请到页面左上角"模型配置"选项卡中重新选择后点击"加载模型"按钮"""
|
model_status = """模型未成功重新加载,请到页面左上角"模型配置"选项卡中重新选择后点击"加载模型"按钮"""
|
||||||
print(model_status)
|
logger.info(model_status)
|
||||||
return history + [[None, model_status]]
|
return history + [[None, model_status]]
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -109,7 +107,7 @@ def get_vector_store(vs_id, files, history):
|
||||||
else:
|
else:
|
||||||
file_status = "模型未完成加载,请先在加载模型后再导入文件"
|
file_status = "模型未完成加载,请先在加载模型后再导入文件"
|
||||||
vs_path = None
|
vs_path = None
|
||||||
print(file_status)
|
logger.info(file_status)
|
||||||
return vs_path, None, history + [[None, file_status]]
|
return vs_path, None, history + [[None, file_status]]
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -235,7 +233,7 @@ with gr.Blocks(css=block_css) as demo:
|
||||||
inputs=[select_vs, folder_files, chatbot],
|
inputs=[select_vs, folder_files, chatbot],
|
||||||
outputs=[vs_path, folder_files, chatbot],
|
outputs=[vs_path, folder_files, chatbot],
|
||||||
)
|
)
|
||||||
logger.setup([query, vs_path, chatbot, mode], "flagged")
|
flag_csv_logger.setup([query, vs_path, chatbot, mode], "flagged")
|
||||||
query.submit(get_answer,
|
query.submit(get_answer,
|
||||||
[query, vs_path, chatbot, mode],
|
[query, vs_path, chatbot, mode],
|
||||||
[chatbot, query])
|
[chatbot, query])
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue