forked from Azure-Samples/genai-gateway-apim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.bicep
390 lines (348 loc) · 11.7 KB
/
main.bicep
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
// parameters
param publisherName string = 'myPublisherName'
param publisherEmail string = '[email protected]'
param apiName string = 'myAPI'
param productName string = 'APIM-AI_APIS'
param productDescription string = 'A product with AI APIs'
var APIM_NAME = 'APIM8'
var serviceName = 'service${uniqueString(resourceGroup().id)}-${APIM_NAME}'
var openai_first_endpoint_name = '${uniqueString(resourceGroup().id)}-${APIM_NAME}-AOAI1'
var openai_second_endpoint_name = '${uniqueString(resourceGroup().id)}-${APIM_NAME}-AOAI2'
// model version: 0613
param location string = 'eastus2' // resourceGroup().location
// FIRST: creating Azure Cognitive Services account for OpenAI
resource cognitiveServicesAccount1 'Microsoft.CognitiveServices/accounts@2023-10-01-preview' = {
name: openai_first_endpoint_name
location: location
sku: {
name: 'S0'
}
kind: 'OpenAI'
properties: {
customSubDomainName: openai_first_endpoint_name
networkAcls: {
defaultAction: 'Allow'
virtualNetworkRules: []
ipRules: []
}
publicNetworkAccess: 'Enabled'
}
identity: {
type: 'SystemAssigned'
}
}
// SECOND: creating Azure Cognitive Services account for OpenAI
resource cognitiveServicesAccount2 'Microsoft.CognitiveServices/accounts@2023-10-01-preview' = {
name: openai_second_endpoint_name
location: location
sku: {
name: 'S0'
}
kind: 'OpenAI'
properties: {
customSubDomainName: openai_second_endpoint_name
networkAcls: {
defaultAction: 'Allow'
virtualNetworkRules: []
ipRules: []
}
publicNetworkAccess: 'Enabled'
}
identity: {
type: 'SystemAssigned'
}
}
// creating 1st deployment for the OpenAI model
resource cognitiveServicesAccountDeployment1 'Microsoft.CognitiveServices/accounts/deployments@2023-10-01-preview' = {
parent: cognitiveServicesAccount1
name: 'conversation-model'
sku: {
name: 'Standard'
capacity: 2
}
properties: {
model: {
format: 'OpenAI'
name: 'gpt-35-turbo'
version: '0613'
}
versionUpgradeOption: 'OnceCurrentVersionExpired'
currentCapacity: 2
raiPolicyName: 'Microsoft.Default'
}
}
// create 2nd deployment for the OpenAI model
resource cognitiveServicesAccountDeployment2 'Microsoft.CognitiveServices/accounts/deployments@2023-10-01-preview' = {
parent: cognitiveServicesAccount2
name: 'conversation-model'
sku: {
name: 'Standard'
capacity: 2
}
properties: {
model: {
format: 'OpenAI'
name: 'gpt-35-turbo'
version: '0613'
}
versionUpgradeOption: 'OnceCurrentVersionExpired'
currentCapacity: 2
raiPolicyName: 'Microsoft.Default'
}
}
// Create API Management service
// NOTE: certain features are only available in certain regions, e.g 'westcentralus' for example until this is GA
resource apimService 'Microsoft.ApiManagement/service@2023-09-01-preview' = {
name: serviceName
location: 'southcentralus'
sku: {
name: 'StandardV2'
capacity: 1
}
properties: {
publisherEmail: publisherEmail
publisherName: publisherName
}
identity: {
type: 'SystemAssigned'
}
}
// ROLES
// role definition Cognitive Services User
param roleDefinitionId string = '5e0bd9bd-7b93-4f28-af87-19fc36ad61bd'
// Assign the role (to Cog service account 1), a role entry is added to Cognitive Services account with the following args:
// - roleDefinitionId (Cognitive Service User),
// - principalId (APIM service instance)
// - scope (Cognitive Services account)
resource roleAssignment 'Microsoft.Authorization/roleAssignments@2020-04-01-preview' = {
name: guid(cognitiveServicesAccount1.id, resourceGroup().id, APIM_NAME)
scope: cognitiveServicesAccount1
properties: {
roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', roleDefinitionId)
principalType: 'ServicePrincipal'
principalId: apimService.identity.principalId
}
}
// Assign the role (to Cog service account 2), a role entry is added to Cognitive Services account with the following args:
// - roleDefinitionId (Cognitive Service User),
// - principalId (APIM service instance)
// - scope (Cognitive Services account)
resource roleAssignment2 'Microsoft.Authorization/roleAssignments@2020-04-01-preview' = {
name: guid(cognitiveServicesAccount2.id, resourceGroup().id, APIM_NAME)
scope: cognitiveServicesAccount2
properties: {
roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', roleDefinitionId)
principalType: 'ServicePrincipal'
principalId: apimService.identity.principalId
}
}
param resourceGroupName string = resourceGroup().name
// 3 - POLICIES
// Creating a backend for Cog service account 1, also adding a circuit breaker rule
resource backend1 'Microsoft.ApiManagement/service/backends@2023-09-01-preview' = {
parent: apimService
name: 'backend1'
properties: {
url: '${cognitiveServicesAccount1.properties.endpoint}openai'
protocol: 'http'
circuitBreaker: {
rules: [
{
failureCondition: {
count: 3
errorReasons: [
'Server errors'
]
interval: 'P1D'
statusCodeRanges: [
{
min: 500
max: 599
}
]
}
name: 'myBreakerRule'
tripDuration: 'PT1H'
}
]
}
}
}
// Creating a backend for Cog service account 2, also adding a circuit breaker rule
resource backend2 'Microsoft.ApiManagement/service/backends@2023-09-01-preview' = {
parent: apimService
name: 'backend2'
properties: {
url: '${cognitiveServicesAccount2.properties.endpoint}openai'
protocol: 'http'
circuitBreaker: {
rules: [
{
failureCondition: {
count: 3
errorReasons: [
'Server errors'
]
interval: 'P1D'
statusCodeRanges: [
{
min: 500
max: 599
}
]
}
name: 'myBreakerRule'
tripDuration: 'PT1H'
}
]
}
}
}
var subscriptionId = az.subscription().subscriptionId
// Create a load balancer (pool) for the backends where backend1 and backend2 are added
resource loadBalancing 'Microsoft.ApiManagement/service/backends@2023-09-01-preview' = {
parent: apimService
name: 'LoadBalancer'
properties: {
description: 'Load balancer for multiple backends'
type: 'Pool'
pool: {
services: [
{
id: '/subscriptions/${subscriptionId}/resourceGroups/${resourceGroupName}/providers/Microsoft.ApiManagement/service/${serviceName}/backends/${backend1.id}'
}
{
id: '/subscriptions/${subscriptionId}/resourceGroups/${resourceGroupName}/providers/Microsoft.ApiManagement/service/${serviceName}/backends/${backend2.id}'
}
]
}
}
}
// Create Application Insights for API Management
resource appInsights 'Microsoft.Insights/components@2020-02-02' = {
name: serviceName
location: location
kind: 'web'
properties: {
Application_Type: 'web'
}
}
// Create a 'logger instance' on the API Management service that logs to Application Insights
resource aiLoggerWithSystemAssignedIdentity 'Microsoft.ApiManagement/service/loggers@2022-08-01' = {
parent: apimService
name: 'ailogger'
properties: {
loggerType: 'applicationInsights'
description: 'Application Insights logger with system-assigned managed identity'
credentials: {
connectionString: appInsights.properties.ConnectionString
identityClientId: 'systemAssigned'
}
}
}
// Create an API in the API Management service, and a diagnostic setting that logs to Application Insights
// NOTE: diagnostic instance is added on API level, not on the service level as in the previous example
resource api1 'Microsoft.ApiManagement/service/apis@2020-06-01-preview' = {
parent: apimService
name: apiName
properties: {
displayName: apiName
apiType: 'http'
path: '${apiName}/openai'
format: 'openapi+json-link'
value: 'https://raw.githubusercontent.com/Azure/azure-rest-api-specs/main/specification/cognitiveservices/data-plane/AzureOpenAI/inference/preview/2024-03-01-preview/inference.json'
subscriptionKeyParameterNames: {
header: 'api-key'
}
}
resource apimDiagnostics 'diagnostics@2023-05-01-preview' = {
name: 'applicationinsights' // Use a supported diagnostic identifier
properties: {
loggerId: '/subscriptions/${subscriptionId}/resourceGroups/${resourceGroup().name}/providers/Microsoft.ApiManagement/service/${apimService.name}/loggers/${aiLoggerWithSystemAssignedIdentity.name}'
metrics: true
}
}
}
// Creating a policy for the API, adding the following:
// - `set-backend-service policy` to route the request to the load balancer, backend-id is the load balancer id
// - `authentication-managed-identity` policy to get the managed identity token
// - `azure-openai-token-limit` policy to limit the number of requests to the API
// - `azure-openai-emit-token-metric` policy to emit a metric with the token
// - `set-header` policy to add the managed identity token to the request
var headerPolicyXml = format('''
<policies>
<inbound>
<base />
<set-backend-service id="apim-generated-policy" backend-id="{0}" />
<authentication-managed-identity resource="https://cognitiveservices.azure.com" output-token-variable-name="managed-id-access-token" ignore-error="false" />
<azure-openai-token-limit counter-key="@(context.Subscription.Id)" tokens-per-minute="{1}" estimate-prompt-tokens="false" retry-after-variable-name="token-limit-retry-after"/>
<azure-openai-emit-token-metric namespace="genaidemometrics">
<dimension name="Subscription ID" />
<dimension name="Client IP" value="@(context.Request.IpAddress)" />
</azure-openai-emit-token-metric>
<set-header name="Authorization" exists-action="override">
<value>@("Bearer " + (string)context.Variables["managed-id-access-token"])</value>
</set-header>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
''',loadBalancing.name, 5000)
// Create a policy for the API, using the headerPolicyXml variable
resource apiPolicy 'Microsoft.ApiManagement/service/apis/policies@2020-06-01-preview' = {
parent: api1
name: 'policy'
properties: {
format: 'rawxml'
value: headerPolicyXml
}
dependsOn: [
loadBalancing
]
}
// Creating a product for the API. Products are used to group APIs and apply policies to them
resource product 'Microsoft.ApiManagement/service/products@2020-06-01-preview' = {
parent: apimService
name: productName
properties: {
displayName: productName
description: productDescription
state: 'published'
subscriptionRequired: true
}
}
// Create PRODUCT-API association the API with the product
resource productApi1 'Microsoft.ApiManagement/service/products/apis@2020-06-01-preview' = {
parent: product
name: api1.name
}
// Creating a user for the API Management service
resource user 'Microsoft.ApiManagement/service/users@2020-06-01-preview' = {
parent: apimService
name: 'userName'
properties: {
firstName: 'User'
lastName: 'Name'
email: '[email protected]'
state: 'active'
}
}
// Creating a subscription for the API Management service
// NOTE: the subscription is associated with the user and the product, AND the subscription ID is what will be used in the request to authenticate the calling client
resource subscription 'Microsoft.ApiManagement/service/subscriptions@2020-06-01-preview' = {
parent: apimService
name: 'subscriptionAIProduct'
properties: {
displayName: 'Subscribing to AI services'
state: 'active'
ownerId: user.id
scope: product.id
}
}