Langchain-Chatchat/chains/local_doc_qa.py

157 lines
6.2 KiB
Python
Raw Normal View History

2023-04-13 23:01:52 +08:00
from langchain.chains import RetrievalQA
from langchain.prompts import PromptTemplate
2023-04-25 20:36:16 +08:00
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS
2023-04-13 23:01:52 +08:00
from langchain.document_loaders import UnstructuredFileLoader
from models.chatglm_llm import ChatGLM
import sentence_transformers
import os
from configs.model_config import *
import datetime
2023-04-14 00:42:21 +08:00
from typing import List
2023-04-17 23:59:22 +08:00
from textsplitter import ChineseTextSplitter
2023-04-25 20:36:16 +08:00
from langchain.docstore.document import Document
2023-04-13 23:01:52 +08:00
# return top-k text chunk from vector store
VECTOR_SEARCH_TOP_K = 6
2023-04-13 23:01:52 +08:00
# LLM input history length
LLM_HISTORY_LEN = 3
2023-04-17 23:59:22 +08:00
def load_file(filepath):
2023-04-25 20:36:16 +08:00
if filepath.lower().endswith(".md"):
loader = UnstructuredFileLoader(filepath, mode="elements")
docs = loader.load()
elif filepath.lower().endswith(".pdf"):
2023-04-17 23:59:22 +08:00
loader = UnstructuredFileLoader(filepath)
textsplitter = ChineseTextSplitter(pdf=True)
docs = loader.load_and_split(textsplitter)
else:
loader = UnstructuredFileLoader(filepath, mode="elements")
textsplitter = ChineseTextSplitter(pdf=False)
docs = loader.load_and_split(text_splitter=textsplitter)
return docs
2023-04-26 22:29:20 +08:00
def generate_prompt(related_docs: List[str],
query: str,
prompt_template=PROMPT_TEMPLATE) -> str:
context = "\n".join([doc.page_content for doc in related_docs])
prompt = prompt_template.replace("{question}", query).replace("{context}", context)
return prompt
2023-04-25 20:36:16 +08:00
2023-04-26 22:29:20 +08:00
def get_docs_with_score(docs_with_score):
docs=[]
for doc, score in docs_with_score:
doc.metadata["score"] = score
docs.append(doc)
return docs
2023-04-25 20:36:16 +08:00
2023-04-13 23:01:52 +08:00
class LocalDocQA:
llm: object = None
embeddings: object = None
top_k: int = VECTOR_SEARCH_TOP_K
2023-04-13 23:01:52 +08:00
def init_cfg(self,
embedding_model: str = EMBEDDING_MODEL,
embedding_device=EMBEDDING_DEVICE,
llm_history_len: int = LLM_HISTORY_LEN,
llm_model: str = LLM_MODEL,
2023-04-14 00:06:45 +08:00
llm_device=LLM_DEVICE,
top_k=VECTOR_SEARCH_TOP_K,
2023-04-20 12:50:51 +08:00
use_ptuning_v2: bool = USE_PTUNING_V2
2023-04-13 23:01:52 +08:00
):
self.llm = ChatGLM()
self.llm.load_model(model_name_or_path=llm_model_dict[llm_model],
2023-04-20 12:50:51 +08:00
llm_device=llm_device,
use_ptuning_v2=use_ptuning_v2)
2023-04-13 23:01:52 +08:00
self.llm.history_len = llm_history_len
2023-04-25 20:36:16 +08:00
self.embeddings = HuggingFaceEmbeddings(model_name=embedding_model_dict[embedding_model],
model_kwargs={'device': embedding_device})
2023-04-14 00:06:45 +08:00
self.top_k = top_k
2023-04-13 23:01:52 +08:00
def init_knowledge_vector_store(self,
2023-04-19 21:29:20 +08:00
filepath: str or List[str],
vs_path: str or os.PathLike = None):
loaded_files = []
2023-04-14 00:42:21 +08:00
if isinstance(filepath, str):
if not os.path.exists(filepath):
print("路径不存在")
2023-04-13 23:01:52 +08:00
return None
2023-04-14 00:42:21 +08:00
elif os.path.isfile(filepath):
file = os.path.split(filepath)[-1]
try:
2023-04-17 23:59:22 +08:00
docs = load_file(filepath)
2023-04-14 00:42:21 +08:00
print(f"{file} 已成功加载")
2023-04-19 21:29:20 +08:00
loaded_files.append(filepath)
2023-04-17 23:59:22 +08:00
except Exception as e:
print(e)
2023-04-14 00:42:21 +08:00
print(f"{file} 未能成功加载")
return None
elif os.path.isdir(filepath):
docs = []
for file in os.listdir(filepath):
fullfilepath = os.path.join(filepath, file)
try:
2023-04-17 23:59:22 +08:00
docs += load_file(fullfilepath)
2023-04-14 00:42:21 +08:00
print(f"{file} 已成功加载")
2023-04-19 21:29:20 +08:00
loaded_files.append(fullfilepath)
2023-04-17 23:59:22 +08:00
except Exception as e:
print(e)
2023-04-14 00:42:21 +08:00
print(f"{file} 未能成功加载")
else:
2023-04-13 23:01:52 +08:00
docs = []
2023-04-14 00:42:21 +08:00
for file in filepath:
2023-04-13 23:01:52 +08:00
try:
2023-04-17 23:59:22 +08:00
docs += load_file(file)
2023-04-13 23:01:52 +08:00
print(f"{file} 已成功加载")
2023-04-19 21:29:20 +08:00
loaded_files.append(file)
2023-04-17 23:59:22 +08:00
except Exception as e:
print(e)
2023-04-13 23:01:52 +08:00
print(f"{file} 未能成功加载")
if len(docs) > 0:
if vs_path and os.path.isdir(vs_path):
2023-04-25 20:36:16 +08:00
vector_store = FAISS.load_local(vs_path, self.embeddings)
vector_store.add_documents(docs)
else:
if not vs_path:
vs_path = f"""{VS_ROOT_PATH}{os.path.splitext(file)[0]}_FAISS_{datetime.datetime.now().strftime("%Y%m%d_%H%M%S")}"""
2023-04-25 20:36:16 +08:00
vector_store = FAISS.from_documents(docs, self.embeddings)
2023-04-13 23:01:52 +08:00
vector_store.save_local(vs_path)
return vs_path, loaded_files
2023-04-19 21:29:20 +08:00
else:
print("文件均未成功加载,请检查依赖包或替换为其他文件再次上传。")
return None, loaded_files
2023-04-13 23:01:52 +08:00
def get_knowledge_based_answer(self,
query,
vs_path,
2023-04-26 22:29:20 +08:00
chat_history=[],
streaming=True):
self.llm.streaming = streaming
2023-04-25 20:36:16 +08:00
vector_store = FAISS.load_local(vs_path, self.embeddings)
2023-04-26 22:29:20 +08:00
related_docs_with_score = vector_store.similarity_search_with_score(query,
k=self.top_k)
related_docs = get_docs_with_score(related_docs_with_score)
prompt = generate_prompt(related_docs, query)
2023-04-13 23:01:52 +08:00
2023-04-26 22:29:20 +08:00
if streaming:
for result, history in self.llm._call(prompt=prompt,
history=chat_history):
history[-1][0] = query
response = {"query": query,
"result": result,
"source_documents": related_docs}
yield response, history
else:
result, history = self.llm._call(prompt=prompt,
history=chat_history)
history[-1][0] = query
response = {"query": query,
"result": result,
"source_documents": related_docs}
return response, history