-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolbert_helper.py
63 lines (44 loc) · 1.97 KB
/
colbert_helper.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
60
61
62
63
'''
Colbert indexing and searching
'''
import sys;
sys.path.insert(0, '/home/zoukos/ceid/Information_Retrieval/ColBERT/')
from colbert import Indexer, Searcher
from colbert.infra import Run, RunConfig, ColBERTConfig
#from colbert.data import Queries, Collection
#==============================================================================================
checkpoint = "colbertv2.0"
doc_maxlen = 512
nbits = 2
index_name = "index_1"
nranks = 1 #Number of GPUs
kmeans_niters = 4 #Number of iterations of k-means clustering
#==============================================================================================
def create_index(docs_dataset):
'''
Creates an index from the input dataset.
Accepts a dataset created with load_dataset containing all the documents.
'''
global index_name
global nbits
global doc_maxlen
global checkpoint
with Run().context(RunConfig(nranks=1)):
config = ColBERTConfig(doc_maxlen=doc_maxlen, nbits=nbits, kmeans_niters=4)
indexer = Indexer(checkpoint=checkpoint, config=config)
indexer.index(name=index_name, collection=docs_dataset["text"], overwrite=True)
print("PATH: ", indexer.get_index())
#==============================================================================================
def get_searcher(docs_dataset):
global index_name
searcher = None
with Run().context(RunConfig()):
searcher = Searcher(index=index_name, collection=docs_dataset["text"])
return searcher
#==============================================================================================
def search(searcher, docs_dataset, query, num_results: int):
search_results = searcher.search(query, k=num_results)[0]
#Maps the ids returned by colbert to our own ids
#Colbert ids are in search_results
#Our ids are stored in the dataset under "doc"
return list(map(lambda passage_id: docs_dataset[passage_id]["doc"], search_results))