-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathmain.bicep
558 lines (481 loc) · 15.8 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
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
@description('List of OpenAI resources to create for Pool 1. Add pairs of name and location.')
param openAIConfig_1 array = []
@description('List of OpenAI resources to create for Pool 2. Add pairs of name and location.')
param openAIConfig_2 array = []
@description('List of OpenAI resources to create for Pool 3. Add pairs of name and location.')
param openAIConfig_3 array = []
@description('Deployment 1 Name')
param openAIDeploymentName_1 string
@description('Deployment 2 Name')
param openAIDeploymentName_2 string
@description('Deployment 3 Name')
param openAIDeploymentName_3 string
@description('Azure OpenAI Sku')
@allowed([
'S0'
])
param openAISku string = 'S0'
@description('Model 1 Name')
param openAIModelName_1 string
@description('Model 1 Version')
param openAIModelVersion_1 string
@description('Model 2 Name')
param openAIModelName_2 string
@description('Model 2 Version')
param openAIModelVersion_2 string
@description('Model 3 Name')
param openAIModelName_3 string
@description('Model 3 Version')
param openAIModelVersion_3 string
@description('Model Capacity')
param openAIModelCapacity int = 20
@description('The name of the API Management resource')
param apimResourceName string
@description('Location for the APIM resource')
param apimResourceLocation string = resourceGroup().location
@description('The pricing tier of this API Management service')
@allowed([
'Consumption'
'Developer'
'Basic'
'Basicv2'
'Standard'
'Standardv2'
'Premium'
])
param apimSku string = 'Consumption'
@description('The instance size of this API Management service.')
@allowed([
0
1
2
])
param apimSkuCount int = 1
@description('The email address of the owner of the service')
param apimPublisherEmail string = '[email protected]'
@description('The name of the owner of the service')
param apimPublisherName string = 'Microsoft'
@description('The name of the APIM API for OpenAI API')
param openAIAPIName string = 'openai'
@description('The relative path of the APIM API for OpenAI API')
param openAIAPIPath string = 'openai'
@description('The display name of the APIM API for OpenAI API')
param openAIAPIDisplayName string = 'OpenAI'
@description('The description of the APIM API for OpenAI API')
param openAIAPIDescription string = 'Azure OpenAI API inferencing API'
@description('Full URL for the OpenAI API spec')
param openAIAPISpecURL string = 'https://raw.githubusercontent.com/Azure/azure-rest-api-specs/main/specification/cognitiveservices/data-plane/AzureOpenAI/inference/stable/2024-02-01/inference.json'
@description('The name of the APIM Subscription for OpenAI API')
param openAISubscriptionName string = 'openai-subscription'
@description('The description of the APIM Subscription for OpenAI API')
param openAISubscriptionDescription string = 'OpenAI Subscription'
@description('The name of the OpenAI backend pool 1')
param openAIBackendPoolName_1 string = 'openai-backend-pool-1'
@description('The name of the OpenAI backend pool 2')
param openAIBackendPoolName_2 string = 'openai-backend-pool-2'
@description('The name of the OpenAI backend pool 3')
param openAIBackendPoolName_3 string = 'openai-backend-pool-3'
@description('The description of the OpenAI backend pool')
param openAIBackendPoolDescription string = 'Load balancer for multiple OpenAI endpoints'
@description('Name of the Log Analytics resource')
param logAnalyticsName string = 'workspace'
@description('Location of the Log Analytics resource')
param logAnalyticsLocation string = resourceGroup().location
@description('Name of the Application Insights resource')
param applicationInsightsName string = 'insights'
@description('Location of the Application Insights resource')
param applicationInsightsLocation string = resourceGroup().location
@description('Name of the APIM Logger')
param apimLoggerName string = 'apim-logger'
@description('Description of the APIM Logger')
param apimLoggerDescription string = 'APIM Logger for OpenAI API'
@description('Number of bytes to log for API diagnostics')
param apiDiagnosticsLogBytes int = 8192
@description(' Name for the Workbook')
param workbookName string = 'OpenAIUsageAnalysis'
@description('Location for the Workbook')
param workbookLocation string = resourceGroup().location
@description('Display Name for the Workbook')
param workbookDisplayName string = 'OpenAI Usage Analysis'
param index string = ''
var resourceSuffix = uniqueString(subscription().id, resourceGroup().id, index)
resource cognitiveServices_1 'Microsoft.CognitiveServices/accounts@2021-10-01' = [for config in openAIConfig_1: if(length(openAIConfig_1) > 0) {
name: '${config.name}-${resourceSuffix}'
location: config.location
sku: {
name: openAISku
}
kind: 'OpenAI'
properties: {
apiProperties: {
statisticsEnabled: false
}
customSubDomainName: toLower('${config.name}-${resourceSuffix}')
}
}]
resource cognitiveServices_2 'Microsoft.CognitiveServices/accounts@2021-10-01' = [for config in openAIConfig_2: if(length(openAIConfig_2) > 0) {
name: '${config.name}-${resourceSuffix}'
location: config.location
sku: {
name: openAISku
}
kind: 'OpenAI'
properties: {
apiProperties: {
statisticsEnabled: false
}
customSubDomainName: toLower('${config.name}-${resourceSuffix}')
}
}]
resource cognitiveServices_3 'Microsoft.CognitiveServices/accounts@2021-10-01' = [for config in openAIConfig_3: if(length(openAIConfig_3) > 0) {
name: '${config.name}-${resourceSuffix}'
location: config.location
sku: {
name: openAISku
}
kind: 'OpenAI'
properties: {
apiProperties: {
statisticsEnabled: false
}
customSubDomainName: toLower('${config.name}-${resourceSuffix}')
}
}]
resource deployment_1 'Microsoft.CognitiveServices/accounts/deployments@2023-05-01' = [for (config, i) in openAIConfig_1: if(length(openAIConfig_1) > 0) {
name: openAIDeploymentName_1
parent: cognitiveServices_1[i]
properties: {
model: {
format: 'OpenAI'
name: openAIModelName_1
version: openAIModelVersion_1
}
}
sku: {
name: 'Standard'
capacity: openAIModelCapacity
}
}]
resource deployment_2 'Microsoft.CognitiveServices/accounts/deployments@2023-05-01' = [for (config, i) in openAIConfig_2: if(length(openAIConfig_2) > 0) {
name: openAIDeploymentName_2
parent: cognitiveServices_2[i]
properties: {
model: {
format: 'OpenAI'
name: openAIModelName_2
version: openAIModelVersion_2
}
}
sku: {
name: 'Standard'
capacity: openAIModelCapacity
}
}]
resource deployment_3 'Microsoft.CognitiveServices/accounts/deployments@2023-05-01' = [for (config, i) in openAIConfig_3: if(length(openAIConfig_3) > 0) {
name: openAIDeploymentName_3
parent: cognitiveServices_3[i]
properties: {
model: {
format: 'OpenAI'
name: openAIModelName_3
version: openAIModelVersion_3
}
}
sku: {
name: 'Standard'
capacity: openAIModelCapacity
}
}]
resource apimService 'Microsoft.ApiManagement/service@2023-05-01-preview' = {
name: '${apimResourceName}-${resourceSuffix}'
location: apimResourceLocation
sku: {
name: apimSku
capacity: (apimSku == 'Consumption') ? 0 : ((apimSku == 'Developer') ? 1 : apimSkuCount)
}
properties: {
publisherEmail: apimPublisherEmail
publisherName: apimPublisherName
}
identity: {
type: 'SystemAssigned'
}
}
var roleDefinitionID = resourceId('Microsoft.Authorization/roleDefinitions', '5e0bd9bd-7b93-4f28-af87-19fc36ad61bd')
resource roleAssignment_1 'Microsoft.Authorization/roleAssignments@2022-04-01' = [for (config, i) in openAIConfig_1: if(length(openAIConfig_1) > 0) {
scope: cognitiveServices_1[i]
name: guid(subscription().id, resourceGroup().id, config.name, roleDefinitionID)
properties: {
roleDefinitionId: roleDefinitionID
principalId: apimService.identity.principalId
principalType: 'ServicePrincipal'
}
}]
resource roleAssignment_2 'Microsoft.Authorization/roleAssignments@2022-04-01' = [for (config, i) in openAIConfig_2: if(length(openAIConfig_2) > 0) {
scope: cognitiveServices_2[i]
name: guid(subscription().id, resourceGroup().id, config.name, roleDefinitionID)
properties: {
roleDefinitionId: roleDefinitionID
principalId: apimService.identity.principalId
principalType: 'ServicePrincipal'
}
}]
resource roleAssignment_3 'Microsoft.Authorization/roleAssignments@2022-04-01' = [for (config, i) in openAIConfig_3: if(length(openAIConfig_3) > 0) {
scope: cognitiveServices_3[i]
name: guid(subscription().id, resourceGroup().id, config.name, roleDefinitionID)
properties: {
roleDefinitionId: roleDefinitionID
principalId: apimService.identity.principalId
principalType: 'ServicePrincipal'
}
}]
resource api 'Microsoft.ApiManagement/service/apis@2023-05-01-preview' = {
name: openAIAPIName
parent: apimService
properties: {
apiType: 'http'
description: openAIAPIDescription
displayName: openAIAPIDisplayName
format: 'openapi-link'
path: openAIAPIPath
protocols: [
'https'
]
subscriptionKeyParameterNames: {
header: 'api-key'
query: 'api-key'
}
subscriptionRequired: true
type: 'http'
value: openAIAPISpecURL
}
}
resource apiPolicy 'Microsoft.ApiManagement/service/apis/policies@2021-12-01-preview' = {
name: 'policy'
parent: api
properties: {
format: 'rawxml'
value: loadTextContent('policy.xml')
}
}
// Optimize this
var circuitBreaker = {
rules: [
{
failureCondition: {
count: 3
errorReasons: [
'Server errors'
]
interval: 'PT5M'
statusCodeRanges: [
{
min: 429
max: 429
}, {
min: 500
max: 599
}
]
}
name: 'myBreakerRule'
tripDuration: 'PT1M'
// acceptRetryAfter: true // respects the Retry-After header
}
]
}
resource backendOpenAI_1 'Microsoft.ApiManagement/service/backends@2023-05-01-preview' = [for (config, i) in openAIConfig_1: if(length(openAIConfig_1) > 0) {
name: config.name
parent: apimService
properties: {
description: 'backend description'
url: '${cognitiveServices_1[i].properties.endpoint}/openai'
protocol: 'http'
circuitBreaker: circuitBreaker
}
}]
resource backendOpenAI_2 'Microsoft.ApiManagement/service/backends@2023-05-01-preview' = [for (config, i) in openAIConfig_2: if(length(openAIConfig_2) > 0) {
name: config.name
parent: apimService
properties: {
description: 'backend description'
url: '${cognitiveServices_2[i].properties.endpoint}/openai'
protocol: 'http'
circuitBreaker: circuitBreaker
}
}]
resource backendOpenAI_3 'Microsoft.ApiManagement/service/backends@2023-05-01-preview' = [for (config, i) in openAIConfig_3: if(length(openAIConfig_3) > 0) {
name: config.name
parent: apimService
properties: {
description: 'backend description'
url: '${cognitiveServices_3[i].properties.endpoint}/openai'
protocol: 'http'
circuitBreaker: circuitBreaker
}
}]
resource backendPoolOpenAI_1 'Microsoft.ApiManagement/service/backends@2023-05-01-preview' = if(length(openAIConfig_1) > 1) {
name: openAIBackendPoolName_1
parent: apimService
#disable-next-line BCP035
properties: {
description: openAIBackendPoolDescription
type: 'Pool'
pool: {
services: [for (config, i) in openAIConfig_1: {
id: '/backends/${backendOpenAI_1[i].name}'
}
]
}
}
}
resource backendPoolOpenAI_2 'Microsoft.ApiManagement/service/backends@2023-05-01-preview' = if(length(openAIConfig_2) > 1) {
name: openAIBackendPoolName_2
parent: apimService
#disable-next-line BCP035
properties: {
description: openAIBackendPoolDescription
type: 'Pool'
pool: {
services: [for (config, i) in openAIConfig_2: {
id: '/backends/${backendOpenAI_2[i].name}'
}
]
}
}
}
resource backendPoolOpenAI_3 'Microsoft.ApiManagement/service/backends@2023-05-01-preview' = if(length(openAIConfig_3) > 1) {
name: openAIBackendPoolName_3
parent: apimService
#disable-next-line BCP035
properties: {
description: openAIBackendPoolDescription
type: 'Pool'
pool: {
services: [for (config, i) in openAIConfig_3: {
id: '/backends/${backendOpenAI_3[i].name}'
}
]
}
}
}
resource apimSubscription 'Microsoft.ApiManagement/service/subscriptions@2023-05-01-preview' = {
name: openAISubscriptionName
parent: apimService
properties: {
allowTracing: true
displayName: openAISubscriptionDescription
scope: '/apis/${api.id}'
state: 'active'
}
}
// model-routing: additions BEGIN
resource logAnalytics 'Microsoft.OperationalInsights/workspaces@2021-12-01-preview' = {
name: '${logAnalyticsName}-${resourceSuffix}'
location: logAnalyticsLocation
properties: any({
retentionInDays: 30
features: {
searchVersion: 1
}
sku: {
name: 'PerGB2018'
}
})
}
resource diagnosticSettings_1 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = [for (config, i) in openAIConfig_1: if(length(openAIConfig_1) > 0) {
name: '${cognitiveServices_1[i].name}-diagnostics'
scope: cognitiveServices_1[i]
properties: {
workspaceId: logAnalytics.id
logs: []
metrics: [
{
category: 'AllMetrics'
enabled: true
}
]
}
}]
resource diagnosticSettings_2 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = [for (config, i) in openAIConfig_2: if(length(openAIConfig_2) > 0) {
name: '${cognitiveServices_2[i].name}-diagnostics'
scope: cognitiveServices_2[i]
properties: {
workspaceId: logAnalytics.id
logs: []
metrics: [
{
category: 'AllMetrics'
enabled: true
}
]
}
}]
resource applicationInsights 'Microsoft.Insights/components@2020-02-02' = {
name: '${applicationInsightsName}-${resourceSuffix}'
location: applicationInsightsLocation
kind: 'web'
properties: {
Application_Type: 'web'
WorkspaceResourceId: logAnalytics.id
}
}
resource apimLogger 'Microsoft.ApiManagement/service/loggers@2021-12-01-preview' = {
name: apimLoggerName
parent: apimService
properties: {
credentials: {
instrumentationKey: applicationInsights.properties.InstrumentationKey
}
description: apimLoggerDescription
isBuffered: false
loggerType: 'applicationInsights'
resourceId: applicationInsights.id
}
}
var logSettings = {
headers: [ 'Content-type', 'User-agent', 'x-ms-region', 'x-ratelimit-remaining-tokens' , 'x-ratelimit-remaining-requests' ]
body: { bytes: apiDiagnosticsLogBytes }
}
resource apiDiagnostics 'Microsoft.ApiManagement/service/apis/diagnostics@2022-08-01' = if (!empty(apimLogger.name)) {
name: 'applicationinsights'
parent: api
properties: {
alwaysLog: 'allErrors'
httpCorrelationProtocol: 'W3C'
logClientIp: true
loggerId: apimLogger.id
metrics: true
verbosity: 'verbose'
sampling: {
samplingType: 'fixed'
percentage: 100
}
frontend: {
request: logSettings
response: logSettings
}
backend: {
request: logSettings
response: logSettings
}
}
}
resource workbook 'Microsoft.Insights/workbooks@2022-04-01' = {
name: guid(resourceGroup().id, workbookName)
location: workbookLocation
kind: 'shared'
properties: {
displayName: workbookDisplayName
serializedData: loadTextContent('openai-usage-analysis-workbook.json')
sourceId: applicationInsights.id
category: 'OpenAI'
}
}
output applicationInsightsAppId string = applicationInsights.properties.AppId
output logAnalyticsWorkspaceId string = logAnalytics.properties.customerId
// model-routing: additions END
output apimServiceId string = apimService.id
output apimResourceGatewayURL string = apimService.properties.gatewayUrl
#disable-next-line outputs-should-not-contain-secrets
output apimSubscriptionKey string = apimSubscription.listSecrets().primaryKey