Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Build and deploy AI applications on Azure AI Foundry using Microsoft's model catalog and AI services
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
models/deploy-model/capacity/scripts/discover_and_rank.ps1
1<#2.SYNOPSIS3Discovers available capacity for an Azure OpenAI model across all regions,4cross-references with existing projects and subscription quota, and outputs a ranked table.5.PARAMETER ModelName6The model name (e.g., "gpt-4o", "o3-mini")7.PARAMETER ModelVersion8The model version (e.g., "2025-01-31")9.PARAMETER MinCapacity10Minimum required capacity in K TPM units (default: 0, shows all)11.EXAMPLE12.\discover_and_rank.ps1 -ModelName o3-mini -ModelVersion 2025-01-31 -MinCapacity 20013#>14param(15[Parameter(Mandatory)][string]$ModelName,16[Parameter(Mandatory)][string]$ModelVersion,17[int]$MinCapacity = 018)1920$ErrorActionPreference = "Stop"2122$subId = az account show --query id -o tsv2324# Query model capacity across all regions25$capRaw = az rest --method GET `26--url "https://management.azure.com/subscriptions/$subId/providers/Microsoft.CognitiveServices/modelCapacities" `27--url-parameters api-version=2024-10-01 modelFormat=OpenAI modelName=$ModelName modelVersion=$ModelVersion `282>$null | Out-String | ConvertFrom-Json2930# Query all AI Foundry projects (AIProject kind)31$projRaw = az rest --method GET `32--url "https://management.azure.com/subscriptions/$subId/providers/Microsoft.CognitiveServices/accounts" `33--url-parameters api-version=2024-10-01 `34--query "value[?kind=='AIProject'].{Name:name, Location:location}" `352>$null | Out-String | ConvertFrom-Json3637# Build capacity map (GlobalStandard only, pick max per region)38$capMap = @{}39foreach ($item in $capRaw.value) {40$sku = $item.properties.skuName41$avail = [int]$item.properties.availableCapacity42$region = $item.location43if ($sku -eq "GlobalStandard" -and $avail -gt 0) {44if (-not $capMap[$region] -or $avail -gt $capMap[$region]) {45$capMap[$region] = $avail46}47}48}4950# Build project map51$projMap = @{}52$projSample = @{}53foreach ($p in $projRaw) {54$loc = $p.Location55if (-not $projMap[$loc]) { $projMap[$loc] = 0 }56$projMap[$loc]++57if (-not $projSample[$loc]) { $projSample[$loc] = $p.Name }58}5960# Check subscription quota per region61$quotaMap = @{}62$checkedRegions = @{}63foreach ($region in $capMap.Keys) {64if ($checkedRegions[$region]) { continue }65$checkedRegions[$region] = $true66try {67$usageData = az cognitiveservices usage list --location $region --subscription $subId -o json 2>$null | Out-String | ConvertFrom-Json68$usageEntry = $usageData | Where-Object { $_.name.value -eq "OpenAI.GlobalStandard.$ModelName" }69if ($usageEntry) {70$quotaMap[$region] = [int]$usageEntry.limit - [int]$usageEntry.currentValue71} else {72$quotaMap[$region] = 073}74} catch {75$quotaMap[$region] = -1 # Unable to check76}77}7879# Combine and rank80$results = foreach ($region in $capMap.Keys) {81$avail = $capMap[$region]82$meets = $avail -ge $MinCapacity83$quota = if ($quotaMap[$region]) { $quotaMap[$region] } else { 0 }84$quotaDisplay = if ($quota -eq -1) { "?" } elseif ($quota -gt 0) { "${quota}K" } else { "0" }85$quotaOk = $quota -gt 0 -or $quota -eq -186[PSCustomObject]@{87Region = $region88AvailableTPM = "${avail}K"89AvailableRaw = $avail90MeetsTarget = if ($meets) { "YES" } else { "no" }91Projects = if ($projMap[$region]) { $projMap[$region] } else { 0 }92SampleProject = if ($projSample[$region]) { $projSample[$region] } else { "(none)" }93QuotaAvailable = $quotaDisplay94QuotaOk = $quotaOk95}96}9798$results = $results | Sort-Object @{Expression={$_.MeetsTarget -eq "YES"}; Descending=$true},99@{Expression={$_.QuotaOk}; Descending=$true},100@{Expression={$_.Projects}; Descending=$true},101@{Expression={$_.AvailableRaw}; Descending=$true}102103# Output summary104$total = ($results | Measure-Object).Count105$matching = ($results | Where-Object { $_.MeetsTarget -eq "YES" } | Measure-Object).Count106$withQuota = ($results | Where-Object { $_.MeetsTarget -eq "YES" -and $_.QuotaOk } | Measure-Object).Count107$withProjects = ($results | Where-Object { $_.MeetsTarget -eq "YES" -and $_.Projects -gt 0 } | Measure-Object).Count108109Write-Host "Model: $ModelName v$ModelVersion | SKU: GlobalStandard | Min Capacity: ${MinCapacity}K TPM"110Write-Host "Regions with capacity: $total | Meets target: $matching | With quota: $withQuota | With projects: $withProjects"111Write-Host ""112113$results | Select-Object Region, AvailableTPM, MeetsTarget, QuotaAvailable, Projects, SampleProject | Format-Table -AutoSize114