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
4
3
from dotenv import load_dotenv
5
4
load_dotenv ()
6
5
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
7
10
8
11
from helper_functions import get_client , get_ai_studio_url_for_deploy
9
12
@@ -20,10 +23,11 @@ def deploy_flow(endpoint_name, deployment_name):
20
23
name = endpoint_name ,
21
24
properties = {
22
25
"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
24
28
)
25
29
26
- deployment = ManagedOnlineDeployment ( # defaults to key auth_mode
30
+ deployment = ManagedOnlineDeployment (
27
31
name = deployment_name ,
28
32
endpoint_name = endpoint_name ,
29
33
model = Model (
@@ -64,29 +68,79 @@ def deploy_flow(endpoint_name, deployment_name):
64
68
"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 } " ,
65
69
# the following is enabled by secret injection
66
70
# 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' ]
76
78
}
77
79
)
78
80
79
81
# 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
81
83
82
84
# 2. create deployment
83
- client .begin_create_or_update (deployment ).result ()
85
+ deployment = client .begin_create_or_update (deployment ).result ()
84
86
85
87
# 3. update endpoint traffic for the deployment
86
88
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
90
144
91
145
def output_deployment_details (client , endpoint_name , deployment_name ) -> str :
92
146
print ("\n ~~~Deployment details~~~" )
@@ -107,4 +161,4 @@ def output_deployment_details(client, endpoint_name, deployment_name) -> str:
107
161
endpoint_name = args .endpoint_name if args .endpoint_name else f"rag-copilot-endpoint"
108
162
deployment_name = args .deployment_name if args .deployment_name else f"rag-copilot-deployment"
109
163
110
- deploy_flow (endpoint_name , deployment_name )
164
+ deploy_flow (endpoint_name , deployment_name )
0 commit comments