Skip to content

Commit 4348cf3

Browse files
authored
Merge pull request #96 from Azure-Samples/howie/goldpath
new env name for agent id and connection str
2 parents 31fbe42 + 7a347cb commit 4348cf3

File tree

11 files changed

+68
-43
lines changed

11 files changed

+68
-43
lines changed

.github/workflows/azure-dev.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
USE_APPLICATION_INSIGHTS: ${{ vars.USE_APPLICATION_INSIGHTS }}
4040
USE_SEARCH_SERVICE: ${{ vars.USE_SEARCH_SERVICE }}
4141
AZURE_AI_AGENT_NAME: ${{ vars.AZURE_AI_AGENT_NAME }}
42-
AZURE_AI_AGENT_ID: ${{ vars.AZURE_AI_AGENT_ID }}
42+
AZURE_EXISTING_AGENT_ID: ${{ vars.AZURE_EXISTING_AGENT_ID }}
4343
AZURE_AI_AGENT_DEPLOYMENT_NAME: ${{ vars.AZURE_AI_AGENT_DEPLOYMENT_NAME }}
4444
AZURE_AI_AGENT_DEPLOYMENT_SKU: ${{ vars.AZURE_AI_AGENT_DEPLOYMENT_SKU }}
4545
AZURE_AI_AGENT_DEPLOYMENT_CAPACITY: ${{ vars.AZURE_AI_AGENT_DEPLOYMENT_CAPACITY }}

.github/workflows/template-validation.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
USE_APPLICATION_INSIGHTS: ${{ vars.USE_APPLICATION_INSIGHTS }}
3939
USE_SEARCH_SERVICE: ${{ vars.USE_SEARCH_SERVICE }}
4040
AZURE_AI_AGENT_NAME: ${{ vars.AZURE_AI_AGENT_NAME }}
41-
AZURE_AI_AGENT_ID: ${{ vars.AZURE_AI_AGENT_ID }}
41+
AZURE_EXISTING_AGENT_ID: ${{ vars.AZURE_EXISTING_AGENT_ID }}
4242
AZURE_AI_AGENT_DEPLOYMENT_NAME: ${{ vars.AZURE_AI_AGENT_DEPLOYMENT_NAME }}
4343
AZURE_AI_AGENT_DEPLOYMENT_SKU: ${{ vars.AZURE_AI_AGENT_DEPLOYMENT_SKU }}
4444
AZURE_AI_AGENT_DEPLOYMENT_CAPACITY: ${{ vars.AZURE_AI_AGENT_DEPLOYMENT_CAPACITY }}

infra/api.bicep

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ var env = [
2626
value: apiIdentity.properties.clientId
2727
}
2828
{
29-
name: 'AZURE_AIPROJECT_CONNECTION_STRING'
29+
name: 'AZURE_EXISTING_AIPROJECT_CONNECTION_STRING'
3030
value: projectConnectionString
3131
}
3232
{
3333
name: 'AZURE_AI_AGENT_NAME'
3434
value: agentName
3535
}
3636
{
37-
name: 'AZURE_AI_AGENT_ID'
37+
name: 'AZURE_EXISTING_AGENT_ID'
3838
value: agentID
3939
}
4040
{

infra/main.bicep

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ param logAnalyticsWorkspaceName string = ''
6262
param agentModelFormat string = 'OpenAI'
6363
@description('Name of agent to deploy')
6464
param agentName string = 'agent-template-assistant'
65-
@description('ID of agent to deploy')
65+
@description('(Deprecated) ID of agent to deploy')
6666
param aiAgentID string = ''
67+
@description('ID of the existing agent')
68+
param azureExistingAgentId string = ''
6769
@description('Name of the chat model to deploy')
6870
param agentModelName string = 'gpt-4o-mini'
6971
@description('Name of the model deployment')
@@ -120,7 +122,8 @@ var resourceToken = toLower(uniqueString(subscription().id, environmentName, loc
120122
var projectName = !empty(aiProjectName) ? aiProjectName : 'ai-project-${resourceToken}'
121123
var tags = { 'azd-env-name': environmentName }
122124

123-
var agentID = !empty(aiAgentID) ? aiAgentID : ''
125+
var tempAgentID = !empty(aiAgentID) ? aiAgentID : ''
126+
var agentID = !empty(azureExistingAgentId) ? azureExistingAgentId : tempAgentID
124127

125128
var aiChatModel = [
126129
{
@@ -396,15 +399,15 @@ output AZURE_RESOURCE_GROUP string = rg.name
396399

397400
// Outputs required for local development server
398401
output AZURE_TENANT_ID string = tenant().tenantId
399-
output AZURE_AIPROJECT_CONNECTION_STRING string = projectConnectionString
402+
output AZURE_EXISTING_AIPROJECT_CONNECTION_STRING string = projectConnectionString
400403
output AZURE_AI_AGENT_DEPLOYMENT_NAME string = agentDeploymentName
401404
output AZURE_AI_SEARCH_CONNECTION_NAME string = searchConnectionName
402405
output AZURE_AI_EMBED_DEPLOYMENT_NAME string = embeddingDeploymentName
403406
output AZURE_AI_SEARCH_INDEX_NAME string = aiSearchIndexName
404407
output AZURE_AI_SEARCH_ENDPOINT string = searchServiceEndpoint
405408
output AZURE_AI_EMBED_DIMENSIONS string = embeddingDeploymentDimensions
406409
output AZURE_AI_AGENT_NAME string = agentName
407-
output AZURE_AI_AGENT_ID string = agentID
410+
output AZURE_EXISTING_AGENT_ID string = agentID
408411

409412
// Outputs required by azd for ACA
410413
output AZURE_CONTAINER_ENVIRONMENT_NAME string = containerApps.outputs.environmentName

infra/main.json

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"_generator": {
66
"name": "bicep",
77
"version": "0.34.44.8038",
8-
"templateHash": "5625042254313524971"
8+
"templateHash": "18114037235110680016"
99
}
1010
},
1111
"parameters": {
@@ -23,17 +23,19 @@
2323
"eastus",
2424
"eastus2",
2525
"swedencentral",
26-
"switzerlandnorth",
2726
"westus",
2827
"westus3"
2928
],
3029
"metadata": {
3130
"azd": {
32-
"type": "location"
31+
"type": "location",
32+
"usageName": [
33+
"OpenAI.GlobalStandard.gpt-4o-mini,30",
34+
"OpenAI.GlobalStandard.text-embedding-3-small,30"
35+
]
3336
},
3437
"description": "Location for all resources"
35-
},
36-
"minLength": 1
38+
}
3739
},
3840
"aiExistingProjectConnectionString": {
3941
"type": "string",
@@ -148,7 +150,14 @@
148150
"type": "string",
149151
"defaultValue": "",
150152
"metadata": {
151-
"description": "ID of agent to deploy"
153+
"description": "(Deprecated) ID of agent to deploy"
154+
}
155+
},
156+
"azureExistingAgentId": {
157+
"type": "string",
158+
"defaultValue": "",
159+
"metadata": {
160+
"description": "ID of the existing agent"
152161
}
153162
},
154163
"agentModelName": {
@@ -401,7 +410,8 @@
401410
"tags": {
402411
"azd-env-name": "[parameters('environmentName')]"
403412
},
404-
"agentID": "[if(not(empty(parameters('aiAgentID'))), parameters('aiAgentID'), '')]",
413+
"tempAgentID": "[if(not(empty(parameters('aiAgentID'))), parameters('aiAgentID'), '')]",
414+
"agentID": "[if(not(empty(parameters('azureExistingAgentId'))), parameters('azureExistingAgentId'), variables('tempAgentID'))]",
405415
"aiChatModel": [
406416
{
407417
"name": "[parameters('agentDeploymentName')]",
@@ -488,7 +498,7 @@
488498
"_generator": {
489499
"name": "bicep",
490500
"version": "0.34.44.8038",
491-
"templateHash": "13049296426528843835"
501+
"templateHash": "691137933995863928"
492502
}
493503
},
494504
"parameters": {
@@ -628,7 +638,7 @@
628638
"_generator": {
629639
"name": "bicep",
630640
"version": "0.34.44.8038",
631-
"templateHash": "6691800427529095215"
641+
"templateHash": "618022947787971888"
632642
}
633643
},
634644
"parameters": {
@@ -2717,7 +2727,7 @@
27172727
"_generator": {
27182728
"name": "bicep",
27192729
"version": "0.34.44.8038",
2720-
"templateHash": "4408217411559527029"
2730+
"templateHash": "15702788402850698618"
27212731
},
27222732
"description": "Creates an Azure AI Search instance."
27232733
},
@@ -2810,6 +2820,7 @@
28102820
"tags": "[parameters('tags')]",
28112821
"identity": "[variables('searchIdentityProvider')]",
28122822
"properties": {
2823+
"authOptions": "[parameters('authOptions')]",
28132824
"disableLocalAuth": "[parameters('disableLocalAuth')]",
28142825
"disabledDataExfiltrationOptions": "[parameters('disabledDataExfiltrationOptions')]",
28152826
"encryptionWithCmk": "[parameters('encryptionWithCmk')]",
@@ -4040,7 +4051,7 @@
40404051
"_generator": {
40414052
"name": "bicep",
40424053
"version": "0.34.44.8038",
4043-
"templateHash": "15704795189831026439"
4054+
"templateHash": "8075064967424346344"
40444055
}
40454056
},
40464057
"parameters": {
@@ -4134,15 +4145,15 @@
41344145
"value": "[reference(resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', parameters('identityName')), '2023-01-31').clientId]"
41354146
},
41364147
{
4137-
"name": "AZURE_AIPROJECT_CONNECTION_STRING",
4148+
"name": "AZURE_EXISTING_AIPROJECT_CONNECTION_STRING",
41384149
"value": "[parameters('projectConnectionString')]"
41394150
},
41404151
{
41414152
"name": "AZURE_AI_AGENT_NAME",
41424153
"value": "[parameters('agentName')]"
41434154
},
41444155
{
4145-
"name": "AZURE_AI_AGENT_ID",
4156+
"name": "AZURE_EXISTING_AGENT_ID",
41464157
"value": "[parameters('agentID')]"
41474158
},
41484159
{
@@ -5539,7 +5550,7 @@
55395550
"type": "string",
55405551
"value": "[tenant().tenantId]"
55415552
},
5542-
"AZURE_AIPROJECT_CONNECTION_STRING": {
5553+
"AZURE_EXISTING_AIPROJECT_CONNECTION_STRING": {
55435554
"type": "string",
55445555
"value": "[if(empty(if(and(and(empty(parameters('aiExistingProjectConnectionString')), not(empty(reference(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, if(not(empty(parameters('resourceGroupName'))), parameters('resourceGroupName'), format('{0}{1}', variables('abbrs').resourcesResourceGroups, parameters('environmentName')))), 'Microsoft.Resources/deployments', 'ai'), '2022-09-01').outputs.discoveryUrl.value))), contains(reference(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, if(not(empty(parameters('resourceGroupName'))), parameters('resourceGroupName'), format('{0}{1}', variables('abbrs').resourcesResourceGroups, parameters('environmentName')))), 'Microsoft.Resources/deployments', 'ai'), '2022-09-01').outputs.discoveryUrl.value, '/')), split(reference(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, if(not(empty(parameters('resourceGroupName'))), parameters('resourceGroupName'), format('{0}{1}', variables('abbrs').resourcesResourceGroups, parameters('environmentName')))), 'Microsoft.Resources/deployments', 'ai'), '2022-09-01').outputs.discoveryUrl.value, '/')[2], '')), parameters('aiExistingProjectConnectionString'), format('{0};{1};{2};{3}', if(and(and(empty(parameters('aiExistingProjectConnectionString')), not(empty(reference(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, if(not(empty(parameters('resourceGroupName'))), parameters('resourceGroupName'), format('{0}{1}', variables('abbrs').resourcesResourceGroups, parameters('environmentName')))), 'Microsoft.Resources/deployments', 'ai'), '2022-09-01').outputs.discoveryUrl.value))), contains(reference(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, if(not(empty(parameters('resourceGroupName'))), parameters('resourceGroupName'), format('{0}{1}', variables('abbrs').resourcesResourceGroups, parameters('environmentName')))), 'Microsoft.Resources/deployments', 'ai'), '2022-09-01').outputs.discoveryUrl.value, '/')), split(reference(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, if(not(empty(parameters('resourceGroupName'))), parameters('resourceGroupName'), format('{0}{1}', variables('abbrs').resourcesResourceGroups, parameters('environmentName')))), 'Microsoft.Resources/deployments', 'ai'), '2022-09-01').outputs.discoveryUrl.value, '/')[2], ''), subscription().subscriptionId, if(not(empty(parameters('resourceGroupName'))), parameters('resourceGroupName'), format('{0}{1}', variables('abbrs').resourcesResourceGroups, parameters('environmentName'))), variables('projectName')))]"
55455556
},
@@ -5571,7 +5582,7 @@
55715582
"type": "string",
55725583
"value": "[parameters('agentName')]"
55735584
},
5574-
"AZURE_AI_AGENT_ID": {
5585+
"AZURE_EXISTING_AGENT_ID": {
55755586
"type": "string",
55765587
"value": "[variables('agentID')]"
55775588
},

infra/main.parameters.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
"aiAgentID": {
5757
"value": "${AZURE_AI_AGENT_ID}"
5858
},
59+
"azureExistingAgentId": {
60+
"value": "${AZURE_EXISTING_AGENT_ID}"
61+
},
5962
"agentDeploymentName": {
6063
"value": "${AZURE_AI_AGENT_MODEL_NAME=gpt-4o-mini}"
6164
},

scripts/write_env.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ $envFilePath = "src\.env"
55
Set-Content -Path $envFilePath -Value ""
66

77
# Append new values to the .env file
8-
$azureAiProjectConnectionString = azd env get-value AZURE_AIPROJECT_CONNECTION_STRING
8+
$azureAiProjectConnectionString = azd env get-value AZURE_EXISTING_AIPROJECT_CONNECTION_STRING
99
$azureAiagentDeploymentName = azd env get-value AZURE_AI_AGENT_DEPLOYMENT_NAME
10-
$azureAiAgentId = azd env get-value AZURE_AI_AGENT_ID
10+
$azureAiAgentId = azd env get-value AZURE_EXISTING_AGENT_ID
1111
$azureAiAgentName = azd env get-value AZURE_AI_AGENT_NAME
1212
$azureTenantId = azd env get-value AZURE_TENANT_ID
1313
$searchConnectionName = azd env get-value AZURE_AI_SEARCH_CONNECTION_NAME
@@ -17,9 +17,9 @@ $azureAISearchIndexName = azd env get-value AZURE_AI_SEARCH_INDEX_NAME
1717
$azureAISearchEndpoint = azd env get-value AZURE_AI_SEARCH_ENDPOINT
1818
$serviceAPIUri = azd env get-value SERVICE_API_URI
1919

20-
Add-Content -Path $envFilePath -Value "AZURE_AIPROJECT_CONNECTION_STRING=$azureAiProjectConnectionString"
20+
Add-Content -Path $envFilePath -Value "AZURE_EXISTING_AIPROJECT_CONNECTION_STRING=$azureAiProjectConnectionString"
2121
Add-Content -Path $envFilePath -Value "AZURE_AI_AGENT_DEPLOYMENT_NAME=$azureAiagentDeploymentName"
22-
Add-Content -Path $envFilePath -Value "AZURE_AI_AGENT_ID=$azureAiAgentId"
22+
Add-Content -Path $envFilePath -Value "AZURE_EXISTING_AGENT_ID=$azureAiAgentId"
2323
Add-Content -Path $envFilePath -Value "AZURE_TENANT_ID=$azureTenantId"
2424
Add-Content -Path $envFilePath -Value "AZURE_AI_SEARCH_CONNECTION_NAME=$searchConnectionName"
2525
Add-Content -Path $envFilePath -Value "AZURE_AI_EMBED_DEPLOYMENT_NAME=$azureAIEmbedDeploymentName"

scripts/write_env.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ ENV_FILE_PATH="src/.env"
66
# Clear the contents of the .env file
77
> $ENV_FILE_PATH
88

9-
echo "AZURE_AIPROJECT_CONNECTION_STRING=$(azd env get-value AZURE_AIPROJECT_CONNECTION_STRING)" >> $ENV_FILE_PATH
9+
echo "AZURE_EXISTING_AIPROJECT_CONNECTION_STRING=$(azd env get-value AZURE_EXISTING_AIPROJECT_CONNECTION_STRING)" >> $ENV_FILE_PATH
1010
echo "AZURE_AI_AGENT_DEPLOYMENT_NAME=$(azd env get-value AZURE_AI_AGENT_DEPLOYMENT_NAME)" >> $ENV_FILE_PATH
11-
echo "AZURE_AI_AGENT_ID=$(azd env get-value AZURE_AI_AGENT_ID)" >> $ENV_FILE_PATH
11+
echo "AZURE_EXISTING_AGENT_ID=$(azd env get-value AZURE_EXISTING_AGENT_ID)" >> $ENV_FILE_PATH
1212
echo "AZURE_TENANT_ID=$(azd env get-value AZURE_TENANT_ID)" >> $ENV_FILE_PATH
1313
echo "AZURE_AI_SEARCH_CONNECTION_NAME=$(azd env get-value AZURE_AI_SEARCH_CONNECTION_NAME)" >> $ENV_FILE_PATH
1414
echo "AZURE_AI_EMBED_DEPLOYMENT_NAME=$(azd env get-value AZURE_AI_EMBED_DEPLOYMENT_NAME)" >> $ENV_FILE_PATH

src/api/main.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
async def lifespan(app: fastapi.FastAPI):
2828
agent = None
2929

30+
connection_string = os.environ.get("AZURE_EXISTING_AIPROJECT_CONNECTION_STRING") if os.environ.get("AZURE_EXISTING_AIPROJECT_CONNECTION_STRING") else os.environ.get("AZURE_AIPROJECT_CONNECTION_STRING")
31+
agent_id = os.environ.get("AZURE_EXISTING_AGENT_ID") if os.environ.get("AZURE_EXISTING_AGENT_ID") else os.environ.get("AZURE_AI_AGENT_ID")
3032
try:
3133
ai_client = AIProjectClient.from_connection_string(
3234
credential=DefaultAzureCredential(exclude_shared_token_cache_credential=True),
33-
conn_str=os.environ["AZURE_AIPROJECT_CONNECTION_STRING"],
35+
conn_str=connection_string,
3436
)
3537
logger.info("Created AIProjectClient")
3638

@@ -51,9 +53,9 @@ async def lifespan(app: fastapi.FastAPI):
5153
# Do not instrument the code yet, before trace fix is available.
5254
#ai_client.telemetry.enable()
5355

54-
if os.environ.get("AZURE_AI_AGENT_ID"):
56+
if agent_id:
5557
try:
56-
agent = await ai_client.agents.get_agent(os.environ["AZURE_AI_AGENT_ID"])
58+
agent = await ai_client.agents.get_agent(agent_id)
5759
logger.info("Agent already exists, skipping creation")
5860
logger.info(f"Fetched agent, agent ID: {agent.id}")
5961
logger.info(f"Fetched agent, model name: {agent.model}")
@@ -72,7 +74,7 @@ async def lifespan(app: fastapi.FastAPI):
7274
break
7375

7476
if not agent:
75-
raise RuntimeError("No agent found. Ensure qunicorn.py created one or set AZURE_AI_AGENT_ID.")
77+
raise RuntimeError("No agent found. Ensure qunicorn.py created one or set AZURE_EXISTING_AGENT_ID.")
7678

7779
app.state.ai_client = ai_client
7880
app.state.agent = agent

src/gunicorn.conf.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333

3434
FILES_NAMES = ["product_info_1.md", "product_info_2.md"]
3535

36+
agentID = os.environ.get("AZURE_EXISTING_AGENT_ID") if os.environ.get(
37+
"AZURE_EXISTING_AGENT_ID") else os.environ.get(
38+
"AZURE_AI_AGENT_ID")
39+
40+
connection_string = os.environ.get("AZURE_EXISTING_AIPROJECT_CONNECTION_STRING") if os.environ.get("AZURE_EXISTING_AIPROJECT_CONNECTION_STRING") else os.environ.get("AZURE_AIPROJECT_CONNECTION_STRING")
3641

3742
async def create_index_maybe(
3843
ai_client: AIProjectClient, creds: AsyncTokenCredential) -> None:
@@ -190,22 +195,22 @@ async def initialize_resources():
190195
exclude_shared_token_cache_credential=True) as creds:
191196
async with AIProjectClient.from_connection_string(
192197
credential=creds,
193-
conn_str=os.environ["AZURE_AIPROJECT_CONNECTION_STRING"],
198+
conn_str=connection_string,
194199
) as ai_client:
195-
# If the environment already has AZURE_AI_AGENT_ID, try
200+
# If the environment already has AZURE_AI_AGENT_ID or AZURE_EXISTING_AGENT_ID, try
196201
# fetching that agent
197-
if os.environ.get("AZURE_AI_AGENT_ID") is not None:
202+
if agentID is not None:
198203
try:
199204
agent = await ai_client.agents.get_agent(
200-
os.environ["AZURE_AI_AGENT_ID"])
205+
agentID)
201206
logger.info(f"Found agent by ID: {agent.id}")
202207
# Update the agent with the latest resources
203208
agent = await update_agent(agent, ai_client, creds)
204209
return
205210
except Exception as e:
206211
logger.warning(
207-
"Could not retrieve agent by AZURE_AI_AGENT_ID = "
208-
f"{os.environ['AZURE_AI_AGENT_ID']}, error: {e}")
212+
"Could not retrieve agent by AZURE_EXISTING_AGENT_ID = "
213+
f"{agentID}, error: {e}")
209214

210215
# Check if an agent with the same name already exists
211216
agent_list = await ai_client.agents.list_agents()
@@ -217,15 +222,15 @@ async def initialize_resources():
217222
"Found existing agent named "
218223
f"'{agent_object.name}'"
219224
f", ID: {agent_object.id}")
220-
os.environ["AZURE_AI_AGENT_ID"] = agent_object.id
225+
os.environ["AZURE_EXISTING_AGENT_ID"] = agent_object.id
221226
# Update the agent with the latest resources
222227
agent = await update_agent(
223228
agent_object, ai_client, creds)
224229
return
225230

226231
# Create a new agent
227232
agent = await create_agent(ai_client, creds)
228-
os.environ["AZURE_AI_AGENT_ID"] = agent.id
233+
os.environ["AZURE_EXISTING_AGENT_ID"] = agent.id
229234
logger.info(f"Created agent, agent ID: {agent.id}")
230235

231236
except Exception as e:

0 commit comments

Comments
 (0)