nreimers commited on
Commit
a318cb0
1 Parent(s): 2c9e266
README.md ADDED
@@ -0,0 +1,243 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ configs:
3
+ - config_name: hotpotqa-corpus
4
+ data_files:
5
+ - split: train
6
+ path: hotpotqa/corpus/*
7
+ - config_name: hotpotqa-queries
8
+ data_files:
9
+ - split: train
10
+ path: hotpotqa/queries/train.parquet
11
+ - split: dev
12
+ path: hotpotqa/queries/dev.parquet
13
+ - split: test
14
+ path: hotpotqa/queries/test.parquet
15
+ - config_name: hotpotqa-qrels
16
+ data_files:
17
+ - split: train
18
+ path: hotpotqa/qrels/train.parquet
19
+ - split: dev
20
+ path: hotpotqa/qrels/dev.parquet
21
+ - split: test
22
+ path: hotpotqa/qrels/test.parquet
23
+ - config_name: msmarco-corpus
24
+ data_files:
25
+ - split: train
26
+ path: msmarco/corpus/*
27
+ - config_name: msmarco-queries
28
+ data_files:
29
+ - split: train
30
+ path: msmarco/queries/train.parquet
31
+ - split: dev
32
+ path: msmarco/queries/dev.parquet
33
+ - config_name: msmarco-qrels
34
+ data_files:
35
+ - split: train
36
+ path: msmarco/qrels/train.parquet
37
+ - split: dev
38
+ path: msmarco/qrels/dev.parquet
39
+ - config_name: nfcorpus-corpus
40
+ data_files:
41
+ - split: train
42
+ path: nfcorpus/corpus/*
43
+ - config_name: nfcorpus-queries
44
+ data_files:
45
+ - split: train
46
+ path: nfcorpus/queries/train.parquet
47
+ - split: dev
48
+ path: nfcorpus/queries/dev.parquet
49
+ - split: test
50
+ path: nfcorpus/queries/test.parquet
51
+ - config_name: nfcorpus-qrels
52
+ data_files:
53
+ - split: train
54
+ path: nfcorpus/qrels/train.parquet
55
+ - split: dev
56
+ path: nfcorpus/qrels/dev.parquet
57
+ - split: test
58
+ path: nfcorpus/qrels/test.parquet
59
+ ---
60
+
61
+ # BEIR embeddings with Cohere embed-english-v3.0 model
62
+
63
+ This datasets contains all query & document embeddings for [BEIR](https://github.com/beir-cellar/beir), embedded with the [Cohere embed-english-v3.0](https://huggingface.co/Cohere/Cohere-embed-english-v3.0) embedding model.
64
+
65
+
66
+ ## Loading the dataset
67
+
68
+ ### Loading the document embeddings
69
+ The `corpus` split contains all document embeddings of the corpus.
70
+
71
+ You can either load the dataset like this:
72
+ ```python
73
+ from datasets import load_dataset
74
+ dataset_name = "hotpotqa"
75
+ docs = load_dataset("Cohere/beir-embed-english-v3", f"{dataset_name}-corpus", split="train")
76
+ ```
77
+
78
+ Or you can also stream it without downloading it before:
79
+ ```python
80
+ from datasets import load_dataset
81
+ dataset_name = "hotpotqa"
82
+ docs = load_dataset("Cohere/beir-embed-english-v3", f"{dataset_name}-corpus", split="train", streaming=True)
83
+ for doc in docs:
84
+ doc_id = doc['_id']
85
+ title = doc['title']
86
+ text = doc['text']
87
+ emb = doc['emb']
88
+ ```
89
+
90
+ Note, depending on the dataset size, the corpus split can be quite large.
91
+
92
+ ### Loading the query embeddings
93
+ The `queries` split contains all query embeddings. There might be up to three splits: `train`, `dev`, and `test`, depending which splits are available in BEIR. Evaluation is performed on the `test` split.
94
+
95
+ You can load the dataset like this:
96
+ ```python
97
+ from datasets import load_dataset
98
+ dataset_name = "hotpotqa"
99
+ queries = load_dataset("Cohere/beir-embed-english-v3", f"{dataset_name}-queries", split="test")
100
+
101
+ for query in queries:
102
+ query_id = query['_id']
103
+ text = query['text']
104
+ emb = query['emb']
105
+ ```
106
+
107
+
108
+ ### Loading the qrels
109
+
110
+ The `qrels` split contains the query relevance annotation, i.e., it contains the relevance score for (query, document) pairs.
111
+
112
+
113
+ You can load the dataset like this:
114
+ ```python
115
+ from datasets import load_dataset
116
+ dataset_name = "hotpotqa"
117
+ qrels = load_dataset("Cohere/beir-embed-english-v3", f"{dataset_name}-qrels", split="test")
118
+
119
+ for qrel in qrels:
120
+ query_id = qrel['query_id']
121
+ corpus_id = qrel['corpus_id']
122
+ score = qrel['score']
123
+ ```
124
+
125
+ ## Search
126
+ The following shows an example, how the dataset can be used to build a semantic search application.
127
+
128
+ Get your API key from [cohere.com](https://cohere.com) and start using this dataset.
129
+
130
+ ```python
131
+ #Run: pip install cohere datasets torch
132
+ from datasets import load_dataset
133
+ import torch
134
+ import cohere
135
+ dataset_name = "hotpotqa"
136
+ co = cohere.Client("<<COHERE_API_KEY>>") # Add your cohere API key from www.cohere.com
137
+
138
+ #Load at max 1000 documents + embeddings
139
+ max_docs = 1000
140
+ docs_stream = load_dataset("Cohere/beir-embed-english-v3", f"{dataset_name}-corpus", split="train", streaming=True)
141
+ docs = []
142
+ doc_embeddings = []
143
+ for doc in docs_stream:
144
+ docs.append(doc)
145
+ doc_embeddings.append(doc['emb'])
146
+ if len(docs) >= max_docs:
147
+ break
148
+
149
+ doc_embeddings = torch.tensor(doc_embeddings)
150
+
151
+ query = 'What is an abstract' #Your query
152
+ response = co.embed(texts=[query], model='embed-english-v3.0', input_type='search_query')
153
+ query_embedding = response.embeddings
154
+ query_embedding = torch.tensor(query_embedding)
155
+
156
+ # Compute dot score between query embedding and document embeddings
157
+ dot_scores = torch.mm(query_embedding, doc_embeddings.transpose(0, 1))
158
+ top_k = torch.topk(dot_scores, k=3)
159
+
160
+ # Print results
161
+ print("Query:", query)
162
+ for doc_id in top_k.indices[0].tolist():
163
+ print(docs[doc_id]['title'])
164
+ print(docs[doc_id]['text'], "\n")
165
+ ```
166
+
167
+
168
+ ## Running evaluations
169
+
170
+ This dataset allows to reproduce the [BEIR](https://github.com/beir-cellar/beir) performance results and to compute nDCG@10, Recall@10, and Accuracy@3.
171
+
172
+ You must have `beir`, `faiss`, `numpy`, and `datasets` installed. The following scripts loads all files, runs search and computes the search quality metrices.
173
+
174
+ ```python
175
+ import numpy as np
176
+ import faiss
177
+ from beir.retrieval.evaluation import EvaluateRetrieval
178
+ import time
179
+ from datasets import load_dataset
180
+
181
+ def faiss_search(index, queries_emb, k=[10, 100]):
182
+ start_time = time.time()
183
+ faiss_scores, faiss_doc_ids = index.search(queries_emb, max(k))
184
+ print(f"Search took {(time.time()-start_time):.2f} sec")
185
+
186
+ query2id = {idx: qid for idx, qid in enumerate(query_ids)}
187
+ doc2id = {idx: cid for idx, cid in enumerate(docs_ids)}
188
+
189
+ faiss_results = {}
190
+ for idx in range(0, len(faiss_scores)):
191
+ qid = query2id[idx]
192
+ doc_scores = {doc2id[doc_id]: score.item() for doc_id, score in zip(faiss_doc_ids[idx], faiss_scores[idx])}
193
+ faiss_results[qid] = doc_scores
194
+
195
+ ndcg, map_score, recall, precision = EvaluateRetrieval.evaluate(qrels, faiss_results, k)
196
+ acc = EvaluateRetrieval.evaluate_custom(qrels, faiss_results, [3, 5, 10], metric="acc")
197
+ print(ndcg)
198
+ print(recall)
199
+ print(acc)
200
+
201
+ dataset_name = "<<DATASET_NAME>>"
202
+ dataset_split = "test"
203
+ num_dim = 1024
204
+
205
+ #Load qrels
206
+ df = load_dataset(dataset_name, "qrels", split=dataset_split)
207
+ qrels = {}
208
+ for row in df:
209
+ qid = row['query_id']
210
+ cid = row['corpus_id']
211
+
212
+ if row['score'] > 0:
213
+ if qid not in qrels:
214
+ qrels[qid] = {}
215
+ qrels[qid][cid] = row['score']
216
+
217
+ #Load queries
218
+ df = load_dataset(dataset_name, "queries", split=dataset_split)
219
+
220
+ query_ids = df['_id']
221
+ query_embs = np.asarray(df['emb'])
222
+ print("Query embeddings:", query_embs.shape)
223
+
224
+ #Load corpus
225
+ df = load_dataset(dataset_name, "corpus", split="train")
226
+
227
+ docs_ids = df['_id']
228
+
229
+ #Build index
230
+ print("Build index. This might take some time")
231
+ index = faiss.IndexFlatIP(num_dim)
232
+ index.add(np.asarray(df.to_pandas()['emb'].tolist()))
233
+
234
+ #Run and evaluate search
235
+ print("Seach on index")
236
+ faiss_search(index, query_embs)
237
+ ```
238
+
239
+ ## Notes
240
+ - This dataset was created with `datasets==2.15.0`. Make sure to use this or a newer version of the datasets library.
241
+
242
+
243
+
hotpotqa/corpus/0000.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6462fc6e8349973a744bfd55e297d4f9f7008810cb0f9de918c6360893d624f7
3
+ size 2170669264
hotpotqa/corpus/0001.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:177e1ddf749554d9bed2b684fa4a4c224f2af574a5af0ec573eea72a10afa0bd
3
+ size 2135271799
hotpotqa/corpus/0002.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fa91f45cd0658187b820605b027961db9943d84b709c4ff42233e73b3cda224d
3
+ size 2121660123
hotpotqa/corpus/0003.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:93004a92651cd257ff61b8f44be2517c2b0277fb5fb32656ea8dc6acfc7a18fc
3
+ size 2123236089
hotpotqa/corpus/0004.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4487d87e34fb28f450dd41d6f359fb55afd392d1d4a5c252ab49263d74d9f9b1
3
+ size 2119066757
hotpotqa/corpus/0005.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:48d3181044f29bf4ffecd519851de14f0b3ce60e7c67db5b50889b3747d510f4
3
+ size 493155240
hotpotqa/qrels/dev.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:64160cccaccbd85b374a5c06d1f60e93f1f833098d7ff24720f58dc6af5bc439
3
+ size 201527
hotpotqa/qrels/test.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ebeb047cb814d1c873f0ef04bc64e43ee69e33fc79d63afd342a607aad19a458
3
+ size 270451
hotpotqa/qrels/train.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7f1a975882326fd1fc5cc1c997560b2e745b0e816653976ab48829642d3e5a9b
3
+ size 2813715
hotpotqa/queries/dev.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3bf94e909f9c2d069e422dcba5d34d03a2c8d99530aa0cbfb81c1029cf2cb1b4
3
+ size 11071525
hotpotqa/queries/test.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:02a1770ca110d5b7a9c7e2be7bd509f17c4ef0ccc2723f13db6af6fac44a5870
3
+ size 14949674
hotpotqa/queries/train.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:00ee23b7ded32894a385ee61b6f113a49d1c9ce55ab5540185d1985304542240
3
+ size 172070385
msmarco/corpus/0000.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:54975b13fb21770eb6a516cc4698b048c1ac0e559688043ce79f09cc1448f004
3
+ size 2118624298
msmarco/corpus/0001.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0a014aa458e22611821b3f99107b093c8f9ee2efdb7904b68b0327aa1ee1782f
3
+ size 2119434737
msmarco/corpus/0002.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6d58eddae2e0b6ca43c0f7445affe0aae07d60e3a193bff19014feb3024a0f3a
3
+ size 2121345038
msmarco/corpus/0003.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5d2d378ed361887e2a6d88e49ad17e527ea61cc3219f12389116d6e17a5ddeb5
3
+ size 2122073986
msmarco/corpus/0004.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8feb02b95fa1e71b614002aaf4b0f68ec274c0981d114b277d7aa6d199d62e33
3
+ size 2122366925
msmarco/corpus/0005.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:62ad66e2299edadff7ea8ec1975555bd6e0f938e445275155831d1541f551a14
3
+ size 2122729024
msmarco/corpus/0006.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b30ccb63d7c093af56f4bc061f2feaca4d483c4536744a74f19adac73eb1c2fa
3
+ size 2124551714
msmarco/corpus/0007.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9debb7d84a3853a6e26ddb4b4f74acaa625a967139e849bae484538402ebfde7
3
+ size 2120459673
msmarco/corpus/0008.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a8fad21b88601f049ee08e26cfcccf8f92ec88c72ca2abbb3b6076692d265ea8
3
+ size 1784942584
msmarco/qrels/dev.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:973bea871331018b6afe8108b507c139fdc695b7491a470ce53a375984eac30d
3
+ size 111519
msmarco/qrels/train.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:63d6fa16a1c607061d84b10dcf4753c92d71bb6428f9c00b26a6bdf4bec4d999
3
+ size 6348529
msmarco/queries/dev.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c10f47166ec7b883e7a4c43594fe8e55f164686351d82069e4d615a5fd37d6c0
3
+ size 13760904
msmarco/queries/train.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bb9bd24328457a342c0d47ebacccbca539d687194f493514ca21d7de0769c449
3
+ size 989438410
nfcorpus/corpus/0000.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:34703a489f88e1c0d7328d78b7c274c937cb0e4c807fcbfcb20aee5fe823687b
3
+ size 10283557
nfcorpus/qrels/dev.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2e0f196f77ac4bbb6d7cda2c751f181fa91194710cc3c1d3e25a87616403c358
3
+ size 35925
nfcorpus/qrels/test.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b2b3ed43add9eb4d38962fe7d19a396f38f506a6503efce2496e384a7f9bbd40
3
+ size 36758
nfcorpus/qrels/train.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:47f026019c36684bd160f817b3fb979785c6980ef98231305e9cfe00beb0d719
3
+ size 155190
nfcorpus/queries/dev.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0eb57d19a83d4413eefa4b70facdc6f3b4d3b7fcb68c916caf7c2150d22f6a89
3
+ size 711562
nfcorpus/queries/test.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:39e6a8d051752b31e018dfa386b0ef044e748ca8881873009b0a02859f35b636
3
+ size 709281
nfcorpus/queries/train.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:05068b6f663e652049b993af0229654a591fba08e724a761373759027bfc7a27
3
+ size 5145807