Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 406eb24

Browse files
Merge pull request #64 from Azure-Samples/openai
Properly support OpenAI.com keys
2 parents 6de0b9e + 2d295bb commit 406eb24

File tree

6 files changed

+111
-61
lines changed

6 files changed

+111
-61
lines changed

‎README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ A related option is VS Code Dev Containers, which will open the project in your
5858
1. Make sure the following tools are installed:
5959

6060
* [Azure Developer CLI (azd)](https://aka.ms/install-azd)
61+
* [Node.js 18+](https://nodejs.org/download/)
6162
* [Python 3.10+](https://www.python.org/downloads/)
6263
* [PostgreSQL 14+](https://www.postgresql.org/download/)
6364
* [pgvector](https://github.com/pgvector/pgvector)
@@ -99,6 +100,8 @@ Once you've opened the project in [Codespaces](#github-codespaces), [Dev Contain
99100
100101
This will create a folder under `.azure/` in your project to store the configuration for this deployment. You may have multiple azd environments if desired.
101102
103+
3. (Optional) If you would like to customize the deployment to [use existing Azure resources](docs/deploy_existing.md), you can set the values now.
104+
102105
3. Provision the resources and deploy the code:
103106
104107
```shell

‎docs/deploy_existing.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Deploying with existing Azure resources
2+
3+
If you already have existing Azure resources, or if you want to specify the exact name of new Azure Resource, you can do so by setting `azd` environment values.
4+
You should set these values before running `azd up`. Once you've set them, return to the [deployment steps](../README.md#deployment).
5+
6+
### Openai.com OpenAI account
7+
8+
1. Run `azd env set DEPLOY_AZURE_OPENAI false`
9+
1. Run `azd env set OPENAI_CHAT_HOST openaicom`
10+
2. Run `azd env set OPENAI_EMBED_HOST openaicom`
11+
3. Run `azd env set OPENAICOM_KEY {Your OpenAI API key}`
12+
4. Run `azd up`
13+
14+
You can retrieve your OpenAI key by checking [your user page](https://platform.openai.com/account/api-keys).
15+
Learn more about creating an OpenAI free trial at [this link](https://openai.com/pricing).
16+
Do *not* check your key into source control.
17+
18+
When you run `azd up` after and are prompted to select a value for `openAiResourceGroupLocation`, you can select any location as it will not be used.

‎infra/core/host/container-app-upsert.bicep‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ param serviceBinds array = []
7575
@description('The target port for the container')
7676
param targetPort int = 80
7777

78-
// Service options
79-
@description('PostgreSQL service ID')
80-
param postgresServiceId string = ''
81-
8278
resource existingApp 'Microsoft.App/containerApps@2023年05月02日-preview' existing = if (exists) {
8379
name: name
8480
}
@@ -103,7 +99,6 @@ module app 'container-app.bicep' = {
10399
daprEnabled: daprEnabled
104100
daprAppId: daprAppId
105101
daprAppProtocol: daprAppProtocol
106-
postgresServiceId: postgresServiceId
107102
secrets: secrets
108103
keyvaultIdentities: keyvaultIdentities
109104
external: external

‎infra/core/host/container-app.bicep‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ param containerRegistryName string = ''
3131
@description('Hostname suffix for container registry. Set when deploying to sovereign clouds')
3232
param containerRegistryHostSuffix string = 'azurecr.io'
3333

34-
// Service options
35-
@description('PostgreSQL service ID')
36-
param postgresServiceId string = ''
37-
3834
@description('The protocol used by Dapr to connect to the app, e.g., http or grpc')
3935
@allowed([ 'http', 'grpc' ])
4036
param daprAppProtocol string = 'http'
@@ -102,7 +98,7 @@ var keyvaultIdentitySecrets = [for secret in items(keyvaultIdentities): {
10298
name: secret.key
10399
keyVaultUrl: secret.value.keyVaultUrl
104100
identity: secret.value.identity
105-
}]
101+
}]
106102

107103
module containerRegistryAccess '../security/registry-access.bicep' = if (usePrivateRegistry) {
108104
name: '${deployment().name}-registry-access'
@@ -118,7 +114,7 @@ resource app 'Microsoft.App/containerApps@2023年05月02日-preview' = {
118114
tags: tags
119115
// It is critical that the identity is granted ACR pull access before the app is created
120116
// otherwise the container app will throw a provision error
121-
// This also forces us to use an user assigned managed identity since there would no way to
117+
// This also forces us to use an user assigned managed identity since there would no way to
122118
// provide the system assigned identity with the ACR pull access before the app is created
123119
dependsOn: usePrivateRegistry ? [ containerRegistryAccess ] : []
124120
identity: {

‎infra/main.bicep‎

Lines changed: 78 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ param openAIResourceGroupName string = ''
3737
@description('Whether to deploy Azure OpenAI resources')
3838
param 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')
4156
param chatModelName string = ''
4257
@description('Name of the model deployment')
@@ -50,18 +65,16 @@ param chatDeploymentVersion string = ''
5065
param azureOpenAIAPIVersion string = '2024年03月01日-preview'
5166
@secure()
5267
param azureOpenAIKey string = ''
68+
5369
@description('Azure OpenAI endpoint to use, if not using the one deployed here.')
5470
param 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
6275
param chatDeploymentCapacity int = 0
6376
var 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
153166
var webAppName = replace('${take(prefix, 19)}-ca', '--', '-')
154167
var 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

240273
module 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

261293
module 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
314345
module openAIRoleBackend 'core/security/role.bicep' = {
@@ -334,13 +365,11 @@ output SERVICE_WEB_NAME string = web.outputs.SERVICE_WEB_NAME
334365
output SERVICE_WEB_URI string = web.outputs.SERVICE_WEB_URI
335366
output 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

345374
output POSTGRES_HOST string = postgresServer.outputs.POSTGRES_DOMAIN_NAME
346375
output POSTGRES_USERNAME string = postgresEntraAdministratorName

‎infra/main.parameters.json‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
"azureOpenAIEndpoint": {
2727
"value": "${AZURE_OPENAI_ENDPOINT}"
2828
},
29-
"chatModelName":{
29+
"openAIChatHost": {
30+
"value": "${OPENAI_CHAT_HOST=azure}"
31+
},
32+
"chatModelName": {
3033
"value": "${AZURE_OPENAI_CHAT_MODEL}"
3134
},
3235
"chatDeploymentName": {
@@ -38,6 +41,9 @@
3841
"chatDeploymentCapacity":{
3942
"value": "${AZURE_OPENAI_CHAT_DEPLOYMENT_CAPACITY}"
4043
},
44+
"openAIEmbedHost": {
45+
"value": "${OPENAI_EMBED_HOST=azure}"
46+
},
4147
"embedModelName":{
4248
"value": "${AZURE_OPENAI_EMBED_MODEL}"
4349
},
@@ -52,6 +58,9 @@
5258
},
5359
"embedDimensions": {
5460
"value": "${AZURE_OPENAI_EMBED_DIMENSIONS}"
61+
},
62+
"openAIComKey": {
63+
"value": "${OPENAICOM_KEY}"
5564
}
5665
}
5766
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /