mys commited on
Commit
a85ad26
1 Parent(s): 999bce3

Get API Key and host URL from env vars

Browse files
Files changed (2) hide show
  1. .gitignore +1 -1
  2. create_index.py +36 -8
.gitignore CHANGED
@@ -1,3 +1,3 @@
1
  __pycache__
2
  *.npy
3
- *.jsonl
 
1
  __pycache__
2
  *.npy
3
+ *.json
create_index.py CHANGED
@@ -1,18 +1,46 @@
 
 
1
  from encoder import encode
2
 
3
  import numpy as np
4
  import tqdm
5
  from datasets import load_dataset
6
 
7
- batch_size = 64
8
 
9
- dataset = load_dataset('wiki_qa', split='train+test+validation')
 
 
10
 
11
- answer_embeddings = np.zeros(shape=(len(dataset), 384), dtype=np.float32)
 
 
12
 
13
- for i in tqdm.tqdm(range(0, len(dataset), batch_size), desc='Extracting vectors'):
14
- batch_answers = dataset['answer'][i:i+batch_size]
15
- batch_embeddings = encode(batch_answers)
16
- answer_embeddings[i:i+batch_size] = batch_embeddings
17
 
18
- np.save('answer_embeddings.npy', answer_embeddings)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import os
3
  from encoder import encode
4
 
5
  import numpy as np
6
  import tqdm
7
  from datasets import load_dataset
8
 
 
9
 
10
+ def get_embeddings_and_payload(dataset, batch_size):
11
+ answer_embeddings = np.zeros(shape=(len(dataset), 384), dtype=np.float32)
12
+ answer_texts = []
13
 
14
+ for i in tqdm.tqdm(range(0, len(dataset), batch_size), desc='Extracting vectors'):
15
+ batch_answers = dataset['answer'][i:i+batch_size]
16
+ answer_texts.extend([{"text": answer in batch_answers}])
17
 
18
+ batch_embeddings = encode(batch_answers)
19
+ answer_embeddings[i:i+batch_size] = batch_embeddings
 
 
20
 
21
+ return answer_embeddings, answer_texts
22
+
23
+
24
+ def main(args):
25
+ if args.host_url is None:
26
+ print("You must specify a host URL either by passing it as a command line argument with `--host-url` or as an environment variable named `QDRANT_HOST_URL`")
27
+ exit()
28
+ if args.api_key is None:
29
+ print("You must specify an API key either by passing it as a command line argument with `--api-key` or as an environment variable named `QDRANT_API_KEY`")
30
+ exit()
31
+
32
+ dataset = load_dataset('wiki_qa', split='train+test+validation')
33
+ embeddings, payload_data = get_embeddings_and_payload(
34
+ dataset, args.batch_size)
35
+
36
+
37
+ if __name__ == "__main__":
38
+ ap = argparse.ArgumentParser(prog="Create index on on Qdrant Cloud")
39
+ ap.add_argument("-b", "--batch-size", type=int, default=64,
40
+ help="Batch size when extracting vectors")
41
+ ap.add_argument("-u", "--host-url", type=str,
42
+ default=os.environ['QDRANT_HOST_URL', None], help="Host URL from Qdrant Cloud")
43
+ ap.add_argument("-k", "--api-key", type=str,
44
+ default=os.environ['QDRANT_API_KEY', None], help="API key from Qdrant Cloud")
45
+ args = ap.parse_args()
46
+ main(args)