Skip to content

Commit fc1a681

Browse files
committed
Use bicep and env vars for backends
1 parent f385736 commit fc1a681

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

app/backend/app.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import logging
55
import mimetypes
66
import os
7-
import httpx
87
from pathlib import Path
9-
from typing import Any, AsyncGenerator, Dict, Union, cast, List
8+
from typing import Any, AsyncGenerator, Dict, List, Union, cast
109

10+
import httpx
1111
from azure.core.exceptions import ResourceNotFoundError
1212
from azure.identity.aio import DefaultAzureCredential, get_bearer_token_provider
1313
from azure.monitor.opentelemetry import configure_azure_monitor
@@ -76,11 +76,6 @@
7676
mimetypes.add_type("application/javascript", ".js")
7777
mimetypes.add_type("text/css", ".css")
7878

79-
# Data for the backends could be supplied through config. This data is simply here to illustrate usage.
80-
backends: List[Backend] = [
81-
Backend("cog-w2og7ojyhvoq6.openai.azure.com", 1),
82-
Backend("cog-kfdf7d5q443bu.openai.azure.com", 1),
83-
]
8479

8580
@bp.route("/")
8681
async def index():
@@ -438,13 +433,22 @@ async def setup_clients():
438433

439434
api_version = os.getenv("AZURE_OPENAI_API_VERSION") or "2024-03-01-preview"
440435

441-
lb = AsyncLoadBalancer(backends)
436+
client_args = {}
437+
if AZURE_OPENAI_SERVICE_BACKEND2 := os.environ.get("AZURE_OPENAI_SERVICE_BACKEND2"):
438+
backends: List[Backend] = [
439+
Backend(f"{AZURE_OPENAI_SERVICE}.openai.azure.com", 1),
440+
Backend(f"{AZURE_OPENAI_SERVICE_BACKEND2}.openai.azure.com", 1),
441+
]
442+
443+
lb = AsyncLoadBalancer(backends)
444+
# Inject the load balancer as the transport in a new default httpx client
445+
client_args["http_client"] = httpx.AsyncClient(transport=lb)
442446

443447
openai_client = AsyncAzureOpenAI(
444448
api_version=api_version,
445449
azure_endpoint=endpoint,
446450
azure_ad_token_provider=token_provider,
447-
http_client = httpx.AsyncClient(transport = lb) # Inject the load balancer as the transport in a new default httpx client
451+
**client_args,
448452
)
449453
elif OPENAI_HOST == "local":
450454
openai_client = AsyncOpenAI(

infra/main.bicep

+20
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ param appServiceSkuName string // Set in main.parameters.json
4343
@allowed([ 'azure', 'openai', 'azure_custom' ])
4444
param openAiHost string // Set in main.parameters.json
4545
param isAzureOpenAiHost bool = startsWith(openAiHost, 'azure')
46+
param deployAzureOpenAiBackendTwo bool = false
4647
param azureOpenAiCustomUrl string = ''
4748
param azureOpenAiApiVersion string = ''
4849

@@ -294,6 +295,7 @@ module backend 'core/host/appservice.bicep' = {
294295
AZURE_OPENAI_GPT4V_MODEL: gpt4vModelName
295296
// Specific to Azure OpenAI
296297
AZURE_OPENAI_SERVICE: isAzureOpenAiHost ? openAi.outputs.name : ''
298+
AZURE_OPENAI_SERVICE_BACKEND2: isAzureOpenAiHost && deployAzureOpenAiBackendTwo ? openAiBackendTwo.outputs.name : ''
297299
AZURE_OPENAI_CHATGPT_DEPLOYMENT: chatGpt.deploymentName
298300
AZURE_OPENAI_EMB_DEPLOYMENT: embedding.deploymentName
299301
AZURE_OPENAI_GPT4V_DEPLOYMENT: useGPT4V ? gpt4vDeploymentName : ''
@@ -385,6 +387,23 @@ module openAi 'core/ai/cognitiveservices.bicep' = if (isAzureOpenAiHost) {
385387
}
386388
}
387389

390+
module openAiBackendTwo 'core/ai/cognitiveservices.bicep' = if (isAzureOpenAiHost && deployAzureOpenAiBackendTwo) {
391+
name: 'openai-backend-two'
392+
scope: openAiResourceGroup
393+
params: {
394+
name: '${abbrs.cognitiveServicesAccounts}${resourceToken}-b2'
395+
location: openAiResourceGroupLocation
396+
tags: tags
397+
publicNetworkAccess: publicNetworkAccess
398+
bypass: bypass
399+
sku: {
400+
name: openAiSkuName
401+
}
402+
deployments: openAiDeployments
403+
disableLocalAuth: true
404+
}
405+
}
406+
388407
// Formerly known as Form Recognizer
389408
// Does not support bypass
390409
module documentIntelligence 'core/ai/cognitiveservices.bicep' = {
@@ -766,6 +785,7 @@ output AZURE_OPENAI_GPT4V_MODEL string = gpt4vModelName
766785

767786
// Specific to Azure OpenAI
768787
output AZURE_OPENAI_SERVICE string = isAzureOpenAiHost ? openAi.outputs.name : ''
788+
output AZURE_OPENAI_SERVICE_BACKEND2 string = isAzureOpenAiHost && deployAzureOpenAiBackendTwo ? openAiBackendTwo.outputs.name : ''
769789
output AZURE_OPENAI_API_VERSION string = isAzureOpenAiHost ? azureOpenAiApiVersion : ''
770790
output AZURE_OPENAI_RESOURCE_GROUP string = isAzureOpenAiHost ? openAiResourceGroup.name : ''
771791
output AZURE_OPENAI_CHATGPT_DEPLOYMENT string = isAzureOpenAiHost ? chatGpt.deploymentName : ''

infra/main.parameters.json

+3
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@
119119
"openAiHost": {
120120
"value": "${OPENAI_HOST=azure}"
121121
},
122+
"deployAzureOpenAiBackendTwo": {
123+
"value": "${DEPLOY_AZURE_OPENAI_BACKEND_TWO=false}"
124+
},
122125
"azureOpenAiCustomUrl":{
123126
"value": "${AZURE_OPENAI_CUSTOM_URL}"
124127
},

0 commit comments

Comments
 (0)