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 (
@@ -65,9 +69,7 @@ def deploy_flow(endpoint_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
6771 'AZURE_OPENAI_ENDPOINT' : os .getenv ('AZURE_OPENAI_ENDPOINT' ),
68- 'AZURE_OPENAI_API_KEY' : os .getenv ('AZURE_OPENAI_API_KEY' ),
6972 'AZURE_SEARCH_ENDPOINT' : os .getenv ('AZURE_SEARCH_ENDPOINT' ),
70- 'AZURE_SEARCH_KEY' : os .getenv ('AZURE_SEARCH_KEY' ),
7173 'AZURE_OPENAI_API_VERSION' : os .getenv ('AZURE_OPENAI_API_VERSION' ),
7274 'AZURE_OPENAI_CHAT_DEPLOYMENT' : os .getenv ('AZURE_OPENAI_CHAT_DEPLOYMENT' ),
7375 'AZURE_OPENAI_EVALUATION_DEPLOYMENT' : os .getenv ('AZURE_OPENAI_EVALUATION_DEPLOYMENT' ),
@@ -77,16 +79,62 @@ def deploy_flow(endpoint_name, deployment_name):
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 .getenv ("AZURE_SUBSCRIPTION_ID" )} /resourceGroups/{ os .getenv ("AZURE_RESOURCE_GROUP" )} /providers/Microsoft.CognitiveServices/accounts/{ os .getenv ("AZURE_OPENAI_CONNECTION_NAME" )} " ,
94+ role_name = "Cognitive Services OpenAI User" ,
95+ principal_id = endpoint .identity .principal_id )
96+
97+ # 5. provide endpoint access to Azure AI Search resource
98+ create_role_assignment (
99+ scope = f"/subscriptions/{ os .getenv ("AZURE_SUBSCRIPTION_ID" )} /resourceGroups/{ os .getenv ("AZURE_RESOURCE_GROUP" )} /providers/Microsoft.Search/searchServices/{ os .getenv ("AZURE_SEARCH_CONNECTION_NAME" )} " ,
100+ role_name = "Search Index Data Contributor" ,
101+ principal_id = endpoint .identity .principal_id )
102+
103+ output_deployment_details (
104+ client = client ,
105+ endpoint_name = endpoint_name ,
106+ deployment_name = deployment_name )
107+
108+ def create_role_assignment (scope , role_name , principal_id ):
109+
110+ # Get credential
111+ credential = DefaultAzureCredential ()
112+
113+ # Instantiate the authorization management client
114+ auth_client = AuthorizationManagementClient (
115+ credential = credential ,
116+ subscription_id = os .getenv ("AZURE_SUBSCRIPTION_ID" ))
117+
118+ roles = list (auth_client .role_definitions .list (
119+ scope ,
120+ filter = "roleName eq '{}'" .format (role_name )))
121+
122+ assert len (roles ) == 1
123+ role = roles [0 ]
124+
125+ # Create role assignment properties
126+ parameters = RoleAssignmentCreateParameters (
127+ role_definition_id = role .id ,
128+ principal_id = principal_id ,
129+ principal_type = "ServicePrincipal" )
130+
131+ # Create role assignment
132+ role_assignment = auth_client .role_assignments .create (
133+ scope = scope ,
134+ role_assignment_name = uuid .uuid4 (),
135+ parameters = parameters )
136+
137+ return role_assignment
90138
91139def output_deployment_details (client , endpoint_name , deployment_name ) -> str :
92140 print ("\n ~~~Deployment details~~~" )
@@ -107,4 +155,4 @@ def output_deployment_details(client, endpoint_name, deployment_name) -> str:
107155 endpoint_name = args .endpoint_name if args .endpoint_name else f"rag-copilot-endpoint"
108156 deployment_name = args .deployment_name if args .deployment_name else f"rag-copilot-deployment"
109157
110- deploy_flow (endpoint_name , deployment_name )
158+ deploy_flow (endpoint_name , deployment_name )
0 commit comments