-
Notifications
You must be signed in to change notification settings - Fork 126
/
main.bicep
721 lines (628 loc) · 20.5 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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
@description('List of Mock webapp names used to simulate OpenAI behavior.')
param mockWebApps array = []
@description('The name of the OpenAI mock backend pool')
param mockBackendPoolName string = 'openai-backend-pool'
@description('The description of the OpenAI mock backend pool')
param mockBackendPoolDescription string = 'Load balancer for multiple OpenAI Mocking endpoints'
@description('List of OpenAI resources to create. Add pairs of name and location.')
param openAIConfig array = []
@description('Deployment Name')
param openAIDeploymentName string
@description('Azure OpenAI Sku')
@allowed([
'S0'
])
param openAISku string = 'S0'
@description('Model Name')
param openAIModelName string
@description('Model Version')
param openAIModelVersion 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')
param openAIBackendPoolName string = 'openai-backend-pool'
@description('The description of the OpenAI backend pool')
param openAIBackendPoolDescription string = 'Load balancer for multiple OpenAI endpoints'
// buult-in logging: additions BEGIN
@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'
// buult-in logging: additions END
// message storing: additions BEGIN
@description('Event Hub namespace name')
param eventHubNamespaceName string
@description('Event Hub namespace location')
param eventHubLocation string = resourceGroup().location
@description('Event Hub SKU')
param eventHubSKU string = 'Standard'
@description('Event Hub SKU capacity')
param eventHubSKUCapacity int = 1
@description('Event Hub name')
param eventHubName string
@description('APIM logger name for Event Hub')
param eventHubLoggerName string
@description('Streaming Jobs name')
param streamingJobsName string
@description('Streaming Jobs location')
param streamingJobsLocation string = resourceGroup().location
@description('Azure Cosmos DB account name')
param cosmosDBAccountName string
@description('Location for the Azure Cosmos DB account.')
param cosmosDBLocation string = resourceGroup().location
@description('The name for the database')
param cosmosDBDatabaseName string
@description('The name for the container')
param cosmosDBContainerName string
@description('The partition key for the container')
param partitionKeyPath string = '/model'
@description('The throughput policy for the container')
@allowed([
'Manual'
'Autoscale'
])
param throughputPolicy string = 'Autoscale'
@description('Throughput value when using Manual Throughput Policy for the container')
@minValue(400)
@maxValue(1000000)
param manualProvisionedThroughput int = 400
@description('Maximum throughput when using Autoscale Throughput Policy for the container')
@minValue(1000)
@maxValue(1000000)
param autoscaleMaxThroughput int = 1000
@description('Time to Live for data in analytical store. (-1 no expiry)')
@minValue(-1)
@maxValue(2147483647)
param analyticalStoreTTL int = -1
// message storing: additions END
var resourceSuffix = uniqueString(subscription().id, resourceGroup().id)
resource cognitiveServices 'Microsoft.CognitiveServices/accounts@2021-10-01' = [for config in openAIConfig: if(length(openAIConfig) > 0) {
name: '${config.name}-${resourceSuffix}'
location: config.location
sku: {
name: openAISku
}
kind: 'OpenAI'
properties: {
apiProperties: {
statisticsEnabled: false
}
customSubDomainName: toLower('${config.name}-${resourceSuffix}')
}
}]
resource deployment 'Microsoft.CognitiveServices/accounts/deployments@2023-05-01' = [for (config, i) in openAIConfig: if(length(openAIConfig) > 0) {
name: openAIDeploymentName
parent: cognitiveServices[i]
properties: {
model: {
format: 'OpenAI'
name: openAIModelName
version: openAIModelVersion
}
}
sku: {
name: 'Standard'
capacity: openAIModelCapacity
}
}]
resource managedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2018-11-30' = {
name: '${apimResourceName}-${resourceSuffix}'
location: apimResourceLocation
}
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: 'UserAssigned'
userAssignedIdentities: {
'${managedIdentity.id}': {}
}
}
}
var roleDefinitionID = resourceId('Microsoft.Authorization/roleDefinitions', '5e0bd9bd-7b93-4f28-af87-19fc36ad61bd')
resource roleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = [for (config, i) in openAIConfig: if(length(openAIConfig) > 0) {
scope: cognitiveServices[i]
name: guid(subscription().id, resourceGroup().id, config.name, roleDefinitionID)
properties: {
roleDefinitionId: roleDefinitionID
principalId: managedIdentity.properties.principalId
principalType: 'ServicePrincipal'
}
}]
resource managedIdentityNamedValue 'Microsoft.ApiManagement/service/namedValues@2022-08-01' = {
name: 'managed-identity-clientid'
parent: apimService
properties: {
displayName: 'managed-identity-clientid'
secret: true
value: managedIdentity.properties.clientId
}
}
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')
}
}
resource backendOpenAI 'Microsoft.ApiManagement/service/backends@2023-05-01-preview' = [for (config, i) in openAIConfig: if(length(openAIConfig) > 0) {
name: config.name
parent: apimService
properties: {
description: 'backend description'
url: '${cognitiveServices[i].properties.endpoint}/openai'
protocol: 'http'
circuitBreaker: {
rules: [
{
failureCondition: {
count: 3
errorReasons: [
'Server errors'
]
interval: 'PT5M'
statusCodeRanges: [
{
min: 429
max: 429
}
]
}
name: 'openAIBreakerRule'
tripDuration: 'PT1M'
}
]
}
}
}]
resource backendMock 'Microsoft.ApiManagement/service/backends@2023-05-01-preview' = [for (mock, i) in mockWebApps: if(length(openAIConfig) == 0 && length(mockWebApps) > 0) {
name: mock.name
parent: apimService
properties: {
description: 'backend description'
url: '${mock.endpoint}/openai'
protocol: 'http'
circuitBreaker: {
rules: [
{
failureCondition: {
count: 3
errorReasons: [
'Server errors'
]
interval: 'PT5M'
statusCodeRanges: [
{
min: 429
max: 429
}
]
}
name: 'mockBreakerRule'
tripDuration: 'PT1M'
}
]
}
}
}]
resource backendPoolOpenAI 'Microsoft.ApiManagement/service/backends@2023-05-01-preview' = if(length(openAIConfig) > 1) {
name: openAIBackendPoolName
parent: apimService
properties: {
description: openAIBackendPoolDescription
type: 'Pool'
// protocol: 'http' // the protocol is not needed in the Pool type
// url: '${cognitiveServices[0].properties.endpoint}/openai' // the url is not needed in the Pool type
pool: {
services: [for (config, i) in openAIConfig: {
id: '/backends/${backendOpenAI[i].name}'
}
]
}
}
}
resource backendPoolMock 'Microsoft.ApiManagement/service/backends@2023-05-01-preview' = if(length(openAIConfig) == 0 && length(mockWebApps) > 1) {
name: mockBackendPoolName
parent: apimService
properties: {
description: mockBackendPoolDescription
type: 'Pool'
// protocol: 'http' // the protocol is not needed in the Pool type
// url: '${mockWebApps[0].endpoint}/openai' // the url is not needed in the Pool type
pool: {
services: [for (webApp, i) in mockWebApps: {
id: '/backends/${backendMock[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'
}
}
// buult-in logging: 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 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = [for (config, i) in openAIConfig: if(length(openAIConfig) > 0) {
name: '${cognitiveServices[i].name}-diagnostics'
scope: cognitiveServices[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
// buult-in logging: additions END
// message storing: additions BEGIN
resource eventHubNamespaceResource 'Microsoft.EventHub/namespaces@2021-01-01-preview' = {
name: '${eventHubNamespaceName}-${resourceSuffix}'
location: eventHubLocation
sku: {
name: eventHubSKU
tier: eventHubSKU
capacity: eventHubSKUCapacity
}
properties: {
isAutoInflateEnabled: false
maximumThroughputUnits: 0
}
}
resource eventHubResource 'Microsoft.EventHub/namespaces/eventhubs@2021-01-01-preview' = {
name: eventHubName
parent: eventHubNamespaceResource
properties: {
messageRetentionInDays: 7
partitionCount: 2
status: 'Active'
}
}
resource eventHubLogger 'Microsoft.ApiManagement/service/loggers@2022-08-01' = {
name: eventHubLoggerName
parent: apimService
properties: {
loggerType: 'azureEventHub'
description: 'Log messages to Event Hub'
credentials: {
name: eventHubResource.name
endpointAddress: replace(eventHubNamespaceResource.properties.serviceBusEndpoint, 'https://', '')
identityClientId: managedIdentity.properties.clientId
}
}
}
var eventHubDataSenderRoleDefinitionID = resourceId('Microsoft.Authorization/roleDefinitions', '2b629674-e913-4c01-ae53-ef4638d8f975')
resource eventHubsDataSenderRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
scope: eventHubNamespaceResource
name: guid(subscription().id, resourceGroup().id, eventHubNamespaceResource.name, eventHubDataSenderRoleDefinitionID)
properties: {
roleDefinitionId: eventHubDataSenderRoleDefinitionID
principalId: managedIdentity.properties.principalId
principalType: 'ServicePrincipal'
}
}
var eventHubDataOwnerRoleDefinitionID = resourceId('Microsoft.Authorization/roleDefinitions', 'f526a384-b230-433a-b45c-95f59c4a2dec')
resource eventHubsDataOwnerRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
scope: resourceGroup()
name: guid(streamingJobManagedIdentity.id, eventHubDataOwnerRoleDefinitionID)
properties: {
roleDefinitionId: eventHubDataOwnerRoleDefinitionID
principalId: streamingJobManagedIdentity.properties.principalId
principalType: 'ServicePrincipal'
}
}
resource streamingJobManagedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2018-11-30' = {
name: '${streamingJobsName}-${resourceSuffix}'
location: streamingJobsLocation
}
resource streamingJobsResource 'Microsoft.StreamAnalytics/streamingjobs@2021-10-01-preview' = {
name: '${streamingJobsName}-${resourceSuffix}'
location: streamingJobsLocation
identity: {
type: 'UserAssigned'
userAssignedIdentities: {
'${streamingJobManagedIdentity.id}': {}
}
}
properties: {
sku: {
name: 'StandardV2'
}
outputStartMode: 'JobStartTime'
eventsOutOfOrderPolicy: 'Adjust'
outputErrorPolicy: 'Stop'
eventsOutOfOrderMaxDelayInSeconds: 5
compatibilityLevel: '1.2'
inputs: [
{
name: 'eventhub'
properties: {
type: 'Stream'
serialization: {
type: 'Json'
properties: {
encoding: 'UTF8'
}
}
datasource: {
type: 'Microsoft.EventHub/EventHub'
properties: {
authenticationMode: 'Msi'
eventHubName: eventHubResource.name
serviceBusNamespace: eventHubNamespaceResource.name
}
}
}
}
]
outputs: [
{
name: 'cosmosdb'
properties: {
datasource: {
type: 'Microsoft.Storage/DocumentDB'
properties: {
accountId: cosmosDBAccountResource.name
database: cosmosDBDatabaseResource.name
collectionNamePattern: cosmosDBContainer.name
authenticationMode: 'Msi'
documentId: 'id'
partitionKey: 'model'
}
}
}
}
]
transformation: {
name: 'transformation'
properties: {
query: 'SELECT * INTO [cosmosdb] FROM [eventhub]'
streamingUnits: 3
}
}
}
}
var cosmosDBLocations = [
{
locationName: cosmosDBLocation
failoverPriority: 0
isZoneRedundant: false
}
]
var throughput_Policy = {
Manual: {
throughput: manualProvisionedThroughput
}
Autoscale: {
autoscaleSettings: {
maxThroughput: autoscaleMaxThroughput
}
}
}
resource cosmosDBAccountResource 'Microsoft.DocumentDB/databaseAccounts@2022-05-15' = {
name: '${cosmosDBAccountName}-${resourceSuffix}'
location: cosmosDBLocation
properties: {
consistencyPolicy: {
defaultConsistencyLevel: 'Session'
}
databaseAccountOfferType: 'Standard'
locations: cosmosDBLocations
enableAnalyticalStorage: true
}
}
var cosmosDBRoleDefinitionID = '00000000-0000-0000-0000-000000000002'
resource cosmosDBRoleAssignment 'Microsoft.DocumentDB/databaseAccounts/sqlRoleAssignments@2023-04-15' = {
parent: cosmosDBAccountResource
name: guid(subscription().id, resourceGroup().id, cosmosDBAccountResource.name, cosmosDBRoleDefinitionID)
properties:{
principalId: streamingJobManagedIdentity.properties.principalId
roleDefinitionId: '${cosmosDBAccountResource.id}/sqlRoleDefinitions/${cosmosDBRoleDefinitionID}'
scope: cosmosDBAccountResource.id
}
}
resource cosmosDBDatabaseResource 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2022-05-15' = {
parent: cosmosDBAccountResource
name: cosmosDBDatabaseName
properties: {
resource: {
id: cosmosDBDatabaseName
}
}
}
resource cosmosDBContainer 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2022-05-15' = {
parent: cosmosDBDatabaseResource
name: cosmosDBContainerName
properties: {
resource: {
id: cosmosDBContainerName
partitionKey: {
paths: [
partitionKeyPath
]
kind: 'Hash'
}
analyticalStorageTtl: analyticalStoreTTL
}
options: throughput_Policy[throughputPolicy]
}
}
// message storing: 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
#disable-next-line outputs-should-not-contain-secrets
output cosmosDBConnectionString string = cosmosDBAccountResource.listConnectionStrings().connectionStrings[0].connectionString