|
@@ -8,6 +8,7 @@ from textsplitter import ChineseTextSplitter
|
|
from typing import List, Tuple
|
|
from typing import List, Tuple
|
|
from langchain.docstore.document import Document
|
|
from langchain.docstore.document import Document
|
|
import numpy as np
|
|
import numpy as np
|
|
|
|
+from utils import torch_gc
|
|
|
|
|
|
# return top-k text chunk from vector store
|
|
# return top-k text chunk from vector store
|
|
VECTOR_SEARCH_TOP_K = 6
|
|
VECTOR_SEARCH_TOP_K = 6
|
|
@@ -15,6 +16,10 @@ VECTOR_SEARCH_TOP_K = 6
|
|
# LLM input history length
|
|
# LLM input history length
|
|
LLM_HISTORY_LEN = 3
|
|
LLM_HISTORY_LEN = 3
|
|
|
|
|
|
|
|
+DEVICE_ = EMBEDDING_DEVICE
|
|
|
|
+DEVICE_ID = "0" if torch.cuda.is_available() else None
|
|
|
|
+DEVICE = f"{DEVICE_}:{DEVICE_ID}" if DEVICE_ID else DEVICE_
|
|
|
|
+
|
|
|
|
|
|
def load_file(filepath):
|
|
def load_file(filepath):
|
|
if filepath.lower().endswith(".md"):
|
|
if filepath.lower().endswith(".md"):
|
|
@@ -30,6 +35,7 @@ def load_file(filepath):
|
|
docs = loader.load_and_split(text_splitter=textsplitter)
|
|
docs = loader.load_and_split(text_splitter=textsplitter)
|
|
return docs
|
|
return docs
|
|
|
|
|
|
|
|
+
|
|
def generate_prompt(related_docs: List[str],
|
|
def generate_prompt(related_docs: List[str],
|
|
query: str,
|
|
query: str,
|
|
prompt_template=PROMPT_TEMPLATE) -> str:
|
|
prompt_template=PROMPT_TEMPLATE) -> str:
|
|
@@ -39,7 +45,7 @@ def generate_prompt(related_docs: List[str],
|
|
|
|
|
|
|
|
|
|
def get_docs_with_score(docs_with_score):
|
|
def get_docs_with_score(docs_with_score):
|
|
- docs=[]
|
|
|
|
|
|
+ docs = []
|
|
for doc, score in docs_with_score:
|
|
for doc, score in docs_with_score:
|
|
doc.metadata["score"] = score
|
|
doc.metadata["score"] = score
|
|
docs.append(doc)
|
|
docs.append(doc)
|
|
@@ -50,7 +56,7 @@ def seperate_list(ls: List[int]) -> List[List[int]]:
|
|
lists = []
|
|
lists = []
|
|
ls1 = [ls[0]]
|
|
ls1 = [ls[0]]
|
|
for i in range(1, len(ls)):
|
|
for i in range(1, len(ls)):
|
|
- if ls[i-1] + 1 == ls[i]:
|
|
|
|
|
|
+ if ls[i - 1] + 1 == ls[i]:
|
|
ls1.append(ls[i])
|
|
ls1.append(ls[i])
|
|
else:
|
|
else:
|
|
lists.append(ls1)
|
|
lists.append(ls1)
|
|
@@ -59,49 +65,48 @@ def seperate_list(ls: List[int]) -> List[List[int]]:
|
|
return lists
|
|
return lists
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
def similarity_search_with_score_by_vector(
|
|
def similarity_search_with_score_by_vector(
|
|
self,
|
|
self,
|
|
embedding: List[float],
|
|
embedding: List[float],
|
|
k: int = 4,
|
|
k: int = 4,
|
|
- ) -> List[Tuple[Document, float]]:
|
|
|
|
- scores, indices = self.index.search(np.array([embedding], dtype=np.float32), k)
|
|
|
|
- docs = []
|
|
|
|
- id_set = set()
|
|
|
|
- for j, i in enumerate(indices[0]):
|
|
|
|
- if i == -1:
|
|
|
|
- # This happens when not enough docs are returned.
|
|
|
|
- continue
|
|
|
|
- _id = self.index_to_docstore_id[i]
|
|
|
|
- doc = self.docstore.search(_id)
|
|
|
|
- id_set.add(i)
|
|
|
|
- docs_len = len(doc.page_content)
|
|
|
|
- for k in range(1, max(i, len(docs)-i)):
|
|
|
|
- for l in [i+k, i-k]:
|
|
|
|
- if 0 <= l < len(self.index_to_docstore_id):
|
|
|
|
- _id0 = self.index_to_docstore_id[l]
|
|
|
|
- doc0 = self.docstore.search(_id0)
|
|
|
|
- if docs_len + len(doc0.page_content) > self.chunk_size:
|
|
|
|
- break
|
|
|
|
- elif doc0.metadata["source"] == doc.metadata["source"]:
|
|
|
|
- docs_len += len(doc0.page_content)
|
|
|
|
- id_set.add(l)
|
|
|
|
- id_list = sorted(list(id_set))
|
|
|
|
- id_lists = seperate_list(id_list)
|
|
|
|
- for id_seq in id_lists:
|
|
|
|
- for id in id_seq:
|
|
|
|
- if id == id_seq[0]:
|
|
|
|
- _id = self.index_to_docstore_id[id]
|
|
|
|
- doc = self.docstore.search(_id)
|
|
|
|
- else:
|
|
|
|
- _id0 = self.index_to_docstore_id[id]
|
|
|
|
|
|
+) -> List[Tuple[Document, float]]:
|
|
|
|
+ scores, indices = self.index.search(np.array([embedding], dtype=np.float32), k)
|
|
|
|
+ docs = []
|
|
|
|
+ id_set = set()
|
|
|
|
+ for j, i in enumerate(indices[0]):
|
|
|
|
+ if i == -1:
|
|
|
|
+ # This happens when not enough docs are returned.
|
|
|
|
+ continue
|
|
|
|
+ _id = self.index_to_docstore_id[i]
|
|
|
|
+ doc = self.docstore.search(_id)
|
|
|
|
+ id_set.add(i)
|
|
|
|
+ docs_len = len(doc.page_content)
|
|
|
|
+ for k in range(1, max(i, len(docs) - i)):
|
|
|
|
+ for l in [i + k, i - k]:
|
|
|
|
+ if 0 <= l < len(self.index_to_docstore_id):
|
|
|
|
+ _id0 = self.index_to_docstore_id[l]
|
|
doc0 = self.docstore.search(_id0)
|
|
doc0 = self.docstore.search(_id0)
|
|
- doc.page_content += doc0.page_content
|
|
|
|
- if not isinstance(doc, Document):
|
|
|
|
- raise ValueError(f"Could not find document for id {_id}, got {doc}")
|
|
|
|
- docs.append((doc, scores[0][j]))
|
|
|
|
- return docs
|
|
|
|
-
|
|
|
|
|
|
+ if docs_len + len(doc0.page_content) > self.chunk_size:
|
|
|
|
+ break
|
|
|
|
+ elif doc0.metadata["source"] == doc.metadata["source"]:
|
|
|
|
+ docs_len += len(doc0.page_content)
|
|
|
|
+ id_set.add(l)
|
|
|
|
+ id_list = sorted(list(id_set))
|
|
|
|
+ id_lists = seperate_list(id_list)
|
|
|
|
+ for id_seq in id_lists:
|
|
|
|
+ for id in id_seq:
|
|
|
|
+ if id == id_seq[0]:
|
|
|
|
+ _id = self.index_to_docstore_id[id]
|
|
|
|
+ doc = self.docstore.search(_id)
|
|
|
|
+ else:
|
|
|
|
+ _id0 = self.index_to_docstore_id[id]
|
|
|
|
+ doc0 = self.docstore.search(_id0)
|
|
|
|
+ doc.page_content += doc0.page_content
|
|
|
|
+ if not isinstance(doc, Document):
|
|
|
|
+ raise ValueError(f"Could not find document for id {_id}, got {doc}")
|
|
|
|
+ docs.append((doc, scores[0][j]))
|
|
|
|
+ torch_gc(DEVICE)
|
|
|
|
+ return docs
|
|
|
|
|
|
|
|
|
|
class LocalDocQA:
|
|
class LocalDocQA:
|
|
@@ -116,12 +121,10 @@ class LocalDocQA:
|
|
llm_history_len: int = LLM_HISTORY_LEN,
|
|
llm_history_len: int = LLM_HISTORY_LEN,
|
|
llm_model: str = LLM_MODEL,
|
|
llm_model: str = LLM_MODEL,
|
|
llm_device=LLM_DEVICE,
|
|
llm_device=LLM_DEVICE,
|
|
- streaming=STREAMING,
|
|
|
|
top_k=VECTOR_SEARCH_TOP_K,
|
|
top_k=VECTOR_SEARCH_TOP_K,
|
|
use_ptuning_v2: bool = USE_PTUNING_V2
|
|
use_ptuning_v2: bool = USE_PTUNING_V2
|
|
):
|
|
):
|
|
self.llm = ChatGLM()
|
|
self.llm = ChatGLM()
|
|
- self.llm.streaming = streaming
|
|
|
|
self.llm.load_model(model_name_or_path=llm_model_dict[llm_model],
|
|
self.llm.load_model(model_name_or_path=llm_model_dict[llm_model],
|
|
llm_device=llm_device,
|
|
llm_device=llm_device,
|
|
use_ptuning_v2=use_ptuning_v2)
|
|
use_ptuning_v2=use_ptuning_v2)
|
|
@@ -174,10 +177,12 @@ class LocalDocQA:
|
|
if vs_path and os.path.isdir(vs_path):
|
|
if vs_path and os.path.isdir(vs_path):
|
|
vector_store = FAISS.load_local(vs_path, self.embeddings)
|
|
vector_store = FAISS.load_local(vs_path, self.embeddings)
|
|
vector_store.add_documents(docs)
|
|
vector_store.add_documents(docs)
|
|
|
|
+ torch_gc(DEVICE)
|
|
else:
|
|
else:
|
|
if not vs_path:
|
|
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")}"""
|
|
vs_path = f"""{VS_ROOT_PATH}{os.path.splitext(file)[0]}_FAISS_{datetime.datetime.now().strftime("%Y%m%d_%H%M%S")}"""
|
|
vector_store = FAISS.from_documents(docs, self.embeddings)
|
|
vector_store = FAISS.from_documents(docs, self.embeddings)
|
|
|
|
+ torch_gc(DEVICE)
|
|
|
|
|
|
vector_store.save_local(vs_path)
|
|
vector_store.save_local(vs_path)
|
|
return vs_path, loaded_files
|
|
return vs_path, loaded_files
|
|
@@ -188,28 +193,50 @@ class LocalDocQA:
|
|
def get_knowledge_based_answer(self,
|
|
def get_knowledge_based_answer(self,
|
|
query,
|
|
query,
|
|
vs_path,
|
|
vs_path,
|
|
- chat_history=[]):
|
|
|
|
|
|
+ chat_history=[],
|
|
|
|
+ streaming: bool = STREAMING):
|
|
vector_store = FAISS.load_local(vs_path, self.embeddings)
|
|
vector_store = FAISS.load_local(vs_path, self.embeddings)
|
|
FAISS.similarity_search_with_score_by_vector = similarity_search_with_score_by_vector
|
|
FAISS.similarity_search_with_score_by_vector = similarity_search_with_score_by_vector
|
|
- vector_store.chunk_size=self.chunk_size
|
|
|
|
|
|
+ vector_store.chunk_size = self.chunk_size
|
|
related_docs_with_score = vector_store.similarity_search_with_score(query,
|
|
related_docs_with_score = vector_store.similarity_search_with_score(query,
|
|
k=self.top_k)
|
|
k=self.top_k)
|
|
related_docs = get_docs_with_score(related_docs_with_score)
|
|
related_docs = get_docs_with_score(related_docs_with_score)
|
|
prompt = generate_prompt(related_docs, query)
|
|
prompt = generate_prompt(related_docs, query)
|
|
|
|
|
|
- if self.llm.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)
|
|
|
|
|
|
+ # if streaming:
|
|
|
|
+ # for result, history in self.llm._stream_call(prompt=prompt,
|
|
|
|
+ # history=chat_history):
|
|
|
|
+ # history[-1][0] = query
|
|
|
|
+ # response = {"query": query,
|
|
|
|
+ # "result": result,
|
|
|
|
+ # "source_documents": related_docs}
|
|
|
|
+ # yield response, history
|
|
|
|
+ # else:
|
|
|
|
+ for result, history in self.llm._call(prompt=prompt,
|
|
|
|
+ history=chat_history,
|
|
|
|
+ streaming=streaming):
|
|
history[-1][0] = query
|
|
history[-1][0] = query
|
|
response = {"query": query,
|
|
response = {"query": query,
|
|
"result": result,
|
|
"result": result,
|
|
"source_documents": related_docs}
|
|
"source_documents": related_docs}
|
|
- return response, history
|
|
|
|
|
|
+ yield response, history
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+if __name__ == "__main__":
|
|
|
|
+ local_doc_qa = LocalDocQA()
|
|
|
|
+ local_doc_qa.init_cfg()
|
|
|
|
+ query = "你好"
|
|
|
|
+ vs_path = "/Users/liuqian/Downloads/glm-dev/vector_store/123"
|
|
|
|
+ last_print_len = 0
|
|
|
|
+ for resp, history in local_doc_qa.get_knowledge_based_answer(query=query,
|
|
|
|
+ vs_path=vs_path,
|
|
|
|
+ chat_history=[],
|
|
|
|
+ streaming=True):
|
|
|
|
+ print(resp["result"][last_print_len:], end="", flush=True)
|
|
|
|
+ last_print_len = len(resp["result"])
|
|
|
|
+ for resp, history in local_doc_qa.get_knowledge_based_answer(query=query,
|
|
|
|
+ vs_path=vs_path,
|
|
|
|
+ chat_history=[],
|
|
|
|
+ streaming=False):
|
|
|
|
+ print(resp["result"])
|
|
|
|
+ pass
|