-
Notifications
You must be signed in to change notification settings - Fork 125
/
main.bicep
529 lines (450 loc) · 14.9 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
@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 = 'noreply@microsoft.com'
@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
// semantic-caching: additions BEGIN
@description('Embeddings Deployment Name')
param embeddingsDeploymentName string
@description('Embeddings Model Name')
param embeddingsModelName string
@description('Embeddings Model Version')
param embeddingsModelVersion string = '1'
@description('Embeddings Model Capacity')
param embeddingsModelCapacity int = 20
@description('Location of the Redis Enterprise Cache')
param redisCacheLocation string = resourceGroup().location
@description('Name of the Redis Enterprise Cache')
param redisCacheName string = 'redisCache'
@description('SKU of the Redis Enterprise Cache')
param redisCacheSKU string = 'Enterprise_E5'
@description('Capacity of the Redis Enterprise Cache')
param redisCacheCapacity int = 2
@description('Eviction Policy of the Redis Enterprise Cache')
param redisEvictionPolicy string = 'NoEviction'
@description('Port of the Redis Enterprise Cache')
param redisPort int = 10000
// semantic-caching: 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'
}
}
// semantic-caching: additions BEGIN
resource redisEnterprise 'Microsoft.Cache/redisEnterprise@2022-01-01' = {
name: '${redisCacheName}-${resourceSuffix}'
location: redisCacheLocation
sku: {
name: redisCacheSKU
capacity: redisCacheCapacity
}
}
resource redisCache 'Microsoft.Cache/redisEnterprise/databases@2022-01-01' = {
name: 'default'
parent: redisEnterprise
properties: {
evictionPolicy: redisEvictionPolicy
clusteringPolicy: 'EnterpriseCluster'
modules: [
{
name: 'RediSearch'
}
]
port: redisPort
}
}
resource apimCache 'Microsoft.ApiManagement/service/caches@2021-12-01-preview' = {
name: 'Default'
parent: apimService
properties: {
connectionString: '${redisEnterprise.properties.hostName}:${redisPort},password=${redisCache.listKeys().primaryKey},ssl=True,abortConnect=False'
useFromLocation: 'Default'
description: redisEnterprise.properties.hostName
}
}
resource embeddingsDeployment 'Microsoft.CognitiveServices/accounts/deployments@2023-05-01' = [for (config, i) in openAIConfig: if(length(openAIConfig) > 0) {
name: '${embeddingsDeploymentName}-${redisCache.name}'
parent: cognitiveServices[i]
properties: {
model: {
format: 'OpenAI'
name: embeddingsModelName
version: embeddingsModelVersion
}
}
sku: {
name: 'Standard'
capacity: embeddingsModelCapacity
}
}]
resource backendEmbeddings 'Microsoft.ApiManagement/service/backends@2023-05-01-preview' = {
name: 'embeddings-backend'
parent: apimService
properties: {
description: 'Embeddings Backend'
url: '${cognitiveServices[0].properties.endpoint}/openai/deployments/${embeddingsDeploymentName}-${redisCache.name}/embeddings'
protocol: 'http'
}
}
// semantic-caching: additions END
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: 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/${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
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