-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathmain.bicep
135 lines (116 loc) · 4.09 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
// ------------------
// PARAMETERS
// ------------------
// Typically, parameters would be decorated with appropriate metadata and attributes, but as they are very repetetive in these labs we omit them for brevity.
param apimSku string
param openAIConfig array = []
param openAIModelName string
param openAIModelVersion string
param openAIDeploymentName string
param openAIModelSKU string
param openAIAPIVersion string = '2024-02-01'
param policyXml string
// ------------------
// VARIABLES
// ------------------
var resourceSuffix = uniqueString(subscription().id, resourceGroup().id)
var apiManagementName = 'apim-${resourceSuffix}'
var openAISubscriptionName = 'openai-subscription'
var openAISubscriptionDescription = 'OpenAI Subscription'
var openAIAPIName = 'openai'
// ------------------
// RESOURCES
// ------------------
// 1. Log Analytics Workspace
module lawModule '../../modules/operational-insights/v1/workspaces.bicep' = {
name: 'lawModule'
}
var lawId = lawModule.outputs.id
// 2. Application Insights
module appInsightsModule '../../modules/monitor/v1/appinsights.bicep' = {
name: 'appInsightsModule'
params: {
workbookJson: loadTextContent('openai-usage-analysis-workbook.json')
lawId: lawId
customMetricsOptedInType: 'WithDimensions'
}
}
var appInsightsId = appInsightsModule.outputs.id
var appInsightsInstrumentationKey = appInsightsModule.outputs.instrumentationKey
// 3. API Management
module apimModule '../../modules/apim/v1/apim.bicep' = {
name: 'apimModule'
params: {
apimSku: apimSku
appInsightsInstrumentationKey: appInsightsInstrumentationKey
appInsightsId: appInsightsId
}
}
// 4. Cognitive Services
module openAIModule '../../modules/cognitive-services/v2/openai.bicep' = {
name: 'openAIModule'
params: {
openAIConfig: openAIConfig
openAIDeploymentName: openAIDeploymentName
openAIModelName: openAIModelName
openAIModelVersion: openAIModelVersion
openAIModelSKU: openAIModelSKU
apimPrincipalId: apimModule.outputs.principalId
lawId: lawId
}
}
// 5. APIM OpenAI API
module openAIAPIModule '../../modules/apim/v1/openai-api.bicep' = {
name: 'openAIAPIModule'
params: {
policyXml: policyXml
openAIConfig: openAIModule.outputs.extendedOpenAIConfig
openAIAPIVersion: openAIAPIVersion
appInsightsInstrumentationKey: appInsightsInstrumentationKey
appInsightsId: appInsightsId
}
}
// 6. Create New APIM Subscriptions
// We presume the APIM resource has been created as part of this bicep flow.
resource apim 'Microsoft.ApiManagement/service@2024-06-01-preview' existing = {
name: apiManagementName
dependsOn: [
apimModule
]
}
resource api 'Microsoft.ApiManagement/service/apis@2024-06-01-preview' existing = {
parent: apim
name: openAIAPIName
dependsOn: [
openAIAPIModule
]
}
// Ignore the subscription that gets created in the APIM module and create three new ones for this lab.
resource apimSubscriptions 'Microsoft.ApiManagement/service/subscriptions@2024-06-01-preview' = [for i in range(1, 3): {
name: '${openAISubscriptionName}${i}'
parent: apim
properties: {
allowTracing: true
displayName: '${openAISubscriptionDescription} ${i}'
scope: '/apis/${api.id}'
state: 'active'
}
dependsOn: [
api
]
}]
// ------------------
// MARK: OUTPUTS
// ------------------
output applicationInsightsAppId string = appInsightsModule.outputs.appId
output applicationInsightsName string = appInsightsModule.outputs.applicationInsightsName
output logAnalyticsWorkspaceId string = lawModule.outputs.customerId
output apimServiceId string = apimModule.outputs.id
output apimServiceName string = apimModule.outputs.name
output apimResourceGatewayURL string = apimModule.outputs.gatewayUrl
#disable-next-line outputs-should-not-contain-secrets
output apimSubscription1Key string = apimSubscriptions[0].listSecrets().primaryKey
#disable-next-line outputs-should-not-contain-secrets
output apimSubscription2Key string = apimSubscriptions[1].listSecrets().primaryKey
#disable-next-line outputs-should-not-contain-secrets
output apimSubscription3Key string = apimSubscriptions[2].listSecrets().primaryKey