1- from azure .ai .ml .entities import ManagedOnlineEndpoint , ManagedOnlineDeployment , Model , Environment , BuildContext
2-
3- import os
1+ import os , uuid
2+ # set environment variables before importing any other code
43from dotenv import load_dotenv
54load_dotenv ()
65
6+ from azure .ai .ml .entities import ManagedOnlineEndpoint , ManagedOnlineDeployment , Model , Environment , BuildContext
7+ from azure .identity import DefaultAzureCredential
8+ from azure .mgmt .authorization import AuthorizationManagementClient
9+ from azure .mgmt .authorization .models import RoleAssignmentCreateParameters
710
811from helper_functions import get_client , get_ai_studio_url_for_deploy
912
@@ -20,10 +23,11 @@ def deploy_flow(endpoint_name, deployment_name):
2023 name = endpoint_name ,
2124 properties = {
2225 "enforce_access_to_default_secret_stores" : "enabled" # if you want secret injection support
23- }
26+ },
27+ auth_mode = "aad_token" # using aad auth instead of key-based auth
2428 )
2529
26- deployment = ManagedOnlineDeployment ( # defaults to key auth_mode
30+ deployment = ManagedOnlineDeployment (
2731 name = deployment_name ,
2832 endpoint_name = endpoint_name ,
2933 model = Model (
@@ -64,29 +68,79 @@ def deploy_flow(endpoint_name, deployment_name):
6468 "PRT_CONFIG_OVERRIDE" : f"deployment.subscription_id={ client .subscription_id } ,deployment.resource_group={ client .resource_group_name } ,deployment.workspace_name={ client .workspace_name } ,deployment.endpoint_name={ endpoint_name } ,deployment.deployment_name={ deployment_name } " ,
6569 # the following is enabled by secret injection
6670 # make sure your environment variables here match the environment variables your code depends on
67- 'AZURE_OPENAI_ENDPOINT' : os .getenv ('AZURE_OPENAI_ENDPOINT' ),
68- 'AZURE_OPENAI_API_KEY' : os .getenv ('AZURE_OPENAI_API_KEY' ),
69- 'AZURE_SEARCH_ENDPOINT' : os .getenv ('AZURE_SEARCH_ENDPOINT' ),
70- 'AZURE_SEARCH_KEY' : os .getenv ('AZURE_SEARCH_KEY' ),
71- 'AZURE_OPENAI_API_VERSION' : os .getenv ('AZURE_OPENAI_API_VERSION' ),
72- 'AZURE_OPENAI_CHAT_DEPLOYMENT' : os .getenv ('AZURE_OPENAI_CHAT_DEPLOYMENT' ),
73- 'AZURE_OPENAI_EVALUATION_DEPLOYMENT' : os .getenv ('AZURE_OPENAI_EVALUATION_DEPLOYMENT' ),
74- 'AZURE_OPENAI_EMBEDDING_DEPLOYMENT' : os .getenv ('AZURE_OPENAI_EMBEDDING_DEPLOYMENT' ),
75- 'AZUREAI_SEARCH_INDEX_NAME' : os .getenv ('AZUREAI_SEARCH_INDEX_NAME' )
71+ 'AZURE_OPENAI_ENDPOINT' : os .environ ['AZURE_OPENAI_ENDPOINT' ],
72+ 'AZURE_SEARCH_ENDPOINT' : os .environ ['AZURE_SEARCH_ENDPOINT' ],
73+ 'AZURE_OPENAI_API_VERSION' : os .environ ['AZURE_OPENAI_API_VERSION' ],
74+ 'AZURE_OPENAI_CHAT_DEPLOYMENT' : os .environ ['AZURE_OPENAI_CHAT_DEPLOYMENT' ],
75+ 'AZURE_OPENAI_EVALUATION_DEPLOYMENT' : os .environ ['AZURE_OPENAI_EVALUATION_DEPLOYMENT' ],
76+ 'AZURE_OPENAI_EMBEDDING_DEPLOYMENT' : os .environ ['AZURE_OPENAI_EMBEDDING_DEPLOYMENT' ],
77+ 'AZUREAI_SEARCH_INDEX_NAME' : os .environ ['AZUREAI_SEARCH_INDEX_NAME' ]
7678 }
7779 )
7880
7981 # 1. create endpoint
80- client .begin_create_or_update (endpoint ).result () # result() means we wait on this to complete
82+ endpoint = client .begin_create_or_update (endpoint ).result () # result() means we wait on this to complete
8183
8284 # 2. create deployment
83- client .begin_create_or_update (deployment ).result ()
85+ deployment = client .begin_create_or_update (deployment ).result ()
8486
8587 # 3. update endpoint traffic for the deployment
8688 endpoint .traffic = {deployment_name : 100 } # 100% of traffic
87- client .begin_create_or_update (endpoint ).result ()
88-
89- output_deployment_details (client , endpoint_name , deployment_name )
89+ endpoint = client .begin_create_or_update (endpoint ).result ()
90+
91+ # 4. provide endpoint access to Azure Open AI resource
92+ create_role_assignment (
93+ scope = f"/subscriptions/{ os .environ ["AZURE_SUBSCRIPTION_ID" ]} /resourceGroups/{ os .environ ["AZURE_RESOURCE_GROUP" ]} /providers/Microsoft.CognitiveServices/accounts/{ os .environ ["AZURE_OPENAI_CONNECTION_NAME" ]} " ,
94+ role_name = "Cognitive Services OpenAI User" ,
95+ principal_id = endpoint .identity .principal_id
96+ )
97+
98+ # 5. provide endpoint access to Azure AI Search resource
99+ create_role_assignment (
100+ scope = f"/subscriptions/{ os .environ ["AZURE_SUBSCRIPTION_ID" ]} /resourceGroups/{ os .environ ["AZURE_RESOURCE_GROUP" ]} /providers/Microsoft.Search/searchServices/{ os .environ ["AZURE_SEARCH_CONNECTION_NAME" ]} " ,
101+ role_name = "Search Index Data Contributor" ,
102+ principal_id = endpoint .identity .principal_id
103+ )
104+
105+ output_deployment_details (
106+ client = client ,
107+ endpoint_name = endpoint_name ,
108+ deployment_name = deployment_name
109+ )
110+
111+ def create_role_assignment (scope , role_name , principal_id ):
112+
113+ # Get credential
114+ credential = DefaultAzureCredential ()
115+
116+ # Instantiate the authorization management client
117+ auth_client = AuthorizationManagementClient (
118+ credential = credential ,
119+ subscription_id = os .environ ["AZURE_SUBSCRIPTION_ID" ]
120+ )
121+
122+ roles = list (auth_client .role_definitions .list (
123+ scope ,
124+ filter = "roleName eq '{}'" .format (role_name )))
125+
126+ assert len (roles ) == 1
127+ role = roles [0 ]
128+
129+ # Create role assignment properties
130+ parameters = RoleAssignmentCreateParameters (
131+ role_definition_id = role .id ,
132+ principal_id = principal_id ,
133+ principal_type = "ServicePrincipal"
134+ )
135+
136+ # Create role assignment
137+ role_assignment = auth_client .role_assignments .create (
138+ scope = scope ,
139+ role_assignment_name = uuid .uuid4 (),
140+ parameters = parameters
141+ )
142+
143+ return role_assignment
90144
91145def output_deployment_details (client , endpoint_name , deployment_name ) -> str :
92146 print ("\n ~~~Deployment details~~~" )
@@ -107,4 +161,4 @@ def output_deployment_details(client, endpoint_name, deployment_name) -> str:
107161 endpoint_name = args .endpoint_name if args .endpoint_name else f"rag-copilot-endpoint"
108162 deployment_name = args .deployment_name if args .deployment_name else f"rag-copilot-deployment"
109163
110- deploy_flow (endpoint_name , deployment_name )
164+ deploy_flow (endpoint_name , deployment_name )
0 commit comments