forked from dawoshi/Tianchi-LLM-QA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaiss_retriever.py
executable file
·59 lines (53 loc) · 2.11 KB
/
faiss_retriever.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
# coding: utf-8
from langchain.schema import Document
from langchain.vectorstores import Chroma,FAISS
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
from pdf_parse import DataProcess
import torch
# from bm25_retriever import BM25
class FaissRetriever(object):
def __init__(self, model_path, data):
self.embeddings = HuggingFaceEmbeddings(
model_name = model_path,
model_kwargs = {"device":"cuda"}
)
docs = []
for idx, line in enumerate(data):
line = line.strip("\n").strip()
words = line.split("\t")
docs.append(Document(page_content=words[0], metadata={"id": idx}))
self.vector_store = FAISS.from_documents(docs, self.embeddings)
del self.embeddings
torch.cuda.empty_cache()
def GetTopK(self, query, k):
context = self.vector_store.similarity_search_with_score(query, k=k)
return context
def GetvectorStore(self):
return self.vector_store
if __name__ == "__main__":
base = "/root/autodl-tmp/codes"
model_name=base + "/pre_train_model/m3e-large" #text2vec-large-chinese
dp = DataProcess(pdf_path = base + "/data/train_a.pdf")
dp.ParseBlock(max_seq = 1024)
dp.ParseBlock(max_seq = 512)
print(len(dp.data))
dp.ParseAllPage(max_seq = 256)
dp.ParseAllPage(max_seq = 512)
print(len(dp.data))
dp.ParseOnePageWithRule(max_seq = 256)
dp.ParseOnePageWithRule(max_seq = 512)
print(len(dp.data))
data = dp.data
faissretriever = FaissRetriever(model_name, data)
# bm25 = BM25(data)
faiss_ans = faissretriever.GetTopK("如何预防新冠肺炎", 6)
print(faiss_ans)
faiss_ans = faissretriever.GetTopK("交通事故如何处理", 6)
print(faiss_ans)
faiss_ans = faissretriever.GetTopK("吉利集团的董事长是谁", 6)
print(faiss_ans)
faiss_ans = faissretriever.GetTopK("吉利汽车语音组手叫什么", 6)
print(faiss_ans)
# bm25_ans = bm25.GetBM25TopK("座椅加热", 6)
# ans = reRank(6, bm25_ans, faiss_ans)