-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathmain.bicep
156 lines (139 loc) · 4.91 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
// ------------------
// 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 openAIModelSKU string
param openAIDeploymentName string
param openAIModelCapacity int
param openAIAPIVersion string
param embeddingsDeploymentName string = 'text-embedding-ada-002'
param embeddingsModelName string = 'text-embedding-ada-002'
param embeddingsModelVersion string = '2'
param redisCacheName string = 'rediscache'
param redisCacheSKU string = 'Balanced_B0'
param redisCachePort int = 10000
// ------------------
// VARIABLES
// ------------------
var resourceSuffix = uniqueString(subscription().id, resourceGroup().id)
var apiManagementName = 'apim-${resourceSuffix}'
// Account for all placeholders in the polixy.xml file.
var policyXml = loadTextContent('policy.xml')
var updatedPolicyXml = replace(policyXml, '{backend-id}', (length(openAIConfig) > 1) ? 'openai-backend-pool' : openAIConfig[0].name)
// ------------------
// RESOURCES
// ------------------
// 1. Redis Cache
// 2/4/25: 2024-10-01 is not yet available in all regions. 2024-09-01-preview is more widely available.
// https://learn.microsoft.com/azure/templates/microsoft.cache/redisenterprise
resource redisEnterprise 'Microsoft.Cache/redisEnterprise@2024-09-01-preview' = {
name: '${redisCacheName}-${resourceSuffix}'
location: resourceGroup().location
sku: {
name: redisCacheSKU
}
}
// https://learn.microsoft.com/azure/templates/microsoft.cache/redisenterprise/databases
resource redisCache 'Microsoft.Cache/redisEnterprise/databases@2024-09-01-preview' = {
name: 'default'
parent: redisEnterprise
properties: {
evictionPolicy: 'NoEviction'
clusteringPolicy: 'EnterpriseCluster'
modules: [
{
name: 'RediSearch'
}
]
port: redisCachePort
}
}
// 2. API Management
module apimModule '../../modules/apim/v1/apim.bicep' = {
name: 'apimModule'
params: {
apimSku: apimSku
}
}
resource apimService 'Microsoft.ApiManagement/service@2024-06-01-preview' existing = if (length(apimModule.outputs.id) > 0) {
name: apiManagementName
}
// https://learn.microsoft.com/azure/templates/microsoft.apimanagement/service/caches
resource apimCache 'Microsoft.ApiManagement/service/caches@2024-06-01-preview' = {
name: 'Default'
parent: apimService
properties: {
connectionString: '${redisEnterprise.properties.hostName}:${redisCachePort},password=${redisCache.listKeys().primaryKey},ssl=True,abortConnect=False'
useFromLocation: 'Default'
description: redisEnterprise.properties.hostName
}
}
// 2. Cognitive Services
module openAIModule '../../modules/cognitive-services/v1/openai.bicep' = {
name: 'openAIModule'
params: {
openAIConfig: openAIConfig
openAIDeploymentName: openAIDeploymentName
openAIModelName: openAIModelName
openAIModelVersion: openAIModelVersion
openAIModelSKU: openAIModelSKU
openAIModelCapacity: openAIModelCapacity
apimPrincipalId: apimModule.outputs.principalId
}
}
resource cognitiveService 'Microsoft.CognitiveServices/accounts@2024-10-01' existing = {
name: '${openAIConfig[0].name}-${resourceSuffix}'
}
resource embeddingsDeployment 'Microsoft.CognitiveServices/accounts/deployments@2023-05-01' = {
name: embeddingsDeploymentName
parent: cognitiveService
properties: {
model: {
format: (length(openAIModule.outputs.extendedOpenAIConfig) > 0) ? 'OpenAI': ''
name: embeddingsModelName
version: embeddingsModelVersion
}
}
sku: {
name: 'Standard'
capacity: 20
}
dependsOn: [
cognitiveService
]
}
// 3. APIM OpenAI API
resource backendEmbeddings 'Microsoft.ApiManagement/service/backends@2024-06-01-preview' = {
name: 'embeddings-backend' // this name is hard coded in the policy.xml file
parent: apimService
properties: {
description: 'Embeddings Backend'
url: '${openAIModule.outputs.extendedOpenAIConfig[0].endpoint}openai/deployments/${embeddingsDeploymentName}/embeddings'
protocol: 'http'
}
}
module openAIAPIModule '../../modules/apim/v1/openai-api.bicep' = {
name: 'openAIAPIModule'
params: {
policyXml: updatedPolicyXml
openAIConfig: openAIModule.outputs.extendedOpenAIConfig
openAIAPIVersion: openAIAPIVersion
}
dependsOn: [
backendEmbeddings
]
}
// ------------------
// MARK: OUTPUTS
// ------------------
output apimServiceId string = apimModule.outputs.id
output apimResourceGatewayURL string = apimModule.outputs.gatewayUrl
output apimSubscriptionKey string = openAIAPIModule.outputs.subscriptionPrimaryKey
output redisCacheHost string = redisEnterprise.properties.hostName
#disable-next-line outputs-should-not-contain-secrets
output redisCacheKey string = redisCache.listKeys().primaryKey
output redisCachePort int = redisCachePort