forked from Azure-Samples/azure-search-openai-demo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprepdocs.py
471 lines (437 loc) · 19.4 KB
/
prepdocs.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
import argparse
import asyncio
import logging
from typing import Optional, Union
from azure.core.credentials import AzureKeyCredential
from azure.core.credentials_async import AsyncTokenCredential
from azure.identity.aio import AzureDeveloperCliCredential, get_bearer_token_provider
from prepdocslib.blobmanager import BlobManager
from prepdocslib.embeddings import (
AzureOpenAIEmbeddingService,
ImageEmbeddings,
OpenAIEmbeddingService,
)
from prepdocslib.fileprocessor import FileProcessor
from prepdocslib.filestrategy import FileStrategy
from prepdocslib.htmlparser import LocalHTMLParser
from prepdocslib.integratedvectorizerstrategy import (
IntegratedVectorizerStrategy,
)
from prepdocslib.jsonparser import JsonParser
from prepdocslib.listfilestrategy import (
ADLSGen2ListFileStrategy,
ListFileStrategy,
LocalListFileStrategy,
)
from prepdocslib.parser import Parser
from prepdocslib.pdfparser import DocumentAnalysisParser, LocalPdfParser
from prepdocslib.strategy import DocumentAction, SearchInfo, Strategy
from prepdocslib.textparser import TextParser
from prepdocslib.textsplitter import SentenceTextSplitter, SimpleTextSplitter
logger = logging.getLogger("ingester")
def clean_key_if_exists(key: Union[str, None]) -> Union[str, None]:
"""Remove leading and trailing whitespace from a key if it exists. If the key is empty, return None."""
if key is not None and key.strip() != "":
return key.strip()
return None
async def setup_search_info(
search_service: str, index_name: str, azure_credential: AsyncTokenCredential, search_key: Union[str, None] = None
) -> SearchInfo:
search_creds: Union[AsyncTokenCredential, AzureKeyCredential] = (
azure_credential if search_key is None else AzureKeyCredential(search_key)
)
return SearchInfo(
endpoint=f"https://{search_service}.search.windows.net/",
credential=search_creds,
index_name=index_name,
)
def setup_blob_manager(
azure_credential: AsyncTokenCredential,
storage_account: str,
storage_container: str,
storage_resource_group: str,
subscription_id: str,
search_images: bool,
storage_key: Union[str, None] = None,
):
storage_creds: Union[AsyncTokenCredential, str] = azure_credential if storage_key is None else storage_key
return BlobManager(
endpoint=f"https://{storage_account}.blob.core.windows.net",
container=storage_container,
account=storage_account,
credential=storage_creds,
resourceGroup=storage_resource_group,
subscriptionId=subscription_id,
store_page_images=search_images,
)
def setup_list_file_strategy(
azure_credential: AsyncTokenCredential,
local_files: Union[str, None],
datalake_storage_account: Union[str, None],
datalake_filesystem: Union[str, None],
datalake_path: Union[str, None],
datalake_key: Union[str, None],
):
list_file_strategy: ListFileStrategy
if datalake_storage_account:
if datalake_filesystem is None or datalake_path is None:
raise ValueError("DataLake file system and path are required when using Azure Data Lake Gen2")
adls_gen2_creds: Union[AsyncTokenCredential, str] = azure_credential if datalake_key is None else datalake_key
logger.info("Using Data Lake Gen2 Storage Account: %s", datalake_storage_account)
list_file_strategy = ADLSGen2ListFileStrategy(
data_lake_storage_account=datalake_storage_account,
data_lake_filesystem=datalake_filesystem,
data_lake_path=datalake_path,
credential=adls_gen2_creds,
)
elif local_files:
logger.info("Using local files: %s", local_files)
list_file_strategy = LocalListFileStrategy(path_pattern=local_files)
else:
raise ValueError("Either local_files or datalake_storage_account must be provided.")
return list_file_strategy
def setup_embeddings_service(
azure_credential: AsyncTokenCredential,
openai_host: str,
openai_model_name: str,
openai_service: Union[str, None],
openai_deployment: Union[str, None],
openai_dimensions: int,
openai_key: Union[str, None],
openai_org: Union[str, None],
disable_vectors: bool = False,
disable_batch_vectors: bool = False,
):
if disable_vectors:
logger.info("Not setting up embeddings service")
return None
if openai_host != "openai":
azure_open_ai_credential: Union[AsyncTokenCredential, AzureKeyCredential] = (
azure_credential if openai_key is None else AzureKeyCredential(openai_key)
)
return AzureOpenAIEmbeddingService(
open_ai_service=openai_service,
open_ai_deployment=openai_deployment,
open_ai_model_name=openai_model_name,
open_ai_dimensions=openai_dimensions,
credential=azure_open_ai_credential,
disable_batch=disable_batch_vectors,
)
else:
if openai_key is None:
raise ValueError("OpenAI key is required when using the non-Azure OpenAI API")
return OpenAIEmbeddingService(
open_ai_model_name=openai_model_name,
open_ai_dimensions=openai_dimensions,
credential=openai_key,
organization=openai_org,
disable_batch=disable_batch_vectors,
)
def setup_file_processors(
azure_credential: AsyncTokenCredential,
document_intelligence_service: Union[str, None],
document_intelligence_key: Union[str, None] = None,
local_pdf_parser: bool = False,
local_html_parser: bool = False,
search_images: bool = False,
):
html_parser: Parser
pdf_parser: Parser
doc_int_parser: DocumentAnalysisParser
# check if Azure Document Intelligence credentials are provided
if document_intelligence_service is not None:
documentintelligence_creds: Union[AsyncTokenCredential, AzureKeyCredential] = (
azure_credential if document_intelligence_key is None else AzureKeyCredential(document_intelligence_key)
)
doc_int_parser = DocumentAnalysisParser(
endpoint=f"https://{document_intelligence_service}.cognitiveservices.azure.com/",
credential=documentintelligence_creds,
)
if local_pdf_parser or document_intelligence_service is None:
pdf_parser = LocalPdfParser()
else:
pdf_parser = doc_int_parser
if local_html_parser or document_intelligence_service is None:
html_parser = LocalHTMLParser()
else:
html_parser = doc_int_parser
sentence_text_splitter = SentenceTextSplitter(has_image_embeddings=search_images)
return {
".pdf": FileProcessor(pdf_parser, sentence_text_splitter),
".html": FileProcessor(html_parser, sentence_text_splitter),
".json": FileProcessor(JsonParser(), SimpleTextSplitter()),
".docx": FileProcessor(doc_int_parser, sentence_text_splitter),
".pptx": FileProcessor(doc_int_parser, sentence_text_splitter),
".xlsx": FileProcessor(doc_int_parser, sentence_text_splitter),
".png": FileProcessor(doc_int_parser, sentence_text_splitter),
".jpg": FileProcessor(doc_int_parser, sentence_text_splitter),
".jpeg": FileProcessor(doc_int_parser, sentence_text_splitter),
".tiff": FileProcessor(doc_int_parser, sentence_text_splitter),
".bmp": FileProcessor(doc_int_parser, sentence_text_splitter),
".heic": FileProcessor(doc_int_parser, sentence_text_splitter),
".md": FileProcessor(TextParser(), sentence_text_splitter),
".txt": FileProcessor(TextParser(), sentence_text_splitter),
}
def setup_image_embeddings_service(
azure_credential: AsyncTokenCredential, vision_endpoint: Union[str, None], search_images: bool
) -> Union[ImageEmbeddings, None]:
image_embeddings_service: Optional[ImageEmbeddings] = None
if search_images:
if vision_endpoint is None:
raise ValueError("A computer vision endpoint is required when GPT-4-vision is enabled.")
image_embeddings_service = ImageEmbeddings(
endpoint=vision_endpoint,
token_provider=get_bearer_token_provider(azure_credential, "https://cognitiveservices.azure.com/.default"),
)
return image_embeddings_service
async def main(strategy: Strategy, setup_index: bool = True):
if setup_index:
await strategy.setup()
await strategy.run()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Prepare documents by extracting content from PDFs, splitting content into sections, uploading to blob storage, and indexing in a search index.",
epilog="Example: prepdocs.py '.\\data\*' --storageaccount myaccount --container mycontainer --searchservice mysearch --index myindex -v",
)
parser.add_argument("files", nargs="?", help="Files to be processed")
parser.add_argument(
"--datalakestorageaccount", required=False, help="Optional. Azure Data Lake Storage Gen2 Account name"
)
parser.add_argument(
"--datalakefilesystem",
required=False,
default="gptkbcontainer",
help="Optional. Azure Data Lake Storage Gen2 filesystem name",
)
parser.add_argument(
"--datalakepath",
required=False,
help="Optional. Azure Data Lake Storage Gen2 filesystem path containing files to index. If omitted, index the entire filesystem",
)
parser.add_argument(
"--datalakekey", required=False, help="Optional. Use this key when authenticating to Azure Data Lake Gen2"
)
parser.add_argument(
"--useacls", action="store_true", help="Store ACLs from Azure Data Lake Gen2 Filesystem in the search index"
)
parser.add_argument(
"--category", help="Value for the category field in the search index for all sections indexed in this run"
)
parser.add_argument(
"--skipblobs", action="store_true", help="Skip uploading individual pages to Azure Blob Storage"
)
parser.add_argument("--storageaccount", help="Azure Blob Storage account name")
parser.add_argument("--container", help="Azure Blob Storage container name")
parser.add_argument("--storageresourcegroup", help="Azure blob storage resource group")
parser.add_argument(
"--storagekey",
required=False,
help="Optional. Use this Azure Blob Storage account key instead of the current user identity to login (use az login to set current user for Azure)",
)
parser.add_argument(
"--tenantid", required=False, help="Optional. Use this to define the Azure directory where to authenticate)"
)
parser.add_argument(
"--subscriptionid",
required=False,
help="Optional. Use this to define managed identity connection string in integrated vectorization",
)
parser.add_argument(
"--searchservice",
help="Name of the Azure AI Search service where content should be indexed (must exist already)",
)
parser.add_argument(
"--searchserviceassignedid",
required=False,
help="Search service system assigned Identity (Managed identity) (used for integrated vectorization)",
)
parser.add_argument(
"--index",
help="Name of the Azure AI Search index where content should be indexed (will be created if it doesn't exist)",
)
parser.add_argument(
"--searchkey",
required=False,
help="Optional. Use this Azure AI Search account key instead of the current user identity to login (use az login to set current user for Azure)",
)
parser.add_argument(
"--searchanalyzername",
required=False,
default="en.microsoft",
help="Optional. Name of the Azure AI Search analyzer to use for the content field in the index",
)
parser.add_argument("--openaihost", help="Host of the API used to compute embeddings ('azure' or 'openai')")
parser.add_argument("--openaiservice", help="Name of the Azure OpenAI service used to compute embeddings")
parser.add_argument(
"--openaideployment",
help="Name of the Azure OpenAI model deployment for an embedding model ('text-embedding-ada-002' recommended)",
)
parser.add_argument(
"--openaimodelname", help="Name of the Azure OpenAI embedding model ('text-embedding-ada-002' recommended)"
)
parser.add_argument(
"--openaidimensions",
required=False,
default=1536,
type=int,
help="Dimensions for the embedding model (defaults to 1536 for 'text-embedding-ada-002')",
)
parser.add_argument(
"--novectors",
action="store_true",
help="Don't compute embeddings for the sections (e.g. don't call the OpenAI embeddings API during indexing)",
)
parser.add_argument(
"--disablebatchvectors", action="store_true", help="Don't compute embeddings in batch for the sections"
)
parser.add_argument(
"--openaikey",
required=False,
help="Optional. Use this Azure OpenAI account key instead of the current user identity to login (use az login to set current user for Azure). This is required only when using non-Azure endpoints.",
)
parser.add_argument("--openaiorg", required=False, help="This is required only when using non-Azure endpoints.")
parser.add_argument(
"--remove",
action="store_true",
help="Remove references to this document from blob storage and the search index",
)
parser.add_argument(
"--removeall",
action="store_true",
help="Remove all blobs from blob storage and documents from the search index",
)
parser.add_argument(
"--localpdfparser",
action="store_true",
help="Use PyPdf local PDF parser (supports only digital PDFs) instead of Azure Document Intelligence service to extract text, tables and layout from the documents",
)
parser.add_argument(
"--localhtmlparser",
action="store_true",
help="Use Beautiful soap local HTML parser instead of Azure Document Intelligence service to extract text, tables and layout from the documents",
)
parser.add_argument(
"--documentintelligenceservice",
required=False,
help="Optional. Name of the Azure Document Intelligence service which will be used to extract text, tables and layout from the documents (must exist already)",
)
parser.add_argument(
"--documentintelligencekey",
required=False,
help="Optional. Use this Azure Document Intelligence account key instead of the current user identity to login (use az login to set current user for Azure)",
)
parser.add_argument(
"--searchimages",
action="store_true",
required=False,
help="Optional. Generate image embeddings to enable each page to be searched as an image",
)
parser.add_argument(
"--visionendpoint",
required=False,
help="Optional, required if --searchimages is specified. Endpoint of Azure AI Vision service to use when embedding images.",
)
parser.add_argument(
"--useintvectorization",
required=False,
help="Required if --useintvectorization is specified. Enable Integrated vectorizer indexer support which is in preview)",
)
parser.add_argument("--verbose", "-v", action="store_true", help="Verbose output")
args = parser.parse_args()
if args.verbose:
logging.basicConfig(format="%(message)s")
# We only set the level to INFO for our logger,
# to avoid seeing the noisy INFO level logs from the Azure SDKs
logger.setLevel(logging.INFO)
use_int_vectorization = args.useintvectorization and args.useintvectorization.lower() == "true"
# Use the current user identity to connect to Azure services unless a key is explicitly set for any of them
azd_credential = (
AzureDeveloperCliCredential()
if args.tenantid is None
else AzureDeveloperCliCredential(tenant_id=args.tenantid, process_timeout=60)
)
if args.removeall:
document_action = DocumentAction.RemoveAll
elif args.remove:
document_action = DocumentAction.Remove
else:
document_action = DocumentAction.Add
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
search_info = loop.run_until_complete(
setup_search_info(
search_service=args.searchservice,
index_name=args.index,
azure_credential=azd_credential,
search_key=clean_key_if_exists(args.searchkey),
)
)
blob_manager = setup_blob_manager(
azure_credential=azd_credential,
storage_account=args.storageaccount,
storage_container=args.container,
storage_resource_group=args.storageresourcegroup,
subscription_id=args.subscriptionid,
search_images=args.searchimages,
storage_key=clean_key_if_exists(args.storagekey),
)
list_file_strategy = setup_list_file_strategy(
azure_credential=azd_credential,
local_files=args.files,
datalake_storage_account=args.datalakestorageaccount,
datalake_filesystem=args.datalakefilesystem,
datalake_path=args.datalakepath,
datalake_key=clean_key_if_exists(args.datalakekey),
)
openai_embeddings_service = setup_embeddings_service(
azure_credential=azd_credential,
openai_host=args.openaihost,
openai_model_name=args.openaimodelname,
openai_service=args.openaiservice,
openai_deployment=args.openaideployment,
openai_dimensions=args.openaidimensions,
openai_key=clean_key_if_exists(args.openaikey),
openai_org=args.openaiorg,
disable_vectors=args.novectors,
disable_batch_vectors=args.disablebatchvectors,
)
ingestion_strategy: Strategy
if use_int_vectorization:
ingestion_strategy = IntegratedVectorizerStrategy(
search_info=search_info,
list_file_strategy=list_file_strategy,
blob_manager=blob_manager,
document_action=document_action,
embeddings=openai_embeddings_service,
subscription_id=args.subscriptionid,
search_service_user_assigned_id=args.searchserviceassignedid,
search_analyzer_name=args.searchanalyzername,
use_acls=args.useacls,
category=args.category,
)
else:
file_processors = setup_file_processors(
azure_credential=azd_credential,
document_intelligence_service=args.documentintelligenceservice,
document_intelligence_key=clean_key_if_exists(args.documentintelligencekey),
local_pdf_parser=args.localpdfparser,
local_html_parser=args.localhtmlparser,
search_images=args.searchimages,
)
image_embeddings_service = setup_image_embeddings_service(
azure_credential=azd_credential, vision_endpoint=args.visionendpoint, search_images=args.searchimages
)
ingestion_strategy = FileStrategy(
search_info=search_info,
list_file_strategy=list_file_strategy,
blob_manager=blob_manager,
file_processors=file_processors,
document_action=document_action,
embeddings=openai_embeddings_service,
image_embeddings=image_embeddings_service,
search_analyzer_name=args.searchanalyzername,
use_acls=args.useacls,
category=args.category,
)
loop.run_until_complete(main(ingestion_strategy, setup_index=not args.remove and not args.removeall))
loop.close()