-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprepare_milvus.py
244 lines (178 loc) · 6.33 KB
/
prepare_milvus.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# Import required libraries
from huggingface_hub import snapshot_download # To download vector data
from pymilvus import MilvusClient, DataType
import requests
from time import sleep
from glob import glob
################################################################################
# Configuration
# Flag to indicate if the data is float or binary
FLOAT = False
BINARY = True
# Print on console which 1 of the flags is True
print(f"FLOAT: {FLOAT}")
print(f"BINARY: {BINARY}")
################################################################################
# Download vector dataset
# https://huggingface.co/docs/huggingface_hub/v0.24.6/en/package_reference/file_download#huggingface_hub.snapshot_download
# Setup transaction details
# When 100% of the data is float, use the float repo
if FLOAT:
repo_id = "bluuebunny/arxiv_abstract_embedding_mxbai_large_v1_milvus"
# When 100% of the data is binary, use the binary repo
if BINARY:
repo_id = "bluuebunny/arxiv_abstract_embedding_mxbai_large_v1_milvus_binary"
# Check that only 1 of the flags is True
if FLOAT and BINARY:
raise ValueError("FLOAT and BINARY are both True, set only 1 to True")
# Check that at least 1 of the flags is True
if not FLOAT and not BINARY:
raise ValueError("FLOAT and BINARY are both False, set at least 1 to True")
repo_type = "dataset"
local_dir = "volumes/milvus"
allow_patterns = "*.parquet"
# Download the repo
snapshot_download(repo_id=repo_id, repo_type=repo_type, local_dir=local_dir, allow_patterns=allow_patterns)
################################################################################
# Create collection
# Define client
client = MilvusClient("http://localhost:19530")
# Drop any of the pre-existing collections
# Need to drop it because otherwise milvus does not check for (and keeps)
# duplicate records
client.drop_collection(
collection_name="arxiv_abstracts"
)
# Dataset schema
schema = MilvusClient.create_schema(
auto_id=False,
enable_dynamic_field=False
)
# Add the fields to the schema
schema.add_field(field_name="id", datatype=DataType.VARCHAR, is_primary=True, max_length=32)
if FLOAT:
schema.add_field(field_name="vector", datatype=DataType.FLOAT_VECTOR, dim=1024)
if BINARY:
schema.add_field(field_name="vector", datatype=DataType.BINARY_VECTOR, dim=1024)
schema.add_field(field_name="title", datatype=DataType.VARCHAR, max_length=512)
schema.add_field(field_name="authors", datatype=DataType.VARCHAR, max_length=256)
schema.add_field(field_name="abstract", datatype=DataType.VARCHAR, max_length=3072)
schema.add_field(field_name="categories", datatype=DataType.VARCHAR, max_length=128)
schema.add_field(field_name="month", datatype=DataType.VARCHAR, max_length=16)
schema.add_field(field_name="year", datatype=DataType.VARCHAR, max_length=8)
schema.add_field(field_name="url", datatype=DataType.VARCHAR, max_length=64)
print("Issues with scheme: ", schema.verify())
# Create a collection
client.create_collection(
collection_name="arxiv_abstracts",
schema=schema
)
################################################################################
# Create import job
# https://milvus.io/docs/import-data.md
# Gather files
files = glob('volumes/milvus/data/*.parquet')
files = [ ["/var/lib/milvus/data/" + i.split('/')[-1]] for i in files ]
# Define the API endpoint
job_url = f"http://localhost:19530/v2/vectordb/jobs/import/create"
# Define the headers
headers = {
"Content-Type": "application/json"
}
# Define the data payload
job_url_data = {
"files": files,
"collectionName": "arxiv_abstracts"
}
# Make the POST request
job_response = requests.post(job_url, headers=headers, json=job_url_data)
job_json = job_response.json()
# Print the response
print("Job details:")
print(job_response.status_code)
print(job_json)
# Extract jobId
job_id = job_json['data']['jobId']
# Periodically check on import status
progress_url = "http://localhost:19530/v2/vectordb/jobs/import/get_progress"
progress_url_data = {
"jobId": f"{job_id}"
}
while True:
print('*'*80)
# Sleep a bit
seconds = 10
print(f"Sleeping for {seconds} seconds")
sleep(seconds)
# Make the POST request
progress_response = requests.post(progress_url, headers=headers, json=progress_url_data)
progress_json = progress_response.json()
# print(progress_json)
progress_percent = progress_json['data']['progress']
progress_state = progress_json['data']['state']
if progress_state == 'Pending' or progress_state == 'Importing':
print(f"Job: {progress_state}.")
print(f"Finised: {progress_percent}%.")
elif progress_state == 'Completed':
print(f"Job: {progress_state}.")
print(f"Imported {progress_json['data']['totalRows']} rows.")
break
elif progress_state == 'Failed':
print(f"Job: {progress_state}.")
print(progress_json)
print("Exiting...")
exit()
################################################################################
# Create index
# Set up the index parameters
index_params = MilvusClient.prepare_index_params()
# Add an index on the vector field.
if FLOAT:
index_params.add_index(
field_name="vector",
metric_type="COSINE",
index_type="IVF_FLAT",
index_name="vector_index",
params={ "nlist": 128 }
)
if BINARY:
index_params.add_index(
field_name="vector",
metric_type="HAMMING",
index_type="BIN_IVF_FLAT",
index_name="vector_index",
params={ "nlist": 128 }
)
print("Creating Index file.")
# Create an index file
res = client.create_index(
collection_name="arxiv_abstracts",
index_params=index_params,
sync=True # Wait for index creation to complete before returning.
)
print(res)
print("Listing indexes.")
# List indexes
res = client.list_indexes(
collection_name="arxiv_abstracts"
)
print(res)
print("Describing Index.")
# Describe index
res = client.describe_index(
collection_name="arxiv_abstracts",
index_name="vector_index"
)
print(res)
################################################################################
# Load the collection
print("Loading Collection")
client.load_collection(
collection_name="arxiv_abstracts",
replica_number=1 # Number of replicas to create on query nodes.
)
res = client.get_load_state(
collection_name="arxiv_abstracts"
)
print("Collection load state:")
print(res)