Skip to content

Commit 404914c

Browse files
committed
Changes needed for workshop
1 parent d5b08fc commit 404914c

File tree

7 files changed

+82
-47
lines changed

7 files changed

+82
-47
lines changed

.env.sample

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ OPENAI_EMBED_HOST=azure
1313
# You also need to `azd auth login` if running this locally
1414
AZURE_OPENAI_ENDPOINT=https://YOUR-AZURE-OPENAI-SERVICE-NAME.openai.azure.com
1515
AZURE_OPENAI_VERSION=2024-03-01-preview
16-
AZURE_OPENAI_CHAT_DEPLOYMENT=chat
16+
AZURE_OPENAI_CHAT_DEPLOYMENT=gpt-35-turbo
1717
AZURE_OPENAI_CHAT_MODEL=gpt-35-turbo
18-
AZURE_OPENAI_EMBED_DEPLOYMENT=embed
18+
AZURE_OPENAI_EMBED_DEPLOYMENT=text-embedding-ada-002
1919
AZURE_OPENAI_EMBED_MODEL=text-embedding-ada-002
2020
AZURE_OPENAI_EMBED_MODEL_DIMENSIONS=1536
2121
# Needed for OpenAI.com:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ Since the local app uses OpenAI models, you should first deploy it for the optim
145145
146146
There must be an initial build of static assets before running the backend, since the backend serves static files from the `src/static` directory.
147147
148-
3. Run the FastAPI backend (with hot reloading):
148+
3. Run the FastAPI backend (with hot reloading). This should be run from the root of the project:
149149
150150
```shell
151151
python3 -m uvicorn fastapi_app:create_app --factory --reload

infra/main.bicep

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ param principalId string = ''
3232
param openAILocation string
3333

3434
@description('Name of the OpenAI resource group. If not specified, the resource group name will be generated.')
35-
param openAiResourceGroupName string = ''
35+
param openAIResourceGroupName string = ''
3636

3737
@description('Whether to deploy Azure OpenAI resources')
3838
param deployAzureOpenAI bool = true
@@ -47,15 +47,22 @@ param chatDeploymentName string = ''
4747
// https://learn.microsoft.com/azure/ai-services/openai/concepts/models#gpt-4-and-gpt-4-turbo-preview-models
4848
param chatDeploymentVersion string = ''
4949

50-
param azureOpenAiAPIVersion string = '2024-03-01-preview'
50+
param azureOpenAIAPIVersion string = '2024-03-01-preview'
51+
@secure()
52+
param azureOpenAIKey string = ''
53+
@description('Azure OpenAI endpoint to use, if not using the one deployed here.')
54+
param azureOpenAIEndpoint string = ''
55+
56+
@description('Whether to use Azure OpenAI (either deployed here or elsewhere) or OpenAI.com')
57+
var useAzureOpenAI = deployAzureOpenAI || !empty(azureOpenAIEndpoint)
5158

5259
@description('Capacity of the GPT deployment')
5360
// You can increase this, but capacity is limited per model/region, so you will get errors if you go over
5461
// https://learn.microsoft.com/en-us/azure/ai-services/openai/quotas-limits
5562
param chatDeploymentCapacity int = 0
5663
var chatConfig = {
57-
modelName: !empty(chatModelName) ? chatModelName : deployAzureOpenAI ? 'gpt-35-turbo' : 'gpt-3.5-turbo'
58-
deploymentName: !empty(chatDeploymentName) ? chatDeploymentName : 'chat'
64+
modelName: !empty(chatModelName) ? chatModelName : (useAzureOpenAI ? 'gpt-35-turbo' : 'gpt-3.5-turbo')
65+
deploymentName: !empty(chatDeploymentName) ? chatDeploymentName : 'gpt-35-turbo'
5966
deploymentVersion: !empty(chatDeploymentVersion) ? chatDeploymentVersion : '0125'
6067
deploymentCapacity: chatDeploymentCapacity != 0 ? chatDeploymentCapacity : 30
6168
}
@@ -68,7 +75,7 @@ param embedDimensions int = 0
6875

6976
var embedConfig = {
7077
modelName: !empty(embedModelName) ? embedModelName : 'text-embedding-ada-002'
71-
deploymentName: !empty(embedDeploymentName) ? embedDeploymentName : 'embed'
78+
deploymentName: !empty(embedDeploymentName) ? embedDeploymentName : 'text-embedding-ada-002'
7279
deploymentVersion: !empty(embedDeploymentVersion) ? embedDeploymentVersion : '2'
7380
deploymentCapacity: embedDeploymentCapacity != 0 ? embedDeploymentCapacity : 30
7481
dimensions: embedDimensions != 0 ? embedDimensions : 1536
@@ -183,64 +190,71 @@ module web 'web.bicep' = {
183190
}
184191
{
185192
name: 'OPENAI_CHAT_HOST'
186-
value: deployAzureOpenAI ? 'azure' : 'openaicom'
193+
value: useAzureOpenAI ? 'azure' : 'openaicom'
187194
}
188195
{
189196
name: 'AZURE_OPENAI_CHAT_DEPLOYMENT'
190-
value: deployAzureOpenAI ? chatConfig.deploymentName : ''
197+
value: useAzureOpenAI ? chatConfig.deploymentName : ''
191198
}
192199
{
193200
name: 'AZURE_OPENAI_CHAT_MODEL'
194-
value: deployAzureOpenAI ? chatConfig.modelName : ''
201+
value: useAzureOpenAI ? chatConfig.modelName : ''
195202
}
196203
{
197204
name: 'OPENAICOM_CHAT_MODEL'
198-
value: deployAzureOpenAI ? '' : 'gpt-3.5-turbo'
205+
value: useAzureOpenAI ? '' : 'gpt-3.5-turbo'
199206
}
200207
{
201208
name: 'OPENAI_EMBED_HOST'
202-
value: deployAzureOpenAI ? 'azure' : 'openaicom'
209+
value: useAzureOpenAI ? 'azure' : 'openaicom'
203210
}
204211
{
205212
name: 'OPENAICOM_EMBED_MODEL_DIMENSIONS'
206-
value: deployAzureOpenAI ? '' : '1536'
213+
value: useAzureOpenAI ? '' : '1536'
207214
}
208215
{
209216
name: 'OPENAICOM_EMBED_MODEL'
210-
value: deployAzureOpenAI ? '' : 'text-embedding-ada-002'
217+
value: useAzureOpenAI ? '' : 'text-embedding-ada-002'
211218
}
212219
{
213220
name: 'AZURE_OPENAI_EMBED_MODEL'
214-
value: deployAzureOpenAI ? embedConfig.modelName : ''
221+
value: useAzureOpenAI ? embedConfig.modelName : ''
215222
}
216223
{
217224
name: 'AZURE_OPENAI_EMBED_DEPLOYMENT'
218-
value: deployAzureOpenAI ? embedConfig.deploymentName : ''
225+
value: useAzureOpenAI ? embedConfig.deploymentName : ''
219226
}
220227
{
221228
name: 'AZURE_OPENAI_EMBED_MODEL_DIMENSIONS'
222-
value: deployAzureOpenAI ? string(embedConfig.dimensions) : ''
229+
value: useAzureOpenAI ? string(embedConfig.dimensions) : ''
223230
}
224231
{
225232
name: 'AZURE_OPENAI_ENDPOINT'
226-
value: deployAzureOpenAI ? openAi.outputs.endpoint : ''
233+
value: useAzureOpenAI ? (deployAzureOpenAI ? openAI.outputs.endpoint : azureOpenAIEndpoint) : ''
227234
}
228235
{
229236
name: 'AZURE_OPENAI_VERSION'
230-
value: deployAzureOpenAI ? azureOpenAiAPIVersion : ''
237+
value: useAzureOpenAI ? azureOpenAIAPIVersion : ''
238+
}
239+
{
240+
name: 'AZURE_OPENAI_KEY'
241+
secretRef: 'azure-openai-key'
231242
}
232243
]
244+
secrets: {
245+
'azure-openai-key': azureOpenAIKey
246+
}
233247
}
234248
}
235249

236-
resource openAiResourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' existing =
237-
if (!empty(openAiResourceGroupName)) {
238-
name: !empty(openAiResourceGroupName) ? openAiResourceGroupName : resourceGroup.name
250+
resource openAIResourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' existing =
251+
if (!empty(openAIResourceGroupName)) {
252+
name: !empty(openAIResourceGroupName) ? openAIResourceGroupName : resourceGroup.name
239253
}
240254

241-
module openAi 'core/ai/cognitiveservices.bicep' = {
255+
module openAI 'core/ai/cognitiveservices.bicep' = if (deployAzureOpenAI) {
242256
name: 'openai'
243-
scope: openAiResourceGroup
257+
scope: openAIResourceGroup
244258
params: {
245259
name: '${prefix}-openai'
246260
location: openAILocation
@@ -279,9 +293,9 @@ module openAi 'core/ai/cognitiveservices.bicep' = {
279293
}
280294

281295
// USER ROLES
282-
module openAiRoleUser 'core/security/role.bicep' =
296+
module openAIRoleUser 'core/security/role.bicep' =
283297
if (empty(runningOnGh)) {
284-
scope: openAiResourceGroup
298+
scope: openAIResourceGroup
285299
name: 'openai-role-user'
286300
params: {
287301
principalId: principalId
@@ -291,8 +305,8 @@ module openAiRoleUser 'core/security/role.bicep' =
291305
}
292306

293307
// Backend roles
294-
module openAiRoleBackend 'core/security/role.bicep' = {
295-
scope: openAiResourceGroup
308+
module openAIRoleBackend 'core/security/role.bicep' = {
309+
scope: openAIResourceGroup
296310
name: 'openai-role-backend'
297311
params: {
298312
principalId: web.outputs.SERVICE_WEB_IDENTITY_PRINCIPAL_ID
@@ -314,13 +328,13 @@ output SERVICE_WEB_NAME string = web.outputs.SERVICE_WEB_NAME
314328
output SERVICE_WEB_URI string = web.outputs.SERVICE_WEB_URI
315329
output SERVICE_WEB_IMAGE_NAME string = web.outputs.SERVICE_WEB_IMAGE_NAME
316330

317-
output AZURE_OPENAI_ENDPOINT string = deployAzureOpenAI ? openAi.outputs.endpoint : ''
318-
output AZURE_OPENAI_VERSION string = deployAzureOpenAI ? azureOpenAiAPIVersion : ''
319-
output AZURE_OPENAI_CHAT_DEPLOYMENT string = deployAzureOpenAI ? chatConfig.deploymentName : ''
320-
output AZURE_OPENAI_EMBED_DEPLOYMENT string = deployAzureOpenAI ? embedConfig.deploymentName : ''
321-
output AZURE_OPENAI_CHAT_MODEL string = deployAzureOpenAI ? chatConfig.modelName : ''
322-
output AZURE_OPENAI_EMBED_MODEL string = deployAzureOpenAI ? embedConfig.modelName : ''
323-
output AZURE_OPENAI_EMBED_MODEL_DIMENSIONS int = deployAzureOpenAI ? embedConfig.dimensions : 0
331+
output AZURE_OPENAI_ENDPOINT string = useAzureOpenAI ? (deployAzureOpenAI ? openAI.outputs.endpoint : azureOpenAIEndpoint) : ''
332+
output AZURE_OPENAI_VERSION string = useAzureOpenAI ? azureOpenAIAPIVersion : ''
333+
output AZURE_OPENAI_CHAT_DEPLOYMENT string = useAzureOpenAI ? chatConfig.deploymentName : ''
334+
output AZURE_OPENAI_EMBED_DEPLOYMENT string = useAzureOpenAI ? embedConfig.deploymentName : ''
335+
output AZURE_OPENAI_CHAT_MODEL string = useAzureOpenAI ? chatConfig.modelName : ''
336+
output AZURE_OPENAI_EMBED_MODEL string = useAzureOpenAI ? embedConfig.modelName : ''
337+
output AZURE_OPENAI_EMBED_MODEL_DIMENSIONS int = useAzureOpenAI ? embedConfig.dimensions : 0
324338

325339
output POSTGRES_HOST string = postgresServer.outputs.POSTGRES_DOMAIN_NAME
326340
output POSTGRES_USERNAME string = postgresEntraAdministratorName

infra/main.parameters.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
"deployAzureOpenAI": {
2121
"value": "${DEPLOY_AZURE_OPENAI=true}"
2222
},
23+
"azureOpenAIKey": {
24+
"value": "${AZURE_OPENAI_KEY}"
25+
},
26+
"azureOpenAIEndpoint": {
27+
"value": "${AZURE_OPENAI_ENDPOINT}"
28+
},
2329
"chatModelName":{
2430
"value": "${AZURE_OPENAI_CHAT_MODEL}"
2531
},

infra/web.bicep

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ param exists bool
88
param identityName string
99
param serviceName string = 'web'
1010
param environmentVariables array = []
11+
@secure()
12+
param secrets object
1113

1214
resource webIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = {
1315
name: identityName
@@ -37,6 +39,7 @@ module app 'core/host/container-app-upsert.bicep' = {
3739
}
3840
]
3941
)
42+
secrets: secrets
4043
targetPort: 8000
4144
}
4245
}

src/fastapi_app/openai_clients.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,21 @@
1010
async def create_openai_chat_client(azure_credential):
1111
OPENAI_CHAT_HOST = os.getenv("OPENAI_CHAT_HOST")
1212
if OPENAI_CHAT_HOST == "azure":
13-
logger.info("Authenticating to OpenAI using Azure Identity...")
14-
15-
token_provider = azure.identity.get_bearer_token_provider(
16-
azure_credential, "https://cognitiveservices.azure.com/.default"
17-
)
13+
client_args = {}
14+
if api_key := os.getenv("AZURE_OPENAI_KEY"):
15+
logger.info("Authenticating to Azure OpenAI using API key...")
16+
client_args["api_key"] = api_key
17+
else:
18+
logger.info("Authenticating to Azure OpenAI using Azure Identity...")
19+
token_provider = azure.identity.get_bearer_token_provider(
20+
azure_credential, "https://cognitiveservices.azure.com/.default"
21+
)
22+
client_args["azure_ad_token_provider"] = token_provider
1823
openai_chat_client = openai.AsyncAzureOpenAI(
1924
api_version=os.getenv("AZURE_OPENAI_VERSION"),
2025
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
21-
azure_ad_token_provider=token_provider,
2226
azure_deployment=os.getenv("AZURE_OPENAI_CHAT_DEPLOYMENT"),
27+
**client_args,
2328
)
2429
openai_chat_model = os.getenv("AZURE_OPENAI_CHAT_MODEL")
2530
elif OPENAI_CHAT_HOST == "ollama":
@@ -40,14 +45,21 @@ async def create_openai_chat_client(azure_credential):
4045
async def create_openai_embed_client(azure_credential):
4146
OPENAI_EMBED_HOST = os.getenv("OPENAI_EMBED_HOST")
4247
if OPENAI_EMBED_HOST == "azure":
43-
token_provider = azure.identity.get_bearer_token_provider(
44-
azure_credential, "https://cognitiveservices.azure.com/.default"
45-
)
48+
client_args = {}
49+
if api_key := os.getenv("AZURE_OPENAI_KEY"):
50+
logger.info("Authenticating to Azure OpenAI using API key...")
51+
client_args["api_key"] = api_key
52+
else:
53+
logger.info("Authenticating to Azure OpenAI using Azure Identity...")
54+
token_provider = azure.identity.get_bearer_token_provider(
55+
azure_credential, "https://cognitiveservices.azure.com/.default"
56+
)
57+
client_args["azure_ad_token_provider"] = token_provider
4658
openai_embed_client = openai.AsyncAzureOpenAI(
4759
api_version=os.getenv("AZURE_OPENAI_VERSION"),
4860
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
49-
azure_ad_token_provider=token_provider,
5061
azure_deployment=os.getenv("AZURE_OPENAI_EMBED_DEPLOYMENT"),
62+
**client_args,
5163
)
5264
openai_embed_model = os.getenv("AZURE_OPENAI_EMBED_MODEL")
5365
openai_embed_dimensions = os.getenv("AZURE_OPENAI_EMBED_DIMENSIONS")

src/frontend/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="UTF-8" />
55
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7-
<title>GPT + Enterprise data | Sample</title>
7+
<title>RAG on PostgreSQL</title>
88
</head>
99
<body>
1010
<div id="root"></div>

0 commit comments

Comments
 (0)