Skip to content

Commit eb3e874

Browse files
added scripts to upload team configurations to cosmos
1 parent 4851be6 commit eb3e874

File tree

8 files changed

+637
-8
lines changed

8 files changed

+637
-8
lines changed

azure.yaml

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
# yaml-language-server: $schema=https://raw.githubusercontent.com/Azure/azure-dev/main/schemas/v1.0/azure.yaml.json
22
name: multi-agent-custom-automation-engine-solution-accelerator
3-
metadata:
4-
3+
# metadata:
4+
# template: [email protected]
55
requiredVersions:
6-
azd: ">=1.15.0 !=1.17.1"
6+
azd: ">=1.15.0 !=1.17.1"
7+
hooks:
8+
postprovision:
9+
windows:
10+
run: |
11+
Write-Host "To upload Team Configurations to Cosmos. Run the following command in PowerShell:"
12+
Write-Host "infra\scripts\Upload-Team-Config.ps1" -ForegroundColor Cyan
13+
Write-Host ""
14+
Write-Host "To index Sample Data into Azure Search. Run the following command in PowerShell:"
15+
Write-Host "infra\scripts\Process-Sample-Data.ps1" -ForegroundColor Cyan
16+
shell: pwsh
17+
interactive: true
18+
posix:
19+
run: |
20+
echo "To upload Team Configurations to Cosmos. Run the following command in Bash:"
21+
echo "bash infra/scripts/upload_team_config.sh"
22+
echo ""
23+
echo "To index Sample Data into Azure Search. Run the following command in Bash:"
24+
echo "bash infra/scripts/process_sample_data.sh"
25+
shell: sh
26+
interactive: true
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
#Requires -Version 7.0
2+
3+
param(
4+
[string]$CosmosDbName,
5+
[string]$DatabaseName,
6+
[string]$ContainerName,
7+
[string]$DirectoryPath,
8+
[string]$ResourceGroup
9+
)
10+
11+
# Get parameters from azd env, if not provided
12+
if (-not $CosmosDbName) {
13+
$CosmosDbName = $(azd env get-value COSMOSDB_ACCOUNT_NAME)
14+
}
15+
if (-not $DatabaseName) {
16+
$DatabaseName = $(azd env get-value COSMOSDB_DATABASE)
17+
}
18+
if (-not $ContainerName) {
19+
$ContainerName = $(azd env get-value COSMOSDB_CONTAINER)
20+
}
21+
if (-not $DirectoryPath) {
22+
$DirectoryPath = "data/agent_teams"
23+
}
24+
if (-not $ResourceGroup) {
25+
$ResourceGroup = $(azd env get-value AZURE_RESOURCE_GROUP)
26+
}
27+
$AzSubscriptionId = $(azd env get-value AZURE_SUBSCRIPTION_ID)
28+
29+
# Check if all required arguments are provided
30+
if (-not $CosmosDbName -or -not $DatabaseName -or -not $ContainerName -or -not $DirectoryPath) {
31+
Write-Host "Usage: .\infra\scripts\Upload-Team-Config.ps1 -CosmosDbName <CosmosDbName> -DatabaseName <DatabaseName> -ContainerName <ContainerName> -DirectoryPath <DirectoryPath> [-ResourceGroup <ResourceGroupName>]"
32+
exit 1
33+
}
34+
35+
# Authenticate with Azure
36+
try {
37+
$currentAzContext = az account show | ConvertFrom-Json -ErrorAction Stop
38+
Write-Host "Already authenticated with Azure."
39+
} catch {
40+
Write-Host "Not authenticated with Azure. Attempting to authenticate..."
41+
Write-Host "Authenticating with Azure CLI..."
42+
az login
43+
if ($LASTEXITCODE -ne 0) {
44+
Write-Host "Authentication failed."
45+
exit 1
46+
}
47+
$currentAzContext = az account show | ConvertFrom-Json
48+
}
49+
50+
# Check if user has selected the correct subscription
51+
$currentSubscriptionId = $currentAzContext.id
52+
$currentSubscriptionName = $currentAzContext.name
53+
if ($currentSubscriptionId -ne $AzSubscriptionId) {
54+
Write-Host "Current selected subscription is $currentSubscriptionName ( $currentSubscriptionId )."
55+
$confirmation = Read-Host "Do you want to continue with this subscription? (y/n)"
56+
if ($confirmation.ToLower() -ne "y") {
57+
Write-Host "Fetching available subscriptions..."
58+
$availableSubscriptions = (az account list --query "[?state=='Enabled']" | ConvertFrom-Json -AsHashtable)
59+
$subscriptionArray = $availableSubscriptions | ForEach-Object {
60+
[PSCustomObject]@{ Name = $_.name; Id = $_.id }
61+
}
62+
do {
63+
Write-Host ""
64+
Write-Host "Available Subscriptions:"
65+
Write-Host "========================"
66+
for ($i = 0; $i -lt $subscriptionArray.Count; $i++) {
67+
Write-Host "$($i+1). $($subscriptionArray[$i].Name) ( $($subscriptionArray[$i].Id) )"
68+
}
69+
Write-Host "========================"
70+
Write-Host ""
71+
[int]$subscriptionIndex = Read-Host "Enter the number of the subscription (1-$($subscriptionArray.Count)) to use"
72+
if ($subscriptionIndex -ge 1 -and $subscriptionIndex -le $subscriptionArray.Count) {
73+
$selectedSubscription = $subscriptionArray[$subscriptionIndex-1]
74+
$selectedSubscriptionName = $selectedSubscription.Name
75+
$selectedSubscriptionId = $selectedSubscription.Id
76+
$result = az account set --subscription $selectedSubscriptionId
77+
if ($LASTEXITCODE -eq 0) {
78+
Write-Host "Switched to subscription: $selectedSubscriptionName ( $selectedSubscriptionId )"
79+
break
80+
} else {
81+
Write-Host "Failed to switch to subscription: $selectedSubscriptionName ( $selectedSubscriptionId )."
82+
}
83+
} else {
84+
Write-Host "Invalid selection. Please try again."
85+
}
86+
} while ($true)
87+
} else {
88+
Write-Host "Proceeding with the current subscription: $currentSubscriptionName ( $currentSubscriptionId )"
89+
az account set --subscription $currentSubscriptionId
90+
}
91+
} else {
92+
Write-Host "Proceeding with the subscription: $currentSubscriptionName ( $currentSubscriptionId )"
93+
az account set --subscription $currentSubscriptionId
94+
}
95+
96+
$userPrincipalId = $(az ad signed-in-user show --query id -o tsv)
97+
98+
$cosmosIsPublicAccessDisabled = $false
99+
if ($ResourceGroup) {
100+
$cosmosPublicAccess = $(az cosmosdb show --name $CosmosDbName --resource-group $ResourceGroup --query "publicNetworkAccess" -o tsv)
101+
if ($cosmosPublicAccess -eq "Disabled") {
102+
$cosmosIsPublicAccessDisabled = $true
103+
Write-Host "Enabling public access for CosmosDB: $CosmosDbName"
104+
az cosmosdb update --name $CosmosDbName --resource-group $ResourceGroup --public-network-access enabled --output none
105+
if ($LASTEXITCODE -ne 0) {
106+
Write-Host "Error: Failed to enable public access for CosmosDB."
107+
exit 1
108+
}
109+
Write-Host "Public access enabled for CosmosDB: $CosmosDbName"
110+
} else {
111+
Write-Host "Public access is already enabled for CosmosDB: $CosmosDbName"
112+
}
113+
}
114+
115+
# Determine the correct Python command
116+
$pythonCmd = $null
117+
118+
try {
119+
$pythonVersion = (python --version) 2>&1
120+
if ($pythonVersion -match "Python \d") {
121+
$pythonCmd = "python"
122+
}
123+
}
124+
catch {
125+
# Do nothing, try python3 next
126+
}
127+
128+
if (-not $pythonCmd) {
129+
try {
130+
$pythonVersion = (python3 --version) 2>&1
131+
if ($pythonVersion -match "Python \d") {
132+
$pythonCmd = "python3"
133+
}
134+
}
135+
catch {
136+
Write-Host "Python is not installed on this system or it is not added in the PATH."
137+
exit 1
138+
}
139+
}
140+
141+
if (-not $pythonCmd) {
142+
Write-Host "Python is not installed on this system or it is not added in the PATH."
143+
exit 1
144+
}
145+
146+
# Create virtual environment
147+
$venvPath = "infra/scripts/scriptenv"
148+
if (Test-Path $venvPath) {
149+
Write-Host "Virtual environment already exists. Skipping creation."
150+
} else {
151+
Write-Host "Creating virtual environment"
152+
& $pythonCmd -m venv $venvPath
153+
}
154+
155+
# Activate the virtual environment
156+
$activateScript = ""
157+
if (Test-Path (Join-Path -Path $venvPath -ChildPath "bin/Activate.ps1")) {
158+
$activateScript = Join-Path -Path $venvPath -ChildPath "bin/Activate.ps1"
159+
} elseif (Test-Path (Join-Path -Path $venvPath -ChildPath "Scripts/Activate.ps1")) {
160+
$activateScript = Join-Path -Path $venvPath -ChildPath "Scripts/Activate.ps1"
161+
}
162+
if ($activateScript) {
163+
Write-Host "Activating virtual environment"
164+
. $activateScript
165+
} else {
166+
Write-Host "Error activating virtual environment. Requirements may be installed globally."
167+
}
168+
169+
# Install the requirements
170+
Write-Host "Installing requirements"
171+
pip install --quiet -r infra/scripts/requirements.txt
172+
Write-Host "Requirements installed"
173+
174+
# Run the Python script to upload team configuration
175+
Write-Host "Running the python script to upload team configuration"
176+
$process = Start-Process -FilePath $pythonCmd -ArgumentList "infra/scripts/team-config-scripts/upload_team_config.py", $CosmosDbName, $DatabaseName, $ContainerName, $DirectoryPath, $userPrincipalId -Wait -NoNewWindow -PassThru
177+
if ($process.ExitCode -ne 0) {
178+
Write-Host "Error: Team configuration upload failed."
179+
exit 1
180+
}
181+
182+
#disable public access for cosmos
183+
if ($cosmosIsPublicAccessDisabled) {
184+
Write-Host "Disabling public access for CosmosDB: $CosmosDbName"
185+
az cosmosdb update --name $CosmosDbName --resource-group $ResourceGroup --public-network-access disabled --output none
186+
if ($LASTEXITCODE -ne 0) {
187+
Write-Host "Error: Failed to disable public access for CosmosDB."
188+
exit 1
189+
}
190+
Write-Host "Public access disabled for CosmosDB: $CosmosDbName"
191+
}
192+
193+
Write-Host "Script executed successfully. Team configuration uploaded."

infra/scripts/process_sample_data.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ fi
132132
echo "Files uploaded successfully to blob storage."
133133

134134
# Determine the correct Python command
135-
if command -v python3 && python3 --version &> /dev/null; then
136-
PYTHON_CMD="python3"
137-
elif command -v python && python --version &> /dev/null; then
135+
if command -v python && python --version &> /dev/null; then
138136
PYTHON_CMD="python"
137+
elif command -v python3 && python3 --version &> /dev/null; then
138+
PYTHON_CMD="python3"
139139
else
140140
echo "Python is not installed on this system. Or it is not added in the PATH."
141141
exit 1
@@ -166,7 +166,7 @@ pip install --quiet -r infra/scripts/requirements.txt
166166
echo "Requirements installed"
167167

168168
echo "Running the python script to index data"
169-
python infra/scripts/index_datasets.py "$storageAccount" "$blobContainer" "$aiSearch" "$aiSearchIndex"
169+
$PYTHON_CMD infra/scripts/index_datasets.py "$storageAccount" "$blobContainer" "$aiSearch" "$aiSearchIndex"
170170
if [ $? -ne 0 ]; then
171171
echo "Error: Indexing python script execution failed."
172172
exit 1

infra/scripts/requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
azure-search-documents == 11.5.3
22
azure-identity == 1.24.0
3-
azure-storage-blob == 12.26.0
3+
azure-storage-blob == 12.26.0
4+
semantic-kernel==1.35.3
5+
azure-cosmos==4.9.0
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from typing import Any, Dict, List, Literal, Optional
2+
import uuid
3+
from semantic_kernel.kernel_pydantic import Field, KernelBaseModel
4+
from datetime import datetime, timezone
5+
6+
class BaseDataModel(KernelBaseModel):
7+
"""Base data model with common fields."""
8+
9+
id: str = Field(default_factory=lambda: str(uuid.uuid4()))
10+
timestamp: Optional[datetime] = Field(
11+
default_factory=lambda: datetime.now(timezone.utc)
12+
)
13+
14+
class StartingTask(KernelBaseModel):
15+
id: str
16+
name: str
17+
prompt: str
18+
created: str
19+
creator: str
20+
logo: str
21+
22+
class TeamAgent(KernelBaseModel):
23+
input_key: str
24+
type: str
25+
name: str
26+
deployment_name: str
27+
system_message: str = ""
28+
description: str = ""
29+
icon: str
30+
index_name: str = ""
31+
use_rag: bool = False
32+
use_mcp: bool = False
33+
use_bing: bool = False
34+
use_reasoning: bool = False
35+
coding_tools: bool = False
36+
37+
class TeamConfiguration(BaseDataModel):
38+
team_id: str
39+
data_type: Literal["team_config"] = Field("team_config", Literal=True)
40+
session_id: str # Partition key
41+
name: str
42+
status: str
43+
created: str
44+
created_by: str
45+
agents: List[TeamAgent] = Field(default_factory=list)
46+
description: str = ""
47+
logo: str = ""
48+
plan: str = ""
49+
starting_tasks: List[StartingTask] = Field(default_factory=list)
50+
user_id: str # Who uploaded this configuration

0 commit comments

Comments
 (0)