Oleh Shliazhko
commited on
Commit
•
3b40a03
1
Parent(s):
365eb64
update retriever
Browse files- wiki_mpnet_index.py +7 -7
wiki_mpnet_index.py
CHANGED
@@ -8,24 +8,24 @@ from sentence_transformers import SentenceTransformer
|
|
8 |
|
9 |
class Retriever:
|
10 |
def __init__(self):
|
|
|
11 |
self.index = faiss.read_index("data/en/embs_IVF16384_HNSW32_2lvl_full.idx")
|
12 |
self.index.nprobe = 128
|
13 |
self.model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2', device='cuda')
|
14 |
self.model.max_seq_length = 512
|
15 |
-
|
16 |
-
|
17 |
def get_paragraph_by_vec_idx(self, vec_idx):
|
18 |
-
archive = zipfile.ZipFile('data/en/paragraphs.zip', 'r')
|
19 |
chunk_id = vec_idx // 100000
|
20 |
line_id = vec_idx % 100000
|
21 |
-
with archive.open('enwiki_paragraphs_clean/enwiki_paragraphs_%03d.jsonl' % chunk_id) as f:
|
22 |
for i,l in enumerate(f):
|
23 |
if i == line_id:
|
24 |
data = json.loads(l)
|
25 |
break
|
26 |
return data
|
27 |
-
|
28 |
-
|
29 |
def search(self, query, k=5):
|
30 |
emb = self.model.encode(query)
|
31 |
_, neighbors = self.index.search(emb[np.newaxis, ...], k)
|
@@ -33,4 +33,4 @@ class Retriever:
|
|
33 |
for n in neighbors[0]:
|
34 |
data = get_paragraph_by_vec_idx(n)
|
35 |
results.append(data)
|
36 |
-
return results
|
|
|
8 |
|
9 |
class Retriever:
|
10 |
def __init__(self):
|
11 |
+
self.archive = zipfile.ZipFile('data/en/paragraphs.zip', 'r')
|
12 |
self.index = faiss.read_index("data/en/embs_IVF16384_HNSW32_2lvl_full.idx")
|
13 |
self.index.nprobe = 128
|
14 |
self.model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2', device='cuda')
|
15 |
self.model.max_seq_length = 512
|
16 |
+
|
17 |
+
|
18 |
def get_paragraph_by_vec_idx(self, vec_idx):
|
|
|
19 |
chunk_id = vec_idx // 100000
|
20 |
line_id = vec_idx % 100000
|
21 |
+
with self.archive.open('enwiki_paragraphs_clean/enwiki_paragraphs_%03d.jsonl' % chunk_id) as f:
|
22 |
for i,l in enumerate(f):
|
23 |
if i == line_id:
|
24 |
data = json.loads(l)
|
25 |
break
|
26 |
return data
|
27 |
+
|
28 |
+
|
29 |
def search(self, query, k=5):
|
30 |
emb = self.model.encode(query)
|
31 |
_, neighbors = self.index.search(emb[np.newaxis, ...], k)
|
|
|
33 |
for n in neighbors[0]:
|
34 |
data = get_paragraph_by_vec_idx(n)
|
35 |
results.append(data)
|
36 |
+
return results
|