-
Notifications
You must be signed in to change notification settings - Fork 58
/
setup.py
1329 lines (1221 loc) · 56.3 KB
/
setup.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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import logging
import time
import requests
import argparse
import json
from azure.mgmt.web import WebSiteManagementClient
from azure.identity import ManagedIdentityCredential, AzureCliCredential, ChainedTokenCredential
from azure.core.exceptions import ClientAuthenticationError, HttpResponseError
# Set up logging configuration globally
logging.getLogger('azure').setLevel(logging.WARNING)
def call_search_api(search_service, search_api_version, resource_type, resource_name, method, credential, body=None):
"""
Calls the Azure Search API with the specified parameters.
Args:
search_service (str): The name of the Azure Search service.
search_api_version (str): The version of the Azure Search API to use.
resource_type (str): The type of resource to access (e.g. "indexes", "docs").
resource_name (str): The name of the resource to access.
method (str): The HTTP method to use (either "get" or "put").
credential (TokenCredential): An instance of a TokenCredential class that can provide an access token.
body (dict, optional): The JSON payload to include in the request body (for "put" requests).
Returns:
None
Raises:
ValueError: If the specified HTTP method is not "get" or "put".
HTTPError: If the response status code is 400 or greater.
"""
# get the token
token = credential.get_token("https://search.azure.com/.default").token
headers = {
"Authorization": f"Bearer {token}",
'Content-Type': 'application/json'
# 'api-key': SEARCH_API_KEY
}
search_endpoint = f"https://{search_service}.search.windows.net/{resource_type}/{resource_name}?api-version={search_api_version}"
response = None
try:
if method not in ["get", "put", "delete"]:
logging.warning(f"[call_search_api] Invalid method {method} ")
# get and put processing
if method == "get":
response = requests.get(search_endpoint, headers=headers)
elif method == "put":
response = requests.put(search_endpoint, headers=headers, json=body)
# delete processing
if method == "delete":
response = requests.delete(search_endpoint, headers=headers)
status_code = response.status_code
logging.info(f"[call_search_api] Successfully called search API {method} {resource_type} {resource_name}. Code: {status_code}.")
if response is not None:
status_code = response.status_code
if status_code >= 400:
logging.warning(f"[call_search_api] {status_code} code when calling search API {method} {resource_type} {resource_name}. Reason: {response.reason}.")
try:
response_text_dict = json.loads(response.text)
logging.warning(f"[call_search_api] {status_code} code when calling search API {method} {resource_type} {resource_name}. Message: {response_text_dict['error']['message']}")
except json.JSONDecodeError:
logging.warning(f"[call_search_api] {status_code} Response is not valid JSON. Raw response:\n{response.text}")
else:
logging.info(f"[call_search_api] Successfully called search API {method} {resource_type} {resource_name}. Code: {status_code}.")
except Exception as e:
error_message = str(e)
logging.error(f"Error when calling search API {method} {resource_type} {resource_name}. Error: {error_message}")
def get_function_key(subscription_id, resource_group, function_app_name, credential):
"""
Returns an API key for the given function.
Parameters:
subscription_id (str): The subscription ID.
resource_group (str): The resource group name.
function_app_name (str): The name of the function app.
credential (str): The credential to use.
Returns:
str: A unique key for the function.
"""
logging.info(f"Obtaining function key after creating or updating its value.")
accessToken = f"Bearer {credential.get_token('https://management.azure.com/.default').token}"
# Get key
requestUrl = f"https://management.azure.com/subscriptions/{subscription_id}/resourceGroups/{resource_group}/providers/Microsoft.Web/sites/{function_app_name}/functions/document_chunking/keys/mykey?api-version=2022-03-01"
requestHeaders = {
"Authorization": accessToken,
"Content-Type": "application/json"
}
data = {
"properties": {
"name": "mykey" # Omit the 'value' field to let Azure generate the key
}
}
response = requests.put(requestUrl, headers=requestHeaders, json=data)
response_json = json.loads(response.content.decode('utf-8'))
# print(response_json)
try:
function_key = response_json['properties']['value']
except Exception as e:
function_key = None
logging.error(f"Error when getting function key. Details: {str(e)}.")
return function_key
def approve_private_link_connections(access_token, subscription_id, resource_group, service_name, service_type, api_version):
"""
Approves private link service connections for a given service using
the "GET-then-PUT" pattern to ensure all required fields are present.
Args:
access_token (str): The access token used for authorization.
subscription_id (str): The subscription ID.
resource_group (str): The resource group name.
service_name (str): The name of the service.
service_type (str): The type of the service (e.g., 'Microsoft.Storage/storageAccounts').
api_version (str): The API version.
Returns:
None
Note:
Instead of raising an exception on errors, we log a warning.
This updated version performs a "GET-then-PUT" for each connection
to avoid 'InvalidValuesForRequestParameters' errors.
"""
logging.info(f"[approve_private_link_connections] Access token: {access_token[:10]}...")
logging.info(f"[approve_private_link_connections] Subscription ID: {subscription_id}")
logging.info(f"[approve_private_link_connections] Resource group: {resource_group}")
logging.info(f"[approve_private_link_connections] Service name: {service_name}")
logging.info(f"[approve_private_link_connections] Service type: {service_type}")
logging.info(f"[approve_private_link_connections] API version: {api_version}")
# List all private endpoint connections for the given resource
list_url = (
f"https://management.azure.com/subscriptions/{subscription_id}"
f"/resourceGroups/{resource_group}/providers/{service_type}/{service_name}"
f"/privateEndpointConnections?api-version={api_version}"
)
logging.debug(f"[approve_private_link_connections] Request URL: {list_url}")
request_headers = {
"Authorization": access_token,
"Content-Type": "application/json"
}
try:
response = requests.get(list_url, headers=request_headers)
response.raise_for_status()
response_json = response.json()
if 'value' not in response_json:
logging.error(
f"Unexpected response structure when fetching private link connections. "
f"Response content: {response.content}"
)
return # No connections to approve or error in structure
# Iterate over all connections in the array
for connection in response_json["value"]:
connection_id = connection["id"] # Full ARM ID
connection_name = connection["name"]
status = connection["properties"]["privateLinkServiceConnectionState"]["status"]
logging.info(f"[approve_private_link_connections] Checking connection '{connection_name}'. Status: {status}.")
# Approve only if status is 'Pending'
if status.lower()== "pending":
# 1) GET the entire connection resource so we can PUT it back intact
single_connection_url = f"https://management.azure.com{connection_id}?api-version={api_version}"
logging.debug(f"[approve_private_link_connections] GET single connection URL: {single_connection_url}")
try:
single_conn_response = requests.get(single_connection_url, headers=request_headers)
single_conn_response.raise_for_status()
full_conn_resource = single_conn_response.json()
except requests.HTTPError as http_err:
logging.warning(
f"Failed to GET full connection resource for '{connection_name}': {http_err}. "
f"Response: {single_conn_response.text if 'single_conn_response' in locals() else ''}"
)
continue
# 2) Update the status to "Approved" within the retrieved resource
full_conn_resource["properties"]["privateLinkServiceConnectionState"]["status"] = "Approved"
full_conn_resource["properties"]["privateLinkServiceConnectionState"]["description"] = "Approved by setup script"
# 3) PUT the entire resource (with updated status)
logging.debug(f"[approve_private_link_connections] PUT single connection URL: {single_connection_url}")
approve_response = requests.put(single_connection_url, headers=request_headers, json=full_conn_resource)
if approve_response.status_code in [200, 202]:
logging.info(
f"Approved private endpoint connection '{connection_name}' for service '{service_name}'."
)
else:
logging.warning(
f"Warning: Failed to approve private endpoint connection '{connection_name}' "
f"for service '{service_name}'. Status Code: {approve_response.status_code}, "
f"Response: {approve_response.text}"
)
elif status.lower() == "approved":
logging.info(f"[approve_private_link_connections] Connection '{connection_name}' is already Approved. Skipping re-approval.")
continue
except requests.HTTPError as http_err:
logging.warning(
f"HTTP error occurred when listing/approving private link connections: {http_err}. "
f"Response: {response.text}"
)
except Exception as e:
logging.warning(f"Error occurred when approving private link connections: {e}")
def approve_search_shared_private_access(subscription_id, resource_group, storage_resource_group, aoai_resource_group, function_app_name, storage_account_name, openai_service_name, credential):
"""
Approves Shared Private Access requests for private endpoints for AI Search, storage account, function app, and Azure OpenAI Service.
Args:
subscription_id (str): The subscription ID.
resource_group (str): The resource group name.
function_app_name (str): The name of the function app.
storage_account_name (str): The name of the storage account.
openai_service_name (str): The name of the Azure OpenAI service.
Returns:
None
Raises:
Exception: If approval fails.
"""
try:
logging.info("Approving Shared Private Access requests for storage, function app, and Azure OpenAI Service if needed.")
# Obtain the access token
try:
token_response = credential.get_token("https://management.azure.com/.default")
access_token = f"Bearer {token_response.token}"
logging.info("Obtained access token successfully.")
except ClientAuthenticationError as e:
logging.error(f"Authentication failed when obtaining access token: {e}")
raise
except Exception as e:
logging.error(f"Unexpected error when obtaining access token: {e}")
raise
# Approve private link connection for storage account
try:
approve_private_link_connections(
access_token,
subscription_id,
storage_resource_group,
storage_account_name,
'Microsoft.Storage/storageAccounts',
'2023-01-01'
)
logging.info(f"[approve_private_link_connections] Approved private link connections for Storage Account: {storage_account_name}.")
except Exception as e:
logging.error(f"Failed to approve private link connections for Storage Account '{storage_account_name}': {e}")
raise
# Approve private link connection for function app
try:
approve_private_link_connections(
access_token,
subscription_id,
resource_group,
function_app_name,
'Microsoft.Web/sites',
'2022-09-01'
)
logging.info(f"[approve_private_link_connections] Approved private link connections for Function App: {function_app_name}.")
except Exception as e:
logging.error(f"Failed to approve private link connections for Function App '{function_app_name}': {e}")
raise
# Approve private link connection for Azure OpenAI Service
try:
approve_private_link_connections(
access_token,
subscription_id,
aoai_resource_group,
openai_service_name,
'Microsoft.CognitiveServices/accounts',
'2022-10-01'
)
logging.info(f"Approved private link connections for Azure OpenAI Service: {openai_service_name}.")
except Exception as e:
logging.error(f"Failed to approve private link connections for Azure OpenAI Service '{openai_service_name}': {e}")
raise
except Exception as e:
error_message = str(e)
logging.error(f"Error when approving private link service connection. Please do it manually. Error: {error_message}")
raise
def execute_setup(subscription_id, resource_group, function_app_name, search_principal_id, azure_search_use_mis, enable_managed_identities, enable_env_credentials):
"""
This function performs the necessary steps to set up the ingestion sub components, such as creating the required datastores and indexers.
Args:
subscription_id (str): The subscription ID of the Azure subscription to use.
resource_group (str): The name of the resource group containing the solution resources.
function_app_name (str): The name of the function app to use.
search_principal_id (str): The principal ID of the search managed identity.
azure_search_use_mis (bool): Whether to use Search Service Managed Identity to Connect to data ingestion function
enable_managed_identities (bool, optional): Whether to use VM's managed identities to run the setup, defaults to False.
enable_env_credentials (bool): Whether to use environment credentials to run the setup.
Returns:
None
"""
logging.info(f"Getting function app {function_app_name} properties.")
credential = ChainedTokenCredential(
ManagedIdentityCredential(),
AzureCliCredential()
)
web_mgmt_client = WebSiteManagementClient(credential, subscription_id)
function_app_settings = web_mgmt_client.web_apps.list_application_settings(resource_group, function_app_name)
function_endpoint = f"https://{function_app_name}.azurewebsites.net"
azure_openai_service_name = function_app_settings.properties["AZURE_OPENAI_SERVICE_NAME"]
search_service = function_app_settings.properties["AZURE_SEARCH_SERVICE"]
search_analyzer_name= function_app_settings.properties["SEARCH_ANALYZER_NAME"]
search_api_version = function_app_settings.properties.get("SEARCH_API_VERSION", "2024-07-01")
search_index_interval = function_app_settings.properties["SEARCH_INDEX_INTERVAL"]
search_index_name = function_app_settings.properties["SEARCH_INDEX_NAME"]
storage_container = function_app_settings.properties["STORAGE_CONTAINER"]
storage_account_name = function_app_settings.properties["STORAGE_ACCOUNT_NAME"]
network_isolation = True if function_app_settings.properties["NETWORK_ISOLATION"].lower() == "true" else False
storage_container = function_app_settings.properties["STORAGE_CONTAINER"]
storage_account_name = function_app_settings.properties["STORAGE_ACCOUNT_NAME"]
azure_openai_embedding_deployment = function_app_settings.properties.get("AZURE_OPENAI_EMBEDDING_DEPLOYMENT", "text-embedding")
azure_openai_embedding_model = function_app_settings.properties.get("AZURE_OPENAI_EMBEDDING_MODEL", "text-embedding-3-large")
azure_embeddings_vector_size = function_app_settings.properties.get("AZURE_EMBEDDINGS_VECTOR_SIZE", "3072")
azure_storage_resource_group = function_app_settings.properties["AZURE_STORAGE_ACCOUNT_RG"]
azure_aoai_resource_group = function_app_settings.properties["AZURE_AOAI_RG"]
logging.info(f"[execute_setup] Function endpoint: {function_endpoint}")
logging.info(f"[execute_setup] Search service: {search_service}")
logging.info(f"[execute_setup] Search analyzer name: {search_analyzer_name}")
logging.info(f"[execute_setup] Search API version: {search_api_version}")
logging.info(f"[execute_setup] Search index interval: {search_index_interval}")
logging.info(f"[execute_setup] Search index name: {search_index_name}")
logging.info(f"[execute_setup] Storage container: {storage_container}")
logging.info(f"[execute_setup] Storage account name: {storage_account_name}")
logging.info(f"[execute_setup] Embedding deployment name: {azure_openai_embedding_deployment}")
logging.info(f"[execute_setup] Embedding model: {azure_openai_embedding_model}")
logging.info(f"[execute_setup] Embedding vector size: {azure_embeddings_vector_size}")
logging.info(f"[execute_setup] Resource group: {resource_group}")
logging.info(f"[execute_setup] Storage resource group: {azure_storage_resource_group}")
logging.info(f"[execute_setup] Azure OpenAI resource group: {azure_aoai_resource_group}")
# NL2SQL Elements
storage_container_nl2sql = "nl2sql"
search_index_name_nl2sql_queries = "nl2sql-queries"
search_index_name_nl2sql_tables = "nl2sql-tables"
search_index_name_nl2sql_columns = "nl2sql-columns"
logging.info(f"[execute_setup] NL2SQL Storage container: {storage_container_nl2sql}")
logging.info(f"[execute_setup] NL2SQL Search index name (queries): {search_index_name_nl2sql_queries}")
logging.info(f"[execute_setup] NL2SQL Search index name (tables): {search_index_name_nl2sql_tables}")
logging.info(f"[execute_setup] NL2SQL Search index name (columns): {search_index_name_nl2sql_columns}")
###########################################################################
# Get function key to be used later when creating the skillset
###########################################################################
function_key = get_function_key(subscription_id, resource_group, function_app_name, credential)
if function_key is None:
logging.error(f"Could not get function key. Please make sure the function {function_app_name}/document_chunking is deployed before running this script.")
exit(1)
###########################################################################
# Approve Search Shared Private Links (if needed)
###########################################################################
logging.info("Approving search shared private links.")
approve_search_shared_private_access(subscription_id, resource_group, azure_storage_resource_group, azure_aoai_resource_group, function_app_name, storage_account_name, azure_openai_service_name, credential)
###########################################################################
# Creating blob containers
###########################################################################
# Note: this step was removed since the storage account and container are already created by azd provision
###############################################################################
# Creating AI Search datasource
###############################################################################
def create_datasource(search_service, search_api_version, datasource_name, storage_connection_string, container_name, credential, subfolder=None):
body = {
"description": f"Datastore for {datasource_name}",
"type": "azureblob",
"dataDeletionDetectionPolicy": {
"@odata.type": "#Microsoft.Azure.Search.NativeBlobSoftDeleteDeletionDetectionPolicy"
},
"credentials": {
"connectionString": storage_connection_string
},
"container": {
"name": container_name,
"query": f"{subfolder}/" if subfolder else "" # Adding subfolder path if provided
}
}
call_search_api(search_service, search_api_version, "datasources", f"{datasource_name}-datasource", "put", credential, body)
logging.info("Creating datasources step.")
start_time = time.time()
# Define storage connection string without account key
# TODO: Use storage account resource group
storage_connection_string = f"ResourceId=/subscriptions/{subscription_id}/resourceGroups/{azure_storage_resource_group}/providers/Microsoft.Storage/storageAccounts/{storage_account_name}/;"
# Creating main datasource
create_datasource(search_service, search_api_version, f"{search_index_name}", storage_connection_string, storage_container, credential)
# Creating NL2SQL datasource in different subfolders
nl2sql_subfolders = {
"queries": search_index_name_nl2sql_queries,
"tables": search_index_name_nl2sql_tables,
"columns": search_index_name_nl2sql_columns
}
for subfolder, index_name in nl2sql_subfolders.items():
create_datasource(search_service, search_api_version, index_name, storage_connection_string, "nl2sql", credential, subfolder=subfolder)
response_time = time.time() - start_time
logging.info(f"Create datastores step. {round(response_time, 2)} seconds")
###############################################################################
# Creating indexes
###############################################################################
def create_index_body(index_name, fields, content_field_name, keyword_field_name, vector_dimensions, vector_profile_name="myHnswProfile", vector_algorithm_name="myHnswConfig", dimensions=3072):
body = {
"name": index_name,
"fields": fields,
"corsOptions": {
"allowedOrigins": ["*"],
"maxAgeInSeconds": 60
},
"vectorSearch": {
"profiles": [
{
"name": vector_profile_name,
"algorithm": vector_algorithm_name
}
],
"algorithms": [
{
"name": vector_algorithm_name,
"kind": "hnsw",
"hnswParameters": {
"m": 4,
"efConstruction": 400,
"efSearch": 500,
"metric": "cosine"
}
}
]
},
"semantic": {
"configurations": [
{
"name": "my-semantic-config",
"prioritizedFields": {
"prioritizedContentFields": [
{
"fieldName": content_field_name
}
],
"prioritizedKeywordsFields": [
{
"fieldName": keyword_field_name
}
]
}
}
]
}
}
return body
logging.info("Creating indexes.")
start_time = time.time()
# Common vector search configurations
vector_profile_name = "myHnswProfile"
vector_algorithm_name = "myHnswConfig"
# Define index configurations
indices = [
{
"index_name": search_index_name, # RAG index
"fields": [
{
"name": "id",
"type": "Edm.String",
"key": True,
"analyzer": "keyword",
"searchable": True,
"retrievable": True
},
{
"name": "parent_id",
"type": "Edm.String",
"searchable": False,
"retrievable": True
},
{
"name": "metadata_storage_path",
"type": "Edm.String",
"searchable": False,
"sortable": False,
"filterable": False,
"facetable": False
},
{
"name": "metadata_storage_name",
"type": "Edm.String",
"searchable": False,
"sortable": False,
"filterable": False,
"facetable": False
},
{
"name": "metadata_storage_last_modified",
"type": "Edm.DateTimeOffset",
"searchable": False,
"sortable": True,
"retrievable": True,
"filterable": True
},
{
"name": "metadata_security_id",
"type": "Collection(Edm.String)",
"searchable": False,
"retrievable": True,
"filterable": True
},
{
"name": "chunk_id",
"type": "Edm.Int32",
"searchable": False,
"retrievable": True
},
{
"name": "content",
"type": "Edm.String",
"searchable": True,
"retrievable": True,
"analyzer": search_analyzer_name
},
{
"name": "page",
"type": "Edm.Int32",
"searchable": False,
"retrievable": True
},
{
"name": "offset",
"type": "Edm.Int64",
"filterable": False,
"searchable": False,
"retrievable": True
},
{
"name": "length",
"type": "Edm.Int32",
"filterable": False,
"searchable": False,
"retrievable": True
},
{
"name": "title",
"type": "Edm.String",
"filterable": True,
"searchable": True,
"retrievable": True,
"analyzer": search_analyzer_name
},
{
"name": "category",
"type": "Edm.String",
"filterable": True,
"searchable": True,
"retrievable": True,
"analyzer": search_analyzer_name
},
{
"name": "filepath",
"type": "Edm.String",
"filterable": False,
"searchable": False,
"retrievable": True
},
{
"name": "url",
"type": "Edm.String",
"filterable": False,
"searchable": False,
"retrievable": True
},
{
"name": "summary",
"type": "Edm.String",
"filterable": False,
"searchable": True,
"retrievable": True
},
{
"name": "relatedImages",
"type": "Collection(Edm.String)",
"filterable": False,
"searchable": False,
"retrievable": True
},
{
"name": "relatedFiles",
"type": "Collection(Edm.String)",
"filterable": False,
"searchable": False,
"retrievable": True
},
{
"name": "source",
"type": "Edm.String",
"searchable": False,
"retrievable": True,
"filterable": True
},
{
"name": "contentVector",
"type": "Collection(Edm.Single)",
"searchable": True,
"retrievable": True,
"dimensions": azure_embeddings_vector_size,
"vectorSearchProfile": vector_profile_name
}
],
"content_field_name": "content",
"keyword_field_name": "category",
"vector_dimensions": azure_embeddings_vector_size
},
{
"index_name": search_index_name_nl2sql_queries,
"fields": [
{
"name": "id",
"type": "Edm.String",
"key": True,
"searchable": False,
"filterable": False,
"sortable": False,
"facetable": False
},
{
"name": "question",
"type": "Edm.String",
"searchable": True,
"filterable": False,
"retrievable": True,
"sortable": False,
"facetable": False,
"analyzer": "standard.lucene"
},
{
"name": "query",
"type": "Edm.String",
"searchable": False,
"filterable": False,
"retrievable": True,
"sortable": False,
"facetable": False
},
{
"name": "selected_tables",
"type": "Collection(Edm.String)",
"searchable": False,
"filterable": False,
"retrievable": True
},
{
"name": "selected_columns",
"type": "Collection(Edm.String)",
"searchable": False,
"filterable": False,
"retrievable": True
},
{
"name": "reasoning",
"type": "Edm.String",
"searchable": True,
"filterable": False,
"retrievable": True,
"sortable": False,
"facetable": False
},
{
"name": "contentVector",
"type": "Collection(Edm.Single)",
"searchable": True,
"retrievable": True,
"dimensions": azure_embeddings_vector_size,
"vectorSearchProfile": vector_profile_name
}
],
"content_field_name": "question",
"keyword_field_name": "question",
"vector_dimensions": azure_embeddings_vector_size
},
{
"index_name": search_index_name_nl2sql_tables,
"fields": [
{
"name": "id",
"type": "Edm.String",
"key": True,
"searchable": False,
"filterable": False,
"sortable": False,
"facetable": False
},
{
"name": "table_name",
"type": "Edm.String",
"searchable": True,
"filterable": False,
"retrievable": True,
"sortable": False,
"facetable": False,
"analyzer": "standard.lucene"
},
{
"name": "description",
"type": "Edm.String",
"searchable": True,
"filterable": False,
"retrievable": True,
"sortable": False,
"facetable": False,
"analyzer": "standard.lucene"
},
{
"name": "contentVector",
"type": "Collection(Edm.Single)",
"searchable": True,
"retrievable": True,
"dimensions": azure_embeddings_vector_size,
"vectorSearchProfile": vector_profile_name
}
],
"content_field_name": "description",
"keyword_field_name": "description",
"vector_dimensions": azure_embeddings_vector_size
},
{
"index_name": search_index_name_nl2sql_columns,
"fields": [
{
"name": "id",
"type": "Edm.String",
"key": True,
"searchable": False,
"filterable": False,
"sortable": False,
"facetable": False
},
{
"name": "table_name",
"type": "Edm.String",
"searchable": True,
"filterable": True,
"retrievable": True,
"sortable": False,
"facetable": False,
"analyzer": "standard.lucene"
},
{
"name": "column_name",
"type": "Edm.String",
"searchable": True,
"filterable": False,
"retrievable": True,
"sortable": False,
"facetable": False,
"analyzer": "standard.lucene"
},
{
"name": "description",
"type": "Edm.String",
"searchable": True,
"filterable": False,
"retrievable": True,
"sortable": False,
"facetable": False,
"analyzer": "standard.lucene"
},
{
"name": "contentVector",
"type": "Collection(Edm.Single)",
"searchable": True,
"retrievable": True,
"dimensions": azure_embeddings_vector_size,
"vectorSearchProfile": vector_profile_name
}
],
"content_field_name": "description",
"keyword_field_name": "description",
"vector_dimensions": azure_embeddings_vector_size
}
]
# Iterate over each index configuration and create the index
for index in indices:
body = create_index_body(
index_name=index["index_name"],
fields=index["fields"],
content_field_name=index["content_field_name"],
keyword_field_name=index["keyword_field_name"],
vector_dimensions=index["vector_dimensions"],
vector_profile_name=vector_profile_name,
vector_algorithm_name=vector_algorithm_name,
dimensions=azure_embeddings_vector_size
)
# Delete existing index if it exists
call_search_api(search_service, search_api_version, "indexes", index["index_name"], "delete", credential)
# Create the index
call_search_api(search_service, search_api_version, "indexes", index["index_name"], "put", credential, body)
response_time = time.time() - start_time
logging.info(f"Indexes created in {round(response_time, 2)} seconds")
###########################################################################
# 04 Creating AI Search skillsets
###########################################################################
logging.info("04 Creating skillsets step.")
start_time = time.time()
body = {
"name": f"{search_index_name}-skillset-chunking",
"description":"SKillset to do document chunking",
"skills":[
{
"@odata.type":"#Microsoft.Skills.Custom.WebApiSkill",
"name":"document-chunking",
"description":"Extract chunks from documents.",
"httpMethod":"POST",
"timeout":"PT230S",
"context":"/document",
"batchSize":1,
"inputs":[
{
"name":"documentUrl",
"source":"/document/metadata_storage_path"
},
{
"name":"documentSasToken",
"source":"/document/metadata_storage_sas_token"
},
{
"name":"documentContentType",
"source":"/document/metadata_content_type"
}
],
"outputs":[
{
"name":"chunks",
"targetName":"chunks"
}
]
}
],
"indexProjections": {
"selectors": [
{
"targetIndexName":f"{search_index_name}",
"parentKeyFieldName": "parent_id",
"sourceContext": "/document/chunks/*",
"mappings": [
{
"name": "chunk_id",
"source": "/document/chunks/*/chunk_id",
"inputs": []
},
{
"name": "offset",
"source": "/document/chunks/*/offset",
"inputs": []
},
{
"name": "length",
"source": "/document/chunks/*/length",
"inputs": []
},
{
"name": "page",
"source": "/document/chunks/*/page",
"inputs": []
},
{
"name": "title",
"source": "/document/chunks/*/title",
"inputs": []
},
{
"name": "category",
"source": "/document/chunks/*/category",
"inputs": []
},
{
"name": "url",
"source": "/document/chunks/*/url",
"inputs": []
},
{
"name": "relatedImages",
"source": "/document/chunks/*/relatedImages",
"inputs": []
},
{
"name": "relatedFiles",
"source": "/document/chunks/*/relatedFiles",
"inputs": []
},
{
"name": "filepath",
"source": "/document/chunks/*/filepath",
"inputs": []
},
{
"name": "content",
"source": "/document/chunks/*/content",
"inputs": []
},
{
"name": "summary",
"source": "/document/chunks/*/summary",
"inputs": []
},
{
"name": "source",
"source": "/document/chunks/*/source",
"inputs": []
},
{
"name": "contentVector",
"source": "/document/chunks/*/contentVector",
"inputs": []
},
{
"name": "metadata_storage_last_modified",
"source": "/document/metadata_storage_last_modified",
"inputs": []
},
{
"name": "metadata_storage_name",
"source": "/document/metadata_storage_name",
"inputs": []
},
{
"name": "metadata_storage_path",
"source": "/document/metadata_storage_path",
"inputs": []
},
{
"name": "metadata_security_id",
"source": "/document/metadata_security_id",
"inputs": []
}
]
}
],
"parameters": {
"projectionMode": "skipIndexingParentDocuments"
}
}
}
if azure_search_use_mis:
body['skills'][0]['uri'] = f"{function_endpoint}/api/document-chunking"
body['skills'][0]['authResourceId'] = f"api://{search_principal_id}"
else:
body['skills'][0]['uri'] = f"{function_endpoint}/api/document-chunking?code={function_key}"
# first delete to enforce web api skillset to be updated
call_search_api(search_service, search_api_version, "skillsets", f"{search_index_name}-skillset-chunking", "delete", credential)
call_search_api(search_service, search_api_version, "skillsets", f"{search_index_name}-skillset-chunking", "put", credential, body)
# creating skill sets for the NL2SQL indexes
def create_embedding_skillset(skillset_name, resource_uri, deployment_id, model_name, input_field, output_field, dimensions):
skill = {
"@odata.type": "#Microsoft.Skills.Text.AzureOpenAIEmbeddingSkill",
"name": f"{skillset_name}-embedding-skill",