-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathdata_preparation.py
637 lines (579 loc) · 21.3 KB
/
data_preparation.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
"""Data Preparation Script for an Azure Cognitive Search Index."""
import argparse
import dataclasses
import json
import os
import subprocess
import time
import requests
from azure.ai.documentintelligence import DocumentIntelligenceClient
from azure.core.credentials import AzureKeyCredential
from azure.identity import AzureCliCredential
from azure.search.documents import SearchClient
from data_utils import chunk_blob_container, chunk_directory
from dotenv import load_dotenv
from tqdm import tqdm
# Configure environment variables
load_dotenv() # take environment variables from .env.
SUPPORTED_LANGUAGE_CODES = {
"ar": "Arabic",
"hy": "Armenian",
"eu": "Basque",
"bg": "Bulgarian",
"ca": "Catalan",
"zh-Hans": "Chinese Simplified",
"zh-Hant": "Chinese Traditional",
"cs": "Czech",
"da": "Danish",
"nl": "Dutch",
"en": "English",
"fi": "Finnish",
"fr": "French",
"gl": "Galician",
"de": "German",
"el": "Greek",
"hi": "Hindi",
"hu": "Hungarian",
"id": "Indonesian (Bahasa)",
"ga": "Irish",
"it": "Italian",
"ja": "Japanese",
"ko": "Korean",
"lv": "Latvian",
"no": "Norwegian",
"fa": "Persian",
"pl": "Polish",
"pt-Br": "Portuguese (Brazil)",
"pt-Pt": "Portuguese (Portugal)",
"ro": "Romanian",
"ru": "Russian",
"es": "Spanish",
"sv": "Swedish",
"th": "Thai",
"tr": "Turkish",
}
def check_if_search_service_exists(
search_service_name: str, subscription_id: str, resource_group: str, credential=None
):
"""_summary_
Args:
search_service_name (str): _description_
subscription_id (str): _description_
resource_group (str): _description_
credential: Azure credential to use for getting acs instance
"""
if credential is None:
raise ValueError("credential cannot be None")
url = (
f"https://management.azure.com/subscriptions/{subscription_id}"
f"/resourceGroups/{resource_group}/providers/Microsoft.Search/searchServices"
f"/{search_service_name}?api-version=2024-03-01-Preview"
)
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {credential.get_token('https://management.azure.com/.default').token}",
}
response = requests.get(url, headers=headers)
return response.status_code == 200
def create_search_service(
search_service_name: str,
subscription_id: str,
resource_group: str,
location: str,
sku: str = "standard",
credential=None,
):
"""_summary_
Args:
search_service_name (str): _description_
subscription_id (str): _description_
resource_group (str): _description_
location (str): _description_
credential: Azure credential to use for creating acs instance
Raises:
Exception: _description_
"""
if credential is None:
raise ValueError("credential cannot be None")
url = (
f"https://management.azure.com/subscriptions/{subscription_id}"
f"/resourceGroups/{resource_group}/providers/Microsoft.Search/searchServices"
f"/{search_service_name}?api-version=2024-03-01-Preview"
)
payload = {
"location": f"{location}",
"sku": {"name": sku},
"properties": {
"replicaCount": 1,
"partitionCount": 1,
"hostingMode": "default",
"semanticSearch": "free",
},
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {credential.get_token('https://management.azure.com/.default').token}",
}
response = requests.put(url, json=payload, headers=headers)
if response.status_code != 201:
raise Exception(f"Failed to create search service. Error: {response.text}")
def create_or_update_search_index(
service_name,
subscription_id=None,
resource_group=None,
index_name="default-index",
semantic_config_name="default",
credential=None,
language=None,
vector_config_name=None,
admin_key=None,
):
if credential is None and admin_key is None:
raise ValueError("credential and admin key cannot be None")
if not admin_key:
admin_key = json.loads(
subprocess.run(
f"az search admin-key show --subscription {subscription_id} --resource-group {resource_group} --service-name {service_name}",
shell=True,
capture_output=True,
).stdout
)["primaryKey"]
url = f"https://{service_name}.search.windows.net/indexes/{index_name}?api-version=2024-03-01-Preview"
headers = {
"Content-Type": "application/json",
"api-key": admin_key,
}
body = {
"fields": [
{
"name": "id",
"type": "Edm.String",
"searchable": True,
"key": True,
},
{
"name": "content",
"type": "Edm.String",
"searchable": True,
"sortable": False,
"facetable": False,
"filterable": False,
"analyzer": f"{language}.lucene" if language else None,
},
{
"name": "full_content",
"type": "Edm.String",
"searchable": True,
"sortable": False,
"facetable": False,
"filterable": False,
"analyzer": f"{language}.lucene" if language else None,
},
{
"name": "title",
"type": "Edm.String",
"searchable": True,
"sortable": False,
"facetable": False,
"filterable": False,
"analyzer": f"{language}.lucene" if language else None,
},
{
"name": "filepath",
"type": "Edm.String",
"searchable": True,
"sortable": False,
"facetable": False,
"filterable": False,
},
{
"name": "url",
"type": "Edm.String",
"searchable": True,
},
{
"name": "metadata",
"type": "Edm.String",
"searchable": True,
},
{
"name": "image_mapping",
"type": "Edm.String",
"searchable": False,
"sortable": False,
"facetable": False,
"filterable": False,
},
],
"suggesters": [],
"scoringProfiles": [],
"semantic": {
"configurations": [
{
"name": semantic_config_name,
"prioritizedFields": {
"titleField": {"fieldName": "title"},
"prioritizedContentFields": [{"fieldName": "content"}],
"prioritizedKeywordsFields": [],
},
}
]
},
}
if vector_config_name:
body["fields"].append(
{
"name": "contentVector",
"type": "Collection(Edm.Single)",
"searchable": True,
"retrievable": True,
"stored": True,
"dimensions": int(os.getenv("VECTOR_DIMENSION", 1536)),
"vectorSearchProfile": vector_config_name,
}
)
body["vectorSearch"] = {
"algorithms": [
{
"name": "my-hnsw-config-1",
"kind": "hnsw",
"hnswParameters": {
"m": 4,
"efConstruction": 400,
"efSearch": 500,
"metric": "cosine",
},
}
],
"profiles": [{"name": vector_config_name, "algorithm": "my-hnsw-config-1"}],
}
response = requests.put(url, json=body, headers=headers)
if response.status_code == 201:
print(f"Created search index {index_name}")
elif response.status_code == 204:
print(f"Updated existing search index {index_name}")
else:
raise Exception(f"Failed to create search index. Error: {response.text}")
return True
def upload_documents_to_index(
service_name,
subscription_id,
resource_group,
index_name,
docs,
credential=None,
upload_batch_size=50,
admin_key=None,
):
if credential is None and admin_key is None:
raise ValueError("credential and admin_key cannot be None")
to_upload_dicts = []
id = 0
for d in docs:
if type(d) is not dict:
d = dataclasses.asdict(d)
# add id to documents
d.update({"@search.action": "upload", "id": str(id)})
if "contentVector" in d and d["contentVector"] is None:
del d["contentVector"]
to_upload_dicts.append(d)
id += 1
endpoint = "https://{}.search.windows.net/".format(service_name)
if not admin_key:
admin_key = json.loads(
subprocess.run(
f"az search admin-key show --subscription {subscription_id} --resource-group {resource_group} --service-name {service_name}",
shell=True,
capture_output=True,
).stdout
)["primaryKey"]
search_client = SearchClient(
endpoint=endpoint,
index_name=index_name,
credential=AzureKeyCredential(admin_key),
)
# Upload the documents in batches of upload_batch_size
for i in tqdm(
range(0, len(to_upload_dicts), upload_batch_size), desc="Indexing Chunks..."
):
batch = to_upload_dicts[i : i + upload_batch_size]
results = search_client.upload_documents(documents=batch)
num_failures = 0
errors = set()
for result in results:
if not result.succeeded:
print(
f"Indexing Failed for {result.key} with ERROR: {result.error_message}"
)
num_failures += 1
errors.add(result.error_message)
if num_failures > 0:
raise Exception(
f"INDEXING FAILED for {num_failures} documents. Please recreate the index."
f"To Debug: PLEASE CHECK chunk_size and upload_batch_size. \n Error Messages: {list(errors)}"
)
def validate_index(service_name, subscription_id, resource_group, index_name):
api_version = "2024-03-01-Preview"
admin_key = json.loads(
subprocess.run(
f"az search admin-key show --subscription {subscription_id} --resource-group {resource_group} --service-name {service_name}",
shell=True,
capture_output=True,
).stdout
)["primaryKey"]
headers = {"Content-Type": "application/json", "api-key": admin_key}
params = {"api-version": api_version}
url = f"https://{service_name}.search.windows.net/indexes/{index_name}/stats"
for retry_count in range(5):
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
response = response.json()
num_chunks = response["documentCount"]
if num_chunks == 0 and retry_count < 4:
print("Index is empty. Waiting 60 seconds to check again...")
time.sleep(60)
elif num_chunks == 0 and retry_count == 4:
print("Index is empty. Please investigate and re-index.")
else:
print(f"The index contains {num_chunks} chunks.")
average_chunk_size = response["storageSize"] / num_chunks
print(
f"The average chunk size of the index is {average_chunk_size} bytes."
)
break
else:
if response.status_code == 404:
print(
"The index does not seem to exist. Please make sure the index was created correctly, and that you are using the correct service and index names"
)
elif response.status_code == 403:
print("Authentication Failure: Make sure you are using the correct key")
else:
print(
f"Request failed. Please investigate. Status code: {response.status_code}"
)
break
def create_index(
config,
credential,
form_recognizer_client=None,
embedding_model_endpoint=None,
use_layout=False,
njobs=4,
captioning_model_endpoint=None,
captioning_model_key=None,
):
service_name = config["search_service_name"]
subscription_id = config["subscription_id"]
resource_group = config["resource_group"]
location = config["location"]
index_name = config["index_name"]
language = config.get("language", None)
if language and language not in SUPPORTED_LANGUAGE_CODES:
raise Exception(
f"ERROR: Ingestion does not support {language} documents. "
f"Please use one of {SUPPORTED_LANGUAGE_CODES}."
f"Language is set as two letter code for e.g. 'en' for English."
f"If you donot want to set a language just remove this prompt config or set as None"
)
# check if search service exists, create if not
try:
if check_if_search_service_exists(
service_name, subscription_id, resource_group, credential
):
print(f"Using existing search service {service_name}")
else:
print(f"Creating search service {service_name}")
create_search_service(
service_name,
subscription_id,
resource_group,
location,
credential=credential,
)
except Exception as e:
print(f"Unable to verify if search service exists. Error: {e}")
print("Proceeding to attempt to create index.")
# create or update search index with compatible schema
admin_key = os.environ.get("AZURE_SEARCH_ADMIN_KEY", None)
if not create_or_update_search_index(
service_name,
subscription_id,
resource_group,
index_name,
config["semantic_config_name"],
credential,
language,
vector_config_name=config.get("vector_config_name", None),
admin_key=admin_key,
):
raise Exception(f"Failed to create or update index {index_name}")
data_configs = []
if "data_path" in config:
data_configs.append(
{
"path": config["data_path"],
"url_prefix": config.get("url_prefix", None),
}
)
if "data_paths" in config:
data_configs.extend(config["data_paths"])
for data_config in data_configs:
# chunk directory
print(f"Chunking path {data_config['path']}...")
add_embeddings = False
if config.get("vector_config_name") and embedding_model_endpoint:
add_embeddings = True
if "blob.core" in data_config["path"]:
result = chunk_blob_container(
data_config["path"],
credential=credential,
num_tokens=config["chunk_size"],
token_overlap=config.get("token_overlap", 0),
azure_credential=credential,
form_recognizer_client=form_recognizer_client,
use_layout=use_layout,
njobs=njobs,
add_embeddings=add_embeddings,
embedding_endpoint=embedding_model_endpoint,
url_prefix=data_config["url_prefix"],
)
elif os.path.exists(data_config["path"]):
result = chunk_directory(
data_config["path"],
num_tokens=config["chunk_size"],
token_overlap=config.get("token_overlap", 0),
azure_credential=credential,
form_recognizer_client=form_recognizer_client,
use_layout=use_layout,
njobs=njobs,
add_embeddings=add_embeddings,
embedding_endpoint=embedding_model_endpoint,
url_prefix=data_config["url_prefix"],
captioning_model_endpoint=captioning_model_endpoint,
captioning_model_key=captioning_model_key,
)
else:
raise Exception(
f"Path {data_config['path']} does not exist and is not a blob URL. Please check the path and try again."
)
if len(result.chunks) == 0:
raise Exception(
"No chunks found. Please check the data path and chunk size."
)
print(f"Processed {result.total_files} files")
print(
f"Unsupported formats: {result.num_unsupported_format_files} files")
print(f"Files with errors: {result.num_files_with_errors} files")
print(f"Found {len(result.chunks)} chunks")
# upload documents to index
print("Uploading documents to index...")
upload_documents_to_index(
service_name,
subscription_id,
resource_group,
index_name,
result.chunks,
credential,
)
# check if index is ready/validate index
print("Validating index...")
validate_index(service_name, subscription_id, resource_group, index_name)
print("Index validation completed")
def valid_range(n):
n = int(n)
if n < 1 or n > 32:
raise argparse.ArgumentTypeError(
"njobs must be an Integer between 1 and 32.")
return n
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--config",
type=str,
help="Path to config file containing settings for data preparation",
)
parser.add_argument(
"--form-rec-resource",
type=str,
help="Name of your Form Recognizer resource to use for PDF cracking.",
)
parser.add_argument(
"--form-rec-key",
type=str,
help="Key for your Form Recognizer resource to use for PDF cracking.",
)
parser.add_argument(
"--form-rec-use-layout",
default=False,
action="store_true",
help="Whether to use Layout model for PDF cracking, if False will use Read model.",
)
parser.add_argument(
"--njobs",
type=valid_range,
default=4,
help="Number of jobs to run (between 1 and 32). Default=4",
)
parser.add_argument(
"--embedding-model-endpoint",
type=str,
help="Endpoint for the embedding model to use for vector search. Format: 'https://<AOAI resource name>.openai.azure.com/openai/deployments/<Ada deployment name>/embeddings?api-version=2024-03-01-Preview'",
)
parser.add_argument(
"--embedding-model-key",
type=str,
help="Key for the embedding model to use for vector search.",
)
parser.add_argument(
"--search-admin-key",
type=str,
help="Admin key for the search service. If not provided, will use Azure CLI to get the key.",
)
parser.add_argument(
"--azure-openai-endpoint",
type=str,
help="Endpoint for the (Azure) OpenAI API. Format: 'https://<AOAI resource name>.openai.azure.com/openai/deployments/<vision model name>/chat/completions?api-version=2024-04-01-preview'",
)
parser.add_argument(
"--azure-openai-key", type=str, help="Key for the (Azure) OpenAI API."
)
args = parser.parse_args()
with open(args.config) as f:
config = json.load(f)
credential = AzureCliCredential()
form_recognizer_client = None
print("Data preparation script started")
if args.search_admin_key:
os.environ["AZURE_SEARCH_ADMIN_KEY"] = args.search_admin_key
if args.form_rec_resource and args.form_rec_key:
os.environ["FORM_RECOGNIZER_ENDPOINT"] = (
f"https://{args.form_rec_resource}.cognitiveservices.azure.com/"
)
os.environ["FORM_RECOGNIZER_KEY"] = args.form_rec_key
if args.njobs == 1:
form_recognizer_client = DocumentIntelligenceClient(
endpoint=f"https://{args.form_rec_resource}.cognitiveservices.azure.com/",
credential=AzureKeyCredential(args.form_rec_key),
)
print(
f"Using Form Recognizer resource {args.form_rec_resource} for PDF cracking, with the {'Layout' if args.form_rec_use_layout else 'Read'} model."
)
for index_config in config:
print("Preparing data for index:", index_config["index_name"])
if index_config.get("vector_config_name") and not args.embedding_model_endpoint:
raise Exception(
"ERROR: Vector search is enabled in the config, but no embedding model endpoint and key were provided. Please provide these values or disable vector search."
)
create_index(
index_config,
credential,
form_recognizer_client,
embedding_model_endpoint=args.embedding_model_endpoint,
use_layout=args.form_rec_use_layout,
njobs=args.njobs,
captioning_model_endpoint=args.azure_openai_endpoint,
captioning_model_key=args.azure_openai_key,
)
print("Data preparation for index", index_config["index_name"], "completed")
print(f"Data preparation script completed. {len(config)} indexes updated.")