@@ -37,6 +37,21 @@ param openAIResourceGroupName string = ''
3737@description ('Whether to deploy Azure OpenAI resources' )
3838param deployAzureOpenAI bool = true
3939
40+ @allowed ([
41+ 'azure'
42+ 'openaicom'
43+ ])
44+ param openAIChatHost string = 'azure'
45+
46+ @allowed ([
47+ 'azure'
48+ 'openaicom'
49+ ])
50+ param openAIEmbedHost string = 'azure'
51+
52+ @secure ()
53+ param openAIComKey string = ''
54+
4055@description ('Name of the GPT model to deploy' )
4156param chatModelName string = ''
4257@description ('Name of the model deployment' )
@@ -50,18 +65,16 @@ param chatDeploymentVersion string = ''
5065param azureOpenAIAPIVersion string = '2024-03-01-preview'
5166@secure ()
5267param azureOpenAIKey string = ''
68+
5369@description ('Azure OpenAI endpoint to use, if not using the one deployed here.' )
5470param azureOpenAIEndpoint string = ''
5571
56- @description ('Whether to use Azure OpenAI (either deployed here or elsewhere) or OpenAI.com' )
57- var useAzureOpenAI = deployAzureOpenAI || !empty (azureOpenAIEndpoint )
58-
5972@description ('Capacity of the GPT deployment' )
6073// You can increase this, but capacity is limited per model/region, so you will get errors if you go over
6174// https://learn.microsoft.com/en-us/azure/ai-services/openai/quotas-limits
6275param chatDeploymentCapacity int = 0
6376var chatConfig = {
64- modelName : !empty (chatModelName ) ? chatModelName : (useAzureOpenAI ? 'gpt-35-turbo' : 'gpt-3.5-turbo' )
77+ modelName : !empty (chatModelName ) ? chatModelName : (openAIChatHost == 'azure' ? 'gpt-35-turbo' : 'gpt-3.5-turbo' )
6578 deploymentName : !empty (chatDeploymentName ) ? chatDeploymentName : 'gpt-35-turbo'
6679 deploymentVersion : !empty (chatDeploymentVersion ) ? chatDeploymentVersion : '0125'
6780 deploymentCapacity : chatDeploymentCapacity != 0 ? chatDeploymentCapacity : 30
@@ -152,7 +165,37 @@ module containerApps 'core/host/container-apps.bicep' = {
152165// Web frontend
153166var webAppName = replace ('${take (prefix , 19 )}-ca' , '--' , '-' )
154167var webAppIdentityName = '${prefix }-id-web'
155- var webAppEnv = [
168+
169+ var azureOpenAIKeySecret = !empty (azureOpenAIKey )
170+ ? {
171+ 'azure-openai-key' : azureOpenAIKey
172+ }
173+ : {}
174+ var openAIComKeySecret = !empty (openAIComKey )
175+ ? {
176+ 'openaicom-key' : openAIComKey
177+ }
178+ : {}
179+ var secrets = union (azureOpenAIKeySecret , openAIComKeySecret )
180+
181+ var azureOpenAIKeyEnv = !empty (azureOpenAIKey )
182+ ? [
183+ {
184+ name : 'AZURE_OPENAI_KEY'
185+ secretRef : 'azure-openai-key'
186+ }
187+ ]
188+ : []
189+ var openAIComKeyEnv = !empty (openAIComKey )
190+ ? [
191+ {
192+ name : 'OPENAICOM_KEY'
193+ secretRef : 'openaicom-key'
194+ }
195+ ]
196+ : []
197+
198+ var webAppEnv = union (azureOpenAIKeyEnv , openAIComKeyEnv , [
156199 {
157200 name : 'POSTGRES_HOST'
158201 value : postgresServer .outputs .POSTGRES_DOMAIN_NAME
@@ -179,63 +222,53 @@ var webAppEnv = [
179222 }
180223 {
181224 name : 'OPENAI_CHAT_HOST'
182- value : useAzureOpenAI ? 'azure' : 'openaicom'
225+ value : openAIChatHost
183226 }
184227 {
185228 name : 'AZURE_OPENAI_CHAT_DEPLOYMENT'
186- value : useAzureOpenAI ? chatConfig .deploymentName : ''
229+ value : openAIChatHost == 'azure' ? chatConfig .deploymentName : ''
187230 }
188231 {
189232 name : 'AZURE_OPENAI_CHAT_MODEL'
190- value : useAzureOpenAI ? chatConfig .modelName : ''
233+ value : openAIChatHost == 'azure' ? chatConfig .modelName : ''
191234 }
192235 {
193236 name : 'OPENAICOM_CHAT_MODEL'
194- value : useAzureOpenAI ? '' : 'gpt-3.5-turbo'
237+ value : openAIChatHost == 'openaicom' ? 'gpt-3.5-turbo' : ' '
195238 }
196239 {
197240 name : 'OPENAI_EMBED_HOST'
198- value : useAzureOpenAI ? 'azure' : 'openaicom'
241+ value : openAIEmbedHost
199242 }
200243 {
201244 name : 'OPENAICOM_EMBED_MODEL_DIMENSIONS'
202- value : useAzureOpenAI ? '' : '1536 '
245+ value : openAIEmbedHost == 'openaicom' ? '1536 ' : ''
203246 }
204247 {
205248 name : 'OPENAICOM_EMBED_MODEL'
206- value : useAzureOpenAI ? '' : 'text-embedding-ada-002'
249+ value : openAIEmbedHost == 'openaicom' ? 'text-embedding-ada-002' : ' '
207250 }
208251 {
209252 name : 'AZURE_OPENAI_EMBED_MODEL'
210- value : useAzureOpenAI ? embedConfig .modelName : ''
253+ value : openAIEmbedHost == 'azure' ? embedConfig .modelName : ''
211254 }
212255 {
213256 name : 'AZURE_OPENAI_EMBED_DEPLOYMENT'
214- value : useAzureOpenAI ? embedConfig .deploymentName : ''
257+ value : openAIEmbedHost == 'azure' ? embedConfig .deploymentName : ''
215258 }
216259 {
217260 name : 'AZURE_OPENAI_EMBED_MODEL_DIMENSIONS'
218- value : useAzureOpenAI ? string (embedConfig .dimensions ) : ''
261+ value : openAIEmbedHost == 'azure' ? string (embedConfig .dimensions ) : ''
219262 }
220263 {
221264 name : 'AZURE_OPENAI_ENDPOINT'
222- value : useAzureOpenAI ? (deployAzureOpenAI ? openAI .outputs .endpoint : azureOpenAIEndpoint ) : ''
265+ value : ! empty ( azureOpenAIEndpoint ) ? azureOpenAIEndpoint : (deployAzureOpenAI ? openAI .outputs .endpoint : '' )
223266 }
224267 {
225268 name : 'AZURE_OPENAI_VERSION'
226- value : useAzureOpenAI ? azureOpenAIAPIVersion : ''
227- }
228- ]
229- var webAppEnvWithSecret = !empty (azureOpenAIKey ) ? union (webAppEnv , [
230- {
231- name : 'AZURE_OPENAI_KEY'
232- secretRef : 'azure-openai-key'
269+ value : openAIEmbedHost == 'azure' ? azureOpenAIAPIVersion : ''
233270 }
234- ]) : webAppEnv
235-
236- var secrets = !empty (azureOpenAIKey ) ? {
237- 'azure-openai-key' : azureOpenAIKey
238- } : {}
271+ ])
239272
240273module web 'web.bicep' = {
241274 name : 'web'
@@ -248,15 +281,14 @@ module web 'web.bicep' = {
248281 containerAppsEnvironmentName : containerApps .outputs .environmentName
249282 containerRegistryName : containerApps .outputs .registryName
250283 exists : webAppExists
251- environmentVariables : webAppEnvWithSecret
284+ environmentVariables : webAppEnv
252285 secrets : secrets
253286 }
254287}
255288
256- resource openAIResourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' existing =
257- if (!empty (openAIResourceGroupName )) {
258- name : !empty (openAIResourceGroupName ) ? openAIResourceGroupName : resourceGroup .name
259- }
289+ resource openAIResourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' existing = if (!empty (openAIResourceGroupName )) {
290+ name : !empty (openAIResourceGroupName ) ? openAIResourceGroupName : resourceGroup .name
291+ }
260292
261293module openAI 'core/ai/cognitiveservices.bicep' = if (deployAzureOpenAI ) {
262294 name : 'openai'
@@ -299,16 +331,15 @@ module openAI 'core/ai/cognitiveservices.bicep' = if (deployAzureOpenAI) {
299331}
300332
301333// USER ROLES
302- module openAIRoleUser 'core/security/role.bicep' =
303- if (empty (runningOnGh )) {
304- scope : openAIResourceGroup
305- name : 'openai-role-user'
306- params : {
307- principalId : principalId
308- roleDefinitionId : '5e0bd9bd-7b93-4f28-af87-19fc36ad61bd'
309- principalType : 'User'
310- }
334+ module openAIRoleUser 'core/security/role.bicep' = if (empty (runningOnGh )) {
335+ scope : openAIResourceGroup
336+ name : 'openai-role-user'
337+ params : {
338+ principalId : principalId
339+ roleDefinitionId : '5e0bd9bd-7b93-4f28-af87-19fc36ad61bd'
340+ principalType : 'User'
311341 }
342+ }
312343
313344// Backend roles
314345module openAIRoleBackend 'core/security/role.bicep' = {
@@ -334,13 +365,11 @@ output SERVICE_WEB_NAME string = web.outputs.SERVICE_WEB_NAME
334365output SERVICE_WEB_URI string = web .outputs .SERVICE_WEB_URI
335366output SERVICE_WEB_IMAGE_NAME string = web .outputs .SERVICE_WEB_IMAGE_NAME
336367
337- output AZURE_OPENAI_ENDPOINT string = useAzureOpenAI ? (deployAzureOpenAI ? openAI .outputs .endpoint : azureOpenAIEndpoint ) : ''
338- output AZURE_OPENAI_VERSION string = useAzureOpenAI ? azureOpenAIAPIVersion : ''
339- output AZURE_OPENAI_CHAT_DEPLOYMENT string = useAzureOpenAI ? chatConfig .deploymentName : ''
340- output AZURE_OPENAI_EMBED_DEPLOYMENT string = useAzureOpenAI ? embedConfig .deploymentName : ''
341- output AZURE_OPENAI_CHAT_MODEL string = useAzureOpenAI ? chatConfig .modelName : ''
342- output AZURE_OPENAI_EMBED_MODEL string = useAzureOpenAI ? embedConfig .modelName : ''
343- output AZURE_OPENAI_EMBED_MODEL_DIMENSIONS int = useAzureOpenAI ? embedConfig .dimensions : 0
368+ output AZURE_OPENAI_ENDPOINT string = !empty (azureOpenAIEndpoint )
369+ ? azureOpenAIEndpoint
370+ : (deployAzureOpenAI ? openAI .outputs .endpoint : '' )
371+ output AZURE_OPENAI_CHAT_DEPLOYMENT string = deployAzureOpenAI ? chatConfig .deploymentName : ''
372+ output AZURE_OPENAI_EMBED_DEPLOYMENT string = deployAzureOpenAI ? embedConfig .deploymentName : ''
344373
345374output POSTGRES_HOST string = postgresServer .outputs .POSTGRES_DOMAIN_NAME
346375output POSTGRES_USERNAME string = postgresEntraAdministratorName
0 commit comments