-
Notifications
You must be signed in to change notification settings - Fork 126
/
main.bicep
683 lines (589 loc) · 19.4 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
@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
// vector-searching: additions BEGIN
@description('Embeddings Model Name')
param openAIEmbeddingsDeploymentName string = 'text-embedding-ada-002'
@description('Embeddings Model Name')
param openAIEmbeddingsModelName string = 'text-embedding-ada-002'
@description('Embeddings Model Version')
param openAIEmbeddingsModelVersion string = '2'
@description('AI Search service name')
@minLength(2)
@maxLength(60)
param searchServiceName string = 'search'
@description('AI Search service location')
param searchServiceLocation string = resourceGroup().location
@description('AI Search service SKU')
param searchServiceSku string = 'standard'
@description('Replicas distribute search workloads across the service. You need at least two replicas to support high availability of query workloads (not applicable to the free tier).')
@minValue(1)
@maxValue(12)
param searchServiceReplicaCount int = 1
@description('Partitions allow for scaling of document count as well as faster indexing by sharding your index over multiple search units.')
@allowed([
1
2
3
4
6
12
])
param searchServicePartitionCount int = 1
@description('Search Service API Name')
param searchServiceAPIName string = 'searchservice'
@description('Search Service API Display Name')
param searchServiceAPIDisplayName string = 'AISearchService'
@description('Search Service API Description')
param searchServiceAPIDescription string = 'Azure AI Search Service API'
@description('Search Service API Path')
param searchServiceAPIPath string = 'searchservice'
@description('Search Service Backend Name')
param searchServiceBackendName string = 'search'
@description('Search Service Backend Description')
param searchServiceBackendDescription string = 'Azure AI Search Service Backend'
@description('Search Index API Name')
param searchIndexAPIName string = 'searchindex'
@description('Search Index API Display Name')
param searchIndexAPIDisplayName string = 'AISearchIndex'
@description('Search Index API Description')
param searchIndexAPIDescription string = 'Azure AI Search Index API'
@description('Search Index API Path')
param searchIndexAPIPath string = 'searchindex'
// vector-searching: 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 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') // Cognitive Services OpenAI User
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: 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')
}
}
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'
state: 'active'
}
}
// vector-searching: additions BEGIN
resource embeddingsDeployment 'Microsoft.CognitiveServices/accounts/deployments@2023-05-01' = [for (config, i) in openAIConfig: if(length(openAIConfig) > 0 && !empty(deployment[i].id)) {
name: openAIEmbeddingsDeploymentName
parent: cognitiveServices[i]
properties: {
model: {
format: 'OpenAI'
name: openAIEmbeddingsModelName
version: openAIEmbeddingsModelVersion
}
}
sku: {
name: 'Standard'
capacity: openAIModelCapacity
}
}]
resource searchService 'Microsoft.Search/searchServices@2023-11-01' = {
name: '${searchServiceName}-${resourceSuffix}'
location: searchServiceLocation
sku: {
name: searchServiceSku
}
properties: {
authOptions: {
aadOrApiKey: {
aadAuthFailureMode: 'http401WithBearerChallenge'
}
}
replicaCount: searchServiceReplicaCount
partitionCount: searchServicePartitionCount
}
}
var roleDefinitionIDAISearchService = resourceId('Microsoft.Authorization/roleDefinitions', '7ca78c08-252a-4471-8644-bb5ff32d4ba0') // Search Service Contributor
resource roleAssignmentAISearchService 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
scope: searchService
name: guid(subscription().id, resourceGroup().id, searchService.name, roleDefinitionIDAISearchService)
properties: {
roleDefinitionId: roleDefinitionIDAISearchService
principalId: apimService.identity.principalId
principalType: 'ServicePrincipal'
}
}
var roleDefinitionIDAISearchIndex = resourceId('Microsoft.Authorization/roleDefinitions', '8ebe5a00-799e-43f5-93ac-243d3dce84a7') // Search Index Data Contributor
resource roleAssignmentAISearchIndex 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
scope: searchService
name: guid(subscription().id, resourceGroup().id, searchService.name, roleDefinitionIDAISearchIndex)
properties: {
roleDefinitionId: roleDefinitionIDAISearchIndex
principalId: apimService.identity.principalId
principalType: 'ServicePrincipal'
}
}
resource searchServiceApi 'Microsoft.ApiManagement/service/apis@2023-05-01-preview' = {
name: searchServiceAPIName
parent: apimService
properties: {
apiType: 'http'
description: searchServiceAPIDescription
displayName: searchServiceAPIDisplayName
format: 'openapi+json'
path: searchServiceAPIPath
protocols: [
'https'
]
subscriptionKeyParameterNames: {
header: 'api-key'
query: 'api-key'
}
subscriptionRequired: true
type: 'http'
value: loadJsonContent('searchservice.json')
}
}
resource searchIndexApi 'Microsoft.ApiManagement/service/apis@2023-05-01-preview' = {
name: searchIndexAPIName
parent: apimService
properties: {
apiType: 'http'
description: searchIndexAPIDescription
displayName: searchIndexAPIDisplayName
format: 'openapi+json'
path: searchIndexAPIPath
protocols: [
'https'
]
subscriptionKeyParameterNames: {
header: 'api-key'
query: 'api-key'
}
subscriptionRequired: true
type: 'http'
value: loadJsonContent('searchindex.json')
}
}
resource searchServiceAPIPolicy 'Microsoft.ApiManagement/service/apis/policies@2021-12-01-preview' = {
name: 'policy'
parent: searchServiceApi
properties: {
format: 'rawxml'
value: loadTextContent('searchservice-policy.xml')
}
}
resource searchIndexAPIPolicy 'Microsoft.ApiManagement/service/apis/policies@2021-12-01-preview' = {
name: 'policy'
parent: searchIndexApi
properties: {
format: 'rawxml'
value: loadTextContent('searchindex-policy.xml')
}
}
resource backendSearchService 'Microsoft.ApiManagement/service/backends@2023-05-01-preview' = {
name: searchServiceBackendName
parent: apimService
properties: {
description: searchServiceBackendDescription
url: 'https://${searchServiceName}-${resourceSuffix}.search.windows.net'
protocol: 'http'
}
}
// vector-searching: additions END
// 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 searchServiceApiDiagnostics 'Microsoft.ApiManagement/service/apis/diagnostics@2022-08-01' = if (!empty(apimLogger.name)) {
name: 'applicationinsights'
parent: searchServiceApi
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 searchIndexApiDiagnostics 'Microsoft.ApiManagement/service/apis/diagnostics@2022-08-01' = if (!empty(apimLogger.name)) {
name: 'applicationinsights'
parent: searchIndexApi
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
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