forked from Azure-Samples/azure-search-openai-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.bicep
377 lines (334 loc) · 11.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
targetScope = 'subscription'
@minLength(1)
@maxLength(64)
@description('Name of the the environment which is used to generate a short unique hash used in all resources.')
param environmentName string
@minLength(1)
@description('Primary location for all resources')
param location string
param appServicePlanName string = ''
param backendServiceName string = ''
param resourceGroupName string = ''
param applicationInsightsName string = ''
param searchServiceName string = ''
param searchServiceResourceGroupName string = ''
param searchServiceResourceGroupLocation string = location
param searchServiceSkuName string = 'standard'
param searchIndexName string // Set in main.parameters.json
param storageAccountName string = ''
param storageResourceGroupName string = ''
param storageResourceGroupLocation string = location
param storageContainerName string = 'content'
param openAiServiceName string = ''
param openAiResourceGroupName string = ''
@description('Location for the OpenAI resource group')
@allowed(['canadaeast', 'eastus', 'francecentral', 'japaneast', 'northcentralus'])
@metadata({
azd: {
type: 'location'
}
})
param openAiResourceGroupLocation string
param openAiSkuName string = 'S0'
param formRecognizerServiceName string = ''
param formRecognizerResourceGroupName string = ''
param formRecognizerResourceGroupLocation string = location
param formRecognizerSkuName string = 'S0'
param gptDeploymentName string // Set in main.parameters.json
param gptDeploymentCapacity int = 30
param gptModelName string = 'gpt-35-turbo'
param gptModelVersion string = '0613'
param chatGptDeploymentName string // Set in main.parameters.json
param chatGptDeploymentCapacity int = 30
param chatGptModelName string = 'gpt-35-turbo'
param chatGptModelVersion string = '0613'
param embeddingDeploymentName string = 'embedding'
param embeddingDeploymentCapacity int = 30
param embeddingModelName string = 'text-embedding-ada-002'
@description('Id of the user or app to assign application roles')
param principalId string = ''
@description('Use Application Insights for monitoring and performance tracing')
param useApplicationInsights bool = false
var abbrs = loadJsonContent('abbreviations.json')
var resourceToken = toLower(uniqueString(subscription().id, environmentName, location))
var tags = { 'azd-env-name': environmentName }
// Organize resources in a resource group
resource resourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' = {
name: !empty(resourceGroupName) ? resourceGroupName : '${abbrs.resourcesResourceGroups}${environmentName}'
location: location
tags: tags
}
resource openAiResourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' existing = if (!empty(openAiResourceGroupName)) {
name: !empty(openAiResourceGroupName) ? openAiResourceGroupName : resourceGroup.name
}
resource formRecognizerResourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' existing = if (!empty(formRecognizerResourceGroupName)) {
name: !empty(formRecognizerResourceGroupName) ? formRecognizerResourceGroupName : resourceGroup.name
}
resource searchServiceResourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' existing = if (!empty(searchServiceResourceGroupName)) {
name: !empty(searchServiceResourceGroupName) ? searchServiceResourceGroupName : resourceGroup.name
}
resource storageResourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' existing = if (!empty(storageResourceGroupName)) {
name: !empty(storageResourceGroupName) ? storageResourceGroupName : resourceGroup.name
}
// Monitor application with Azure Monitor
module monitoring './core/monitor/monitoring.bicep' = if (useApplicationInsights) {
name: 'monitoring'
scope: resourceGroup
params: {
location: location
tags: tags
applicationInsightsName: !empty(applicationInsightsName) ? applicationInsightsName : '${abbrs.insightsComponents}${resourceToken}'
}
}
// Create an App Service Plan to group applications under the same payment plan and SKU
module appServicePlan 'core/host/appserviceplan.bicep' = {
name: 'appserviceplan'
scope: resourceGroup
params: {
name: !empty(appServicePlanName) ? appServicePlanName : '${abbrs.webServerFarms}${resourceToken}'
location: location
tags: tags
sku: {
name: 'B1'
capacity: 1
}
kind: 'linux'
}
}
// The application frontend
module backend 'core/host/appservice.bicep' = {
name: 'web'
scope: resourceGroup
params: {
name: !empty(backendServiceName) ? backendServiceName : '${abbrs.webSitesAppService}backend-${resourceToken}'
location: location
tags: union(tags, { 'azd-service-name': 'backend' })
appServicePlanId: appServicePlan.outputs.id
runtimeName: 'python'
runtimeVersion: '3.10'
appCommandLine: 'python3 -m gunicorn main:app'
scmDoBuildDuringDeployment: true
managedIdentity: true
appSettings: {
AZURE_STORAGE_ACCOUNT: storage.outputs.name
AZURE_STORAGE_CONTAINER: storageContainerName
AZURE_OPENAI_SERVICE: openAi.outputs.name
AZURE_SEARCH_INDEX: searchIndexName
AZURE_SEARCH_SERVICE: searchService.outputs.name
AZURE_OPENAI_GPT_DEPLOYMENT: gptDeploymentName
AZURE_OPENAI_CHATGPT_DEPLOYMENT: chatGptDeploymentName
AZURE_OPENAI_EMB_DEPLOYMENT: embeddingDeploymentName
APPLICATIONINSIGHTS_CONNECTION_STRING: useApplicationInsights ? monitoring.outputs.applicationInsightsConnectionString : ''
}
}
}
module openAi 'core/ai/cognitiveservices.bicep' = {
name: 'openai'
scope: openAiResourceGroup
params: {
name: !empty(openAiServiceName) ? openAiServiceName : '${abbrs.cognitiveServicesAccounts}${resourceToken}'
location: openAiResourceGroupLocation
tags: tags
sku: {
name: openAiSkuName
}
deployments: [
{
name: gptDeploymentName
model: {
format: 'OpenAI'
name: gptModelName
version: gptModelVersion
}
sku: {
name: 'Standard'
capacity: gptDeploymentCapacity
}
}
{
name: chatGptDeploymentName
model: {
format: 'OpenAI'
name: chatGptModelName
version: gptModelVersion
}
sku: {
name: 'Standard'
capacity: chatGptDeploymentCapacity
}
}
{
name: embeddingDeploymentName
model: {
format: 'OpenAI'
name: embeddingModelName
version: '2'
}
capacity: embeddingDeploymentCapacity
}
]
}
}
module formRecognizer 'core/ai/cognitiveservices.bicep' = {
name: 'formrecognizer'
scope: formRecognizerResourceGroup
params: {
name: !empty(formRecognizerServiceName) ? formRecognizerServiceName : '${abbrs.cognitiveServicesFormRecognizer}${resourceToken}'
kind: 'FormRecognizer'
location: formRecognizerResourceGroupLocation
tags: tags
sku: {
name: formRecognizerSkuName
}
}
}
module searchService 'core/search/search-services.bicep' = {
name: 'search-service'
scope: searchServiceResourceGroup
params: {
name: !empty(searchServiceName) ? searchServiceName : 'gptkb-${resourceToken}'
location: searchServiceResourceGroupLocation
tags: tags
authOptions: {
aadOrApiKey: {
aadAuthFailureMode: 'http401WithBearerChallenge'
}
}
sku: {
name: searchServiceSkuName
}
semanticSearch: 'free'
}
}
module storage 'core/storage/storage-account.bicep' = {
name: 'storage'
scope: storageResourceGroup
params: {
name: !empty(storageAccountName) ? storageAccountName : '${abbrs.storageStorageAccounts}${resourceToken}'
location: storageResourceGroupLocation
tags: tags
publicNetworkAccess: 'Enabled'
sku: {
name: 'Standard_ZRS'
}
deleteRetentionPolicy: {
enabled: true
days: 2
}
containers: [
{
name: storageContainerName
publicAccess: 'None'
}
]
}
}
// USER ROLES
module openAiRoleUser 'core/security/role.bicep' = {
scope: openAiResourceGroup
name: 'openai-role-user'
params: {
principalId: principalId
roleDefinitionId: '5e0bd9bd-7b93-4f28-af87-19fc36ad61bd'
principalType: 'User'
}
}
module formRecognizerRoleUser 'core/security/role.bicep' = {
scope: formRecognizerResourceGroup
name: 'formrecognizer-role-user'
params: {
principalId: principalId
roleDefinitionId: 'a97b65f3-24c7-4388-baec-2e87135dc908'
principalType: 'User'
}
}
module storageRoleUser 'core/security/role.bicep' = {
scope: storageResourceGroup
name: 'storage-role-user'
params: {
principalId: principalId
roleDefinitionId: '2a2b9908-6ea1-4ae2-8e65-a410df84e7d1'
principalType: 'User'
}
}
module storageContribRoleUser 'core/security/role.bicep' = {
scope: storageResourceGroup
name: 'storage-contribrole-user'
params: {
principalId: principalId
roleDefinitionId: 'ba92f5b4-2d11-453d-a403-e96b0029c9fe'
principalType: 'User'
}
}
module searchRoleUser 'core/security/role.bicep' = {
scope: searchServiceResourceGroup
name: 'search-role-user'
params: {
principalId: principalId
roleDefinitionId: '1407120a-92aa-4202-b7e9-c0e197c71c8f'
principalType: 'User'
}
}
module searchContribRoleUser 'core/security/role.bicep' = {
scope: searchServiceResourceGroup
name: 'search-contrib-role-user'
params: {
principalId: principalId
roleDefinitionId: '8ebe5a00-799e-43f5-93ac-243d3dce84a7'
principalType: 'User'
}
}
module searchSvcContribRoleUser 'core/security/role.bicep' = {
scope: searchServiceResourceGroup
name: 'search-svccontrib-role-user'
params: {
principalId: principalId
roleDefinitionId: '7ca78c08-252a-4471-8644-bb5ff32d4ba0'
principalType: 'User'
}
}
// SYSTEM IDENTITIES
module openAiRoleBackend 'core/security/role.bicep' = {
scope: openAiResourceGroup
name: 'openai-role-backend'
params: {
principalId: backend.outputs.identityPrincipalId
roleDefinitionId: '5e0bd9bd-7b93-4f28-af87-19fc36ad61bd'
principalType: 'ServicePrincipal'
}
}
module storageRoleBackend 'core/security/role.bicep' = {
scope: storageResourceGroup
name: 'storage-role-backend'
params: {
principalId: backend.outputs.identityPrincipalId
roleDefinitionId: '2a2b9908-6ea1-4ae2-8e65-a410df84e7d1'
principalType: 'ServicePrincipal'
}
}
module searchRoleBackend 'core/security/role.bicep' = {
scope: searchServiceResourceGroup
name: 'search-role-backend'
params: {
principalId: backend.outputs.identityPrincipalId
roleDefinitionId: '1407120a-92aa-4202-b7e9-c0e197c71c8f'
principalType: 'ServicePrincipal'
}
}
output AZURE_LOCATION string = location
output AZURE_TENANT_ID string = tenant().tenantId
output AZURE_RESOURCE_GROUP string = resourceGroup.name
output AZURE_OPENAI_SERVICE string = openAi.outputs.name
output AZURE_OPENAI_RESOURCE_GROUP string = openAiResourceGroup.name
output AZURE_OPENAI_GPT_DEPLOYMENT string = gptDeploymentName
output AZURE_OPENAI_CHATGPT_DEPLOYMENT string = chatGptDeploymentName
output AZURE_OPENAI_EMB_DEPLOYMENT string = embeddingDeploymentName
output AZURE_FORMRECOGNIZER_SERVICE string = formRecognizer.outputs.name
output AZURE_FORMRECOGNIZER_RESOURCE_GROUP string = formRecognizerResourceGroup.name
output AZURE_SEARCH_INDEX string = searchIndexName
output AZURE_SEARCH_SERVICE string = searchService.outputs.name
output AZURE_SEARCH_SERVICE_RESOURCE_GROUP string = searchServiceResourceGroup.name
output AZURE_STORAGE_ACCOUNT string = storage.outputs.name
output AZURE_STORAGE_CONTAINER string = storageContainerName
output AZURE_STORAGE_RESOURCE_GROUP string = storageResourceGroup.name
output BACKEND_URI string = backend.outputs.uri